-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (52 loc) · 1.58 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-openapi/runtime/middleware"
"github.com/gorilla/mux"
"github.com/invad0r/building-microservices-with-go/handlers"
)
func main() {
l := log.New(os.Stdout, "product-api ", log.LstdFlags)
ph := handlers.NewProducts(l)
sm := mux.NewRouter()
getRouter := sm.Methods(http.MethodGet).Subrouter()
getRouter.HandleFunc("/", ph.GetProducts)
postRouter := sm.Methods(http.MethodPost).Subrouter()
postRouter.HandleFunc("/", ph.AddProduct)
postRouter.Use(ph.MiddlewareProductValidation)
putRouter := sm.Methods(http.MethodPut).Subrouter()
putRouter.HandleFunc("/{id:[0-9+]}", ph.UpdateProduct)
putRouter.Use(ph.MiddlewareProductValidation)
deleteRouter := sm.Methods(http.MethodDelete).Subrouter()
deleteRouter.HandleFunc("/{id:[0-9+]}", ph.DeleteProduct)
opts := middleware.RedocOpts{SpecURL: "/swagger.yml"}
sh := middleware.Redoc(opts, nil)
getRouter.Handle("/docs", sh)
getRouter.Handle("/swagger.yml", http.FileServer(http.Dir("./")))
s := &http.Server{
Addr: ":9090",
Handler: sm,
IdleTimeout: 120 * time.Second,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
}
go func() {
l.Println("starting server on port 9090")
err := s.ListenAndServe()
if err != nil {
l.Fatal(err)
}
}()
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt)
signal.Notify(sigChan, os.Kill)
sig := <-sigChan
l.Println("Recieved terminate, graceful shutdown", sig)
tc, _ := context.WithTimeout(context.Background(), 30*time.Second)
s.Shutdown(tc)
}