-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.go
79 lines (67 loc) · 1.75 KB
/
server.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
79
package backend
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
// Server is the main program
func Server() {
// read command-line flags
host := flag.String("host", "localhost", "Server host")
port := flag.Int("port", 8080, "Server port")
docker := flag.Bool("docker", false, "Running in docker")
flag.Parse()
// prepare service, http handler and server
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
service := Service{}
// apis
api := router.Group("/api")
api.GET("/products", service.ProductService) // api: /api/products
api.POST("/orders", service.OrderService) // api: /api/orders
// serve static files
router.Use(static.Serve("/", static.LocalFile("./build", true)))
router.NoRoute(func(c *gin.Context) { // fallback
c.File("./build/index.html")
})
var serverPath string
if *docker {
serverPath = "0.0.0.0:8080"
log.Println("Server started at http://localhost:8080 ...")
} else {
serverPath = fmt.Sprintf("%s:%d", *host, *port)
log.Printf("Server started at http://%s ...\n", serverPath)
}
server := &http.Server{
Addr: serverPath,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
// start server
go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatalln(err)
}
}()
// graceful shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalln(err)
}
log.Println("Server exiting")
}