Browse Source

add files

master^2
Justin Duch 1 year ago
committed by Justin Duch
parent
commit
200db9eed7
  1. 4
      .gitignore
  2. 33
      Dockerfile
  3. 9
      Makefile
  4. 58
      README.md
  5. 8
      go.mod
  6. 4
      go.sum
  7. 97
      main.go

4
.gitignore

@ -12,6 +12,4 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

33
Dockerfile

@ -0,0 +1,33 @@
FROM ubuntu:18.04
ARG COMMIT
ENV COMMIT ${COMMIT:-master}
ENV DEBIAN_FRONTEND noninteractive
# Install deps
RUN apt-get update && apt-get install -y \
autoconf automake build-essential curl git libsnappy-dev libtool pkg-config
# Build libpostal
RUN git clone https://github.com/openvenues/libpostal -b $COMMIT /opt/libpostal
WORKDIR /opt/libpostal
RUN mkdir -p /data/libpostal_data && \
./bootstrap.sh && \
./configure --datadir=/data/libpostal_data && \
make && make install && \
ldconfig
# Install go
RUN curl -s https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz | tar -xz -C /usr/local
ENV PATH $PATH:/usr/local/go/bin
# Build libpostal-rest
WORKDIR /src
COPY . .
RUN go build -o libpostal-rest
EXPOSE 8080
CMD ./libpostal-rest

9
Makefile

@ -0,0 +1,9 @@
IMAGE := docker.duch.me/jd/libpostal-rest:latest
docker:
@echo "\n~> building docker image"
@docker build . -t $(IMAGE)
push:
@echo "\n~> push to registry"
@docker push $(IMAGE)

58
README.md

@ -1,3 +1,59 @@
# libpostal-rest
REST API for libpostal.
REST API for libpostal.
**Note:** Training data (~2GB) is included in the Docker image.
## Development/Setup
Requirements:
* [libpostal](https://github.com/openvenues/libpostal)
* [go1.15+](https://golang.org/)
REST server defaults to port 8080:
```bash
go run main.go
```
## Examples
Formatted by [jq](https://stedolan.github.io/jq/).
### Parser
```bash
➜ curl -X POST -d '{"query": "30 place des saisons, 92400 courbevoie france"}' localhost:8080/parser | jq
[
{
"label": "house_number",
"value": "30"
},
{
"label": "road",
"value": "place des saisons"
},
{
"label": "postcode",
"value": "92400"
},
{
"label": "city",
"value": "courbevoie"
},
{
"label": "country",
"value": "france"
}
]
```
### Expand
```bash
➜ curl -X POST -d '{"query": "deux cent onze ave jean jaurès"}' localhost:8080/expand | jq
[
"211 avenue jean jaures"
]
```

8
go.mod

@ -0,0 +1,8 @@
module git.duch.me/justin/libpostal-rest
go 1.15
require (
github.com/gorilla/mux v1.8.0
github.com/openvenues/gopostal v0.0.0-20171226154602-e0184512a45d
)

4
go.sum

@ -0,0 +1,4 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/openvenues/gopostal v0.0.0-20171226154602-e0184512a45d h1:KJ+N55d9zLN8fTg3NchLdmmAmPieXC5E6UNJ8zFFttU=
github.com/openvenues/gopostal v0.0.0-20171226154602-e0184512a45d/go.mod h1:Ycrd7XnwQdumHzpB/6WEa85B4WNdbLC6Wz4FAQNkaV0=

97
main.go

@ -0,0 +1,97 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"time"
"github.com/gorilla/mux"
expand "github.com/openvenues/gopostal/expand"
parser "github.com/openvenues/gopostal/parser"
)
type Request struct {
Query string `json:"query"`
}
func main() {
host := os.Getenv("LISTEN_HOST")
if host == "" {
host = "0.0.0.0"
}
port := os.Getenv("LISTEN_PORT")
if port == "" {
port = "8080"
}
listenSpec := fmt.Sprintf("%s:%s", host, port)
certFile := os.Getenv("SSL_CERT_FILE")
keyFile := os.Getenv("SSL_KEY_FILE")
router := mux.NewRouter()
router.HandleFunc("/health", HealthHandler).Methods("GET")
router.HandleFunc("/expand", ExpandHandler).Methods("POST")
router.HandleFunc("/parser", ParserHandler).Methods("POST")
s := &http.Server{Addr: listenSpec, Handler: router}
go func() {
if certFile != "" && keyFile != "" {
fmt.Printf("listening on https://%s\n", listenSpec)
s.ListenAndServeTLS(certFile, keyFile)
} else {
fmt.Printf("listening on http://%s\n", listenSpec)
s.ListenAndServe()
}
}()
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
fmt.Println("\nShutting down the server...")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
s.Shutdown(ctx)
fmt.Println("Server stopped")
}
func HealthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func ExpandHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var req Request
q, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(q, &req)
expansions := expand.ExpandAddress(req.Query)
expansionThing, _ := json.Marshal(expansions)
w.Write(expansionThing)
}
func ParserHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var req Request
q, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(q, &req)
parsed := parser.ParseAddress(req.Query)
parseThing, _ := json.Marshal(parsed)
w.Write(parseThing)
}
Loading…
Cancel
Save