-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
77 lines (65 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"context"
"embed"
"fmt"
"os"
"github.com/gosom/kit/logging"
"github.com/gosom/kit/web"
"github.com/joho/godotenv"
"github.com/gosom/address-parser-go-rest/addressparser/libpostal"
"github.com/gosom/address-parser-go-rest/addressparser/ports"
)
//go:generate swag i -g main.go --pd
//go:embed docs/swagger.json
var specFs embed.FS
// @title Address Parser API
// @version 1.0.0
// @description This is the API for the address parser service
// @contact.name Giorgos Komninos
// @contact.url http://blog.gkomninos.com
// @host localhost:8080
// @BasePath /
// @accept json
// @produce json
// @query.collection.format multi
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := run(ctx); err != nil {
panic(err)
}
}
func run(ctx context.Context) error {
if err := godotenv.Load(); err != nil {
return err
}
docPath := os.Getenv("DOCS_PATH")
if docPath == "" {
docPath = "/docs"
}
routerCfg := web.RouterConfig{
SwaggerUI: &web.SwaggerUIConfig{
SpecName: "AddressParser API",
SpecFile: "/docs/swagger.json",
Path: docPath,
SpecFS: specFs,
},
}
router := web.NewRouter(routerCfg)
log := logging.Get()
parser := libpostal.NewLibPostalParser(log.With("component", "libpostal"))
hn := ports.NewAddressParserHandler(log.With("component", "handler"), parser)
hn.RegisterRouters(router)
addr := os.Getenv("PARSER_HTTP_ADDR")
if addr == "" {
addr = ":8080"
}
webCfg := web.ServerConfig{
Host: addr,
Router: router,
}
log.Info(fmt.Sprintf("Starting server at %s", addr))
webSvc := web.NewHttpServer(webCfg)
return webSvc.ListenAndServe(ctx)
}