forked from leogregianin/brcep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
69 lines (58 loc) · 1.61 KB
/
server.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
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/leogregianin/brcep/api"
"github.com/leogregianin/brcep/api/cepaberto"
"github.com/leogregianin/brcep/api/viacep"
"github.com/leogregianin/brcep/config"
"github.com/leogregianin/brcep/config/env"
"github.com/leogregianin/brcep/config/flag"
"github.com/leogregianin/brcep/handler"
)
func main() {
fmt.Println(` ___. `)
fmt.Println(` \_ |_________ ____ ____ ______ `)
fmt.Println(` | __ \_ __ \_/ ___\/ __ \\____ \ `)
fmt.Println(` | \_\ \ | \/\ \__\ ___/| |_> > `)
fmt.Println(` |___ /__| \___ >___ > __/ `)
fmt.Println(` \/ \/ \/|__| `)
cfg, err := config.NewConfig([]config.Loader{
flag.NewFlagLoader(),
env.NewEnvLoader(),
})
if err != nil {
panic(err)
}
gin.SetMode(cfg.GetGinOperationMode())
var (
cepApis = map[string]api.Api{
viacep.ID: viacep.NewViaCepApi(cfg.ViaCepUrl, http.DefaultClient),
}
)
if len(cfg.CepAbertoToken) > 0 {
cepApis[cepaberto.ID] = cepaberto.NewCepAbertoApi(
cfg.CepAbertoUrl,
cfg.CepAbertoToken,
http.DefaultClient)
}
var cepHandler = &handler.CepHandler{
PreferredApi: cfg.PreferredAPI,
CepApis: cepApis,
}
router := gin.Default()
router.Use(gin.ErrorLogger())
router.GET("/:cep/json", cepHandler.Handle)
server := &http.Server{
Addr: cfg.Address,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}