-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
120 lines (97 loc) · 2.93 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"aif/configs"
"aif/utils/log"
"context"
"fmt"
"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
"net/http"
"os"
"os/signal"
"strconv"
"time"
)
const version = "1.0 Alpha"
var router *gin.Engine
// Configuration .
var configuration *configs.ViperConfiguration
func init() {
configuration = configs.NewConfiguration()
configuration.Init()
debug := configuration.GetBool("debug")
log.Init(debug)
log.Println("==================================================")
log.Println("Starting IPFS - Arweave Bridge version: " + version)
log.Println("==================================================")
log.Println()
}
func main() {
router = gin.New()
if configuration.GetBool("debug") {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(requestIDMiddleware())
router.Use(corsMiddleware())
router.Use(configurationMiddleware(configuration))
InitializeRouter()
server := &http.Server{
Addr: configuration.Get("server.host") + ":" + strconv.Itoa(configuration.GetInt("server.port")),
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
MaxHeaderBytes: 1 << 10, // 1Mb
}
server.SetKeepAlivesEnabled(true)
// Serve'em
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("listen: %s\n", err)
}
}()
log.Printf("Running on %s:%s", configuration.Get("server.host"), strconv.Itoa(configuration.GetInt("server.port")))
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("initiated server shutdown")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("server shutdown:", err)
}
log.Println("server exiting. bye!")
}
// requestIDMiddleware adds x-request-id
func requestIDMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("X-Request-Id", uuid.NewV4().String())
c.Next()
}
}
func corsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
// configurationMiddleware will add the configuration to the context
func configurationMiddleware(config *configs.ViperConfiguration) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("configuration", config)
c.Next()
}
}