-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
110 lines (92 loc) · 3.16 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
package main
import (
"context"
"fmt"
"gcs/cont"
"gcs/handler"
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"github.com/gin-gonic/gin"
)
var authHandler *handler.AuthHandler
var dashHandler *handler.DashHandler
var userHandler *handler.UserHandler
var appHandler *handler.AppHandler
func init() {
ctx := context.Background()
// config
viper.SetConfigFile("config.json")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
log.Println("Prepare connected to MongoDB: " + viper.GetString("mongo.uri") + ", " + viper.GetString("mongo.db"))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(viper.GetString("mongo.uri")))
if err = client.Ping(context.TODO(), readpref.Primary()); err != nil {
log.Fatal(err)
}
log.Println("Connected to MongoDB: " + viper.GetString("mongo.uri") + ", " + viper.GetString("mongo.db"))
mongodb := client.Database(viper.GetString("mongo.db"))
authHandler = handler.NewAuthHandler(ctx, mongodb)
dashHandler = handler.NewDashHandler(ctx)
userHandler = handler.NewUserHandler(ctx, mongodb)
appHandler = handler.NewAppHandler(ctx, mongodb)
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/**.html")
router.Static("/assets", "./assets")
router.StaticFile("/favicon.ico", "./assets/favicon.ico")
// session
store := cookie.NewStore([]byte(viper.GetString("secret.session")))
router.Use(sessions.Sessions(viper.GetString("secret.cookie.name"), store))
adminAuthMiddleware := handler.AuthMiddleware(cont.Admin.String(), cont.Owner.String(), cont.Maintainer.String(), cont.Developer.String())
v1 := router.Group(fmt.Sprintf("/%s", viper.Get("server.base")))
{
// ping
v1.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
webRouter := v1.Group("/r/w")
{
webRouter.GET("/#", authHandler.ShowLogin)
webRouter.GET("/dash", adminAuthMiddleware, dashHandler.ShowDash)
webRouter.GET("/apps", handler.AuthMiddleware(), appHandler.ShowApps)
webRouter.GET("/app/:id", handler.AuthMiddleware(), appHandler.ShowApp)
}
// json router
jsonRouter := v1.Group("/r/jd")
{
jsonRouter.POST("/#", authHandler.Login)
jsonRouter.GET("/logout", authHandler.Logout)
jsonRouter.GET("/me", handler.AuthMiddleware(), userHandler.Me)
userRouter := jsonRouter.Group("/user")
userRouter.Use(handler.AuthMiddleware(cont.Admin.String()))
{
userRouter.GET("/", userHandler.Users)
userRouter.POST("/", userHandler.NewUser)
userRouter.DELETE("/:id", userHandler.DeleteUser)
userRouter.PUT("/:id", userHandler.UpdateUser)
}
appRouter := jsonRouter.Group("/app")
appRouter.Use(adminAuthMiddleware)
{
appRouter.GET("/", appHandler.Apps)
appRouter.GET("/:id", appHandler.AppInfo)
appRouter.POST("/", appHandler.NewApp)
appRouter.DELETE("/:id", appHandler.DeleteApp)
appRouter.PUT("/:id", appHandler.UpdateApp)
}
}
}
router.Run(fmt.Sprintf(":%d", viper.GetInt("server.port")))
}