-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (57 loc) · 2.18 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/Rahul-D78/micro-go/handler"
"github.com/gorilla/mux"
)
func main() {
l := log.New(os.Stdout, "Microservices in GO ", log.LstdFlags)
ph := handler.NewProducts(l)
//converting the function into a handler type and registering it into DefaultServeMux
//DefaultServeMux is a server multiplexer which have the logic to call the function based on the path given
// Read More about it https://pkg.go.dev/net/http#ServeMux
//create a new ServeMux
sm := mux.NewRouter()
getRouter := sm.Methods(http.MethodGet).Subrouter()
getRouter.HandleFunc("/", ph.GetProducts)
putRouter := sm.Methods(http.MethodPut).Subrouter()
putRouter.HandleFunc("/{id:[0-9]+}", ph.UpdateProducts)
postRouter := sm.Methods(http.MethodPost).Subrouter()
postRouter.HandleFunc("/", ph.AddProduct)
// sm.Handle("/products", ph)
//modifying for handling blocking connections
s := &http.Server{
Addr: ":8080",
Handler: sm,
IdleTimeout: 120 * time.Second,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
}
//start the server
go func() {
err := s.ListenAndServe()
if err != nil {
l.Fatal(err)
}
}()
//trap sigterm or interput and gracefully shutdown the server
//signal.notify is going to brodcast a message on this channel when even an operting system kill command or interrupt is recieved
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt)
signal.Notify(sigChan, os.Kill)
//Block until a signal is recieved
sig := <-sigChan
l.Println("Recieved terminate, gracefully shutdown", sig)
//In case of upgrade a version or some other work which needed to shut the server down can potentially loose all
//the clients connection but doing it `gracefully` using the GO server
//Wait until the request that are currently handled by the function have completed it will then shutdown
//30 secs to attempt to gracefully shuts down if the handlers are still working after those 40 secs forcefully close it
// gracefully shutdown the server, waiting max 30 seconds for current operations to complete
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
s.Shutdown(ctx)
}