-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (107 loc) · 3.62 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
package main
import (
"errors"
"flag"
"fmt"
"html/template"
env "jwt-auth/config"
"jwt-auth/controllers"
db "jwt-auth/database"
m "jwt-auth/middlewares"
// "jwt-auth/models"
tempConf "jwt-auth/templates"
"os"
"github.com/gin-gonic/gin"
_ "github.com/joho/godotenv/autoload"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func loadEnv() {
env.PORT = os.Getenv("PORT")
env.JWT_KEY = os.Getenv("JWT_KEY")
env.HOST_SERVER = os.Getenv("HOST_SERVER")
env.EMAIL_PORT = env.GetEnvAsInt("EMAIL_PORT", 465)
env.EMAIL_DOMAIN = os.Getenv("EMAIL_DOMAIN")
env.EMAIL_USER = os.Getenv("EMAIL_USER")
env.EMAIL_PASS = os.Getenv("EMAIL_PASS")
env.CACHE_PASS = os.Getenv("CACHE_PASS")
env.MAINDB_PASS = os.Getenv("MAINDB_PASS")
}
func loadTemplates() {
tempConf.SignUpEmailTemplate = template.Must(template.ParseFiles("./templates/signupEmailTemplate.html"))
tempConf.ResetEmailTemplate = template.Must(template.ParseFiles("./templates/resetEmailTemplate.html"))
tempConf.ResetSuccessEmailTemplate = template.Must(template.ParseFiles("./templates/resetSuccessEmailTemplate.html"))
}
func initializeDBs(dev bool) {
if dev {
db.MainDB.Connect(fmt.Sprintf("root:%s@tcp(localhost:3306)/main_DB?parseTime=true", env.MAINDB_PASS), &gorm.Config{})
// db.MainDB.Migrate(&models.User{})
db.SessionDB.Connect(fmt.Sprintf("root:%s@tcp(localhost:3306)/session_DB?parseTime=true", env.MAINDB_PASS), &gorm.Config{})
db.SessionDB.Migrate(&db.Session{})
db.SessionCache.Connect(fmt.Sprintf("redis://default:%s@localhost:6379/0", env.CACHE_PASS))
} else {
gormConfig := &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}
db.MainDB.Connect(fmt.Sprintf("root:%s@tcp(main_DB:3306)/main_DB?parseTime=true", env.MAINDB_PASS), gormConfig)
// db.MainDB.Migrate(&models.User{})
db.SessionDB.Connect(fmt.Sprintf("root:%s@tcp(main_DB:3306)/session_DB?parseTime=true", env.MAINDB_PASS), gormConfig)
db.SessionDB.Migrate(&db.Session{})
db.SessionCache.Connect(fmt.Sprintf("redis://default:%s@sessions_cache:6379/0", env.CACHE_PASS))
}
}
func main() {
loadEnv()
loadTemplates()
environment := flag.Bool("dev", false, "environment description")
flag.Parse()
initializeDBs(*environment)
// seed_data.PopulateMainDB()
// Initialize router
if !*environment {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
initRouter(router)
router.Run(":" + env.PORT)
}
func initRouter(router *gin.Engine) {
// Do auth
router.Use(m.GlobeAuth())
router.Use(m.CORSMiddleware()) // TODO: remove in prod
// React apps
router.NoRoute(func(c *gin.Context) {
view := "dashboard"
if len(c.Errors) != 0 {
view = "login"
c.Header("Cache-Control", "no-store")
}
path := c.Request.URL.Path
if path == "/" || path == "" {
path = "/index.html"
}
filePath := fmt.Sprintf("./views/%s%s", view, path)
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
c.File(fmt.Sprintf("./views/%s%s", view, "/index.html"))
c.Abort()
return
}
c.File(fmt.Sprintf("./views/%s%s", view, path))
c.Abort()
})
// API routes
api := router.Group("/api")
secured := api.Group("/").Use(m.APIHeadersMiddleware())
{
// Signup / login
secured.POST("/login", controllers.Login)
secured.GET("/logout", controllers.Logout)
secured.POST("/register", controllers.Register)
api.GET("/activate/:token", controllers.Activate)
// Updating user info
secured.POST("/init-reset", controllers.InitiateReset)
secured.POST("/complete-reset", controllers.CompleteReset)
secured.POST("/deleteAccount", controllers.DeleteAccount)
// Getting user info
secured.GET("/profileInfo", controllers.ProfileInfo)
secured.GET("/allFriends", controllers.AllFriends)
}
}