-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (57 loc) · 1.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
package main
import (
"flag"
"fmt"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/mfjkri/OneNUS-Backend/database"
"github.com/mfjkri/OneNUS-Backend/routes"
"github.com/mfjkri/OneNUS-Backend/seed"
"github.com/mfjkri/OneNUS-Backend/utils"
)
func init() {
os.Setenv("TZ", "Asia/Singapore")
utils.LoadEnv()
database.Connect()
database.Migrate()
}
func CORSConfig() cors.Config {
corsConfig := cors.DefaultConfig()
corsConfig.AllowOrigins = []string{
"http://localhost:3000",
"http://192.168.0.100:3000",
"https://app.onenus.link",
}
corsConfig.AllowCredentials = true
corsConfig.AddAllowHeaders("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers", "Content-Type", "X-XSRF-TOKEN", "Accept", "Origin", "X-Requested-With", "Authorization")
corsConfig.AddAllowMethods("GET", "POST", "DELETE")
return corsConfig
}
func main() {
// Some command utilities
cmd := flag.String("cmd", "", "")
flag.Parse()
str_cmd := string(*cmd)
// Set Gin mode based on env var
gin.SetMode(os.Getenv("GIN_MODE"))
// Create a new router
router := gin.Default()
// Configure CORS
router.Use(cors.New(CORSConfig()))
// Initialize Routes
routes.RegisterPublicRoutes(router)
routes.RegisterProtectedRoutes(router)
// Check for any command parameters used
if str_cmd == "reset" {
seed.DeleteAll()
} else if str_cmd == "seed" {
seed.GenerateData()
} else if str_cmd == "update" {
seed.UpdateUsers()
seed.UpdatePosts()
}
fmt.Println("Now listening on port", os.Getenv("PORT"), "...")
// Start listening
router.Run()
}