-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (38 loc) · 1.24 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/pjserol/api-rest-user/common/logs"
"github.com/pjserol/api-rest-user/config"
"github.com/pjserol/api-rest-user/db"
"github.com/pjserol/api-rest-user/handler"
)
func main() {
env := config.InitEnvironment()
ctx := context.Background()
if env.IsTestMode {
// display the line and file in the logs
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
// run in a goroutine to don't be a blocking call
go db.Initialise(ctx)
// See http://www.gorillatoolkit.org/pkg/mux
r := mux.NewRouter()
r.HandleFunc("/health-check/", handler.HealthCheckHandler).Methods("GET")
r.HandleFunc("/v1/user/{userId:.*?}/", handler.GetUserHandler).Methods("GET")
r.HandleFunc("/v1/user/", handler.PostUserHandler).Methods("POST")
r.HandleFunc("/v1/user/{userId:.*?}/", handler.PutUserHandler).Methods("PUT")
srv := &http.Server{
Handler: r,
Addr: fmt.Sprintf("0.0.0.0:%d", env.PortNumber),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
logs.Log(ctx, fmt.Sprintf("environment:%s::Ready to serve requests on 0.0.0.0:%d", env.AppEnvironment, env.PortNumber))
logs.Log(ctx, "API started!")
log.Fatal(srv.ListenAndServe())
}