/
main.go
156 lines (131 loc) · 3.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"os"
"strings"
"time"
helmet "github.com/danielkov/gin-helmet"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
api "github.com/nettica-com/nettica-admin/api"
auth "github.com/nettica-com/nettica-admin/auth"
docs "github.com/nettica-com/nettica-admin/cmd/nettica-api/docs"
"github.com/nettica-com/nettica-admin/mongo"
util "github.com/nettica-com/nettica-admin/util"
version "github.com/nettica-com/nettica-admin/version"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
var (
cacheDb = cache.New(60*time.Minute, 10*time.Minute)
)
func init() {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.DebugLevel)
}
// @title Nettica API
// @description Nettica API documentation
// @BasePath /api/v1.0
// @host my.nettica.com
// @contactName Nettica
// @contactEmail support@nettica.com
// @contactURL https://nettica.com
// @schemes https
// @produce json
// @consumes json
// @license MIT
// @securityDefinitions.apiKey apiKey
// @in header
// @name X-API-KEY
func main() {
log.Infof("Starting Nettica version: %s", version.Version)
// load .env environment variables
err := godotenv.Load()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to load .env file")
}
if os.Getenv("GIN_MODE") == "debug" {
// set gin release debug
gin.SetMode(gin.DebugMode)
} else {
// set gin release mode
gin.SetMode(gin.ReleaseMode)
// disable console color
gin.DisableConsoleColor()
// log level info
log.SetLevel(log.InfoLevel)
}
// creates a gin router with default middleware: logger and recovery (crash-free) middleware
app := gin.New()
app.Use(gin.Logger())
app.Use(gin.Recovery())
docs.SwaggerInfo.BasePath = "/api/v1.0"
docs.SwaggerInfo.Host = os.Getenv("SERVER")[8:]
docs.SwaggerInfo.Schemes = []string{"https"}
// cors middleware
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AddAllowHeaders("Authorization", util.AuthTokenHeaderName)
app.Use(cors.New(config))
// protection middleware
app.Use(helmet.Default())
// add cache storage to gin app
app.Use(func(ctx *gin.Context) {
ctx.Set("cache", cacheDb)
ctx.Next()
})
// serve static files
app.Use(static.Serve("/", static.LocalFile("./ui/dist", false)))
// setup Oauth2 client
oauth2Client, err := auth.GetAuthProvider()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to setup Oauth2")
}
app.Use(func(ctx *gin.Context) {
ctx.Set("oauth2Client", oauth2Client)
ctx.Next()
})
// apply api routes public
api.ApplyRoutes(app, false)
// simple middleware to check auth
app.Use(func(c *gin.Context) {
cacheDb := c.MustGet("cache").(*cache.Cache)
token := util.GetCleanAuthToken(c)
oauth2Token, exists := cacheDb.Get(token)
if exists && oauth2Token.(*oauth2.Token).AccessToken == token {
// will be accessible in auth endpoints
c.Set("oauth2Token", oauth2Token)
c.Next()
return
}
// avoid 401 page for refresh after logout
if !strings.Contains(c.Request.URL.Path, "/api/") {
c.Redirect(301, "/index.html")
return
}
c.Next()
// c.AbortWithStatus(http.StatusUnauthorized)
// return
})
// apply api router private
api.ApplyRoutes(app, true)
// Initialize the database
err = mongo.Initialize()
if err != nil {
log.Error(err)
}
err = app.Run(fmt.Sprintf("%s:%s", os.Getenv("LISTEN_ADDR"), os.Getenv("PORT")))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to start server")
}
}