REST API for libpostal.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
2.0 KiB

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, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
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)
}