-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (64 loc) · 1.87 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
78
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/opentracing/opentracing-go"
"github.com/rs/cors"
"github.com/windbnb/user-service/cronUtil"
handler "github.com/windbnb/user-service/handler"
repository "github.com/windbnb/user-service/repository"
router "github.com/windbnb/user-service/router"
service "github.com/windbnb/user-service/service"
"github.com/windbnb/user-service/tracer"
util "github.com/windbnb/user-service/util"
)
func main() {
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
db := util.ConnectToDatabase()
service.InitJWTKey()
tracer, closer := tracer.Init("user-service")
opentracing.SetGlobalTracer(tracer)
router := router.ConfigureRouter(&handler.Handler{
Tracer: tracer,
Closer: closer,
Service: &service.UserService{
Repo: &repository.Repository{
Db: db}}})
cronUtil.ConfigureCronJobs(db)
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3005"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowCredentials: true,
Debug: true,
AllowedHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
})
servicePath, servicePathFound := os.LookupEnv("SERVICE_PATH")
if !servicePathFound {
servicePath = "localhost:8081"
}
srv := &http.Server{Addr: servicePath, Handler: c.Handler(router)}
go func() {
log.Println("server starting")
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}()
<-quit
defer db.Close()
log.Println("service shutting down ...")
// gracefully stop server
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal(err)
}
log.Println("server stopped")
}