/
main.go
61 lines (51 loc) · 1.35 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
package main
import (
"fmt"
"net/http"
"time"
"github.com/apex/httplog"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/caarlos0/cepinator/cache"
"github.com/caarlos0/cepinator/controller"
"github.com/caarlos0/env"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
var version = "dev"
// Config type
type Config struct {
Port string `env:"PORT" envDefault:"3000"`
RedisURL string `env:"REDIS_URL" envDefault:":6379"`
}
func init() {
log.SetHandler(logfmt.Default)
log.SetLevel(log.InfoLevel)
}
func main() {
var config Config
if err := env.Parse(&config); err != nil {
log.WithError(err).Fatal("failed to load config")
}
var log = log.WithField("version", version).
WithField("port", config.Port).
WithField("redis", config.RedisURL)
var cache = cache.New(config.RedisURL)
defer func() {
if err := cache.Close(); err != nil {
log.WithError(err).Error("failed to close cache")
}
}()
var r = mux.NewRouter()
r.HandleFunc("/{cep}", controller.CEP(cache))
var srv = &http.Server{
Handler: httplog.New(handlers.CompressHandler(r)),
Addr: fmt.Sprintf("0.0.0.0:%v", config.Port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Info("starting up...")
if err := srv.ListenAndServe(); err != nil {
log.WithError(err).Error("failed to start up server")
}
}