-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (83 loc) · 2.51 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
package main
import (
"fmt"
"os"
"time"
"github.com/TheLazarusNetwork/LazarusTunnel/api"
"github.com/TheLazarusNetwork/LazarusTunnel/core"
"github.com/TheLazarusNetwork/LazarusTunnel/middleware"
"github.com/TheLazarusNetwork/LazarusTunnel/util"
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"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.DebugLevel)
// Get Hostname for updating Log StandardFields
HostName, err := os.Hostname()
if err != nil {
log.Infof("Error in getting the Hostname: %v", err)
} else {
util.StandardFields = log.Fields{
"hostname": HostName,
"appname": "TunnelAPI",
}
}
// Check if loading environment variables from .env file is required
if os.Getenv("LOAD_CONFIG_FILE") == "" {
// Load environment variables from .env file
err = godotenv.Load()
if err != nil {
log.WithFields(util.StandardFields).Fatalf("Error in reading the config file: %v", err)
}
}
// initialize json files and update config files
core.Init()
middleware.UpdateCaddyConfig()
middleware.UpdateNginxConfig()
}
func main() {
log.WithFields(util.StandardFields).Infof("Starting TunnelServices Version: %s", util.Version)
if os.Getenv("RUNTYPE") == "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
ginApp := gin.Default()
// cors middleware
config := cors.DefaultConfig()
config.AllowAllOrigins = true
ginApp.Use(cors.New(config))
// protection middleware
ginApp.Use(helmet.Default())
// add cache storage to gin ginApp
ginApp.Use(func(ctx *gin.Context) {
ctx.Set("cache", cache.New(60*time.Minute, 10*time.Minute))
ctx.Next()
})
// serve static files
ginApp.Use(static.Serve("/", static.LocalFile("./ui", false)))
// no route redirect to frontend app
ginApp.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"status": 404, "message": "Invalid Endpoint Request"})
})
// Apply API Routes
api.ApplyRoutes(ginApp)
err := ginApp.Run(fmt.Sprintf("%s:%s", os.Getenv("SERVER"), os.Getenv("PORT")))
if err != nil {
log.WithFields(util.StandardFields).Fatal("Failed to Start Server")
}
}