-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
65 lines (55 loc) · 1.4 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
package main
import (
"context"
"github.com/gin-gonic/gin"
cors "github.com/rs/cors/wrapper/gin"
"log"
"net/http"
"os"
"os/signal"
"time"
)
const ginContextTimeout = 10
func main() {
router := gin.New()
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // Todo env
AllowedMethods: []string{"GET"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"*"},
AllowCredentials: true,
}))
router.Use(setHeader())
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hi")
})
router.GET("/image", getImage)
srv := &http.Server{
Addr: ":8080", // Todo env
Handler: router,
}
go func() {
log.Printf("listen: %s\n", srv.Addr)
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), ginContextTimeout*time.Second)
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
defer cancel()
log.Println("Server exiting")
}
func setHeader() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("x-content-type-options", "nosniff")
c.Writer.Header().Set("x-xss-protection", "1; mode=block")
c.Writer.Header().Set("x-frame-options", "DENY")
c.Writer.Header().Set("cache-control", "public, max-age=86400")
}
}