-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
134 lines (105 loc) · 3.11 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
package main
import (
"fmt"
"food_delivery/common"
"food_delivery/component/appctx"
"food_delivery/component/uploadprovider"
"food_delivery/middleware"
"food_delivery/modules/upload/uploadtransport/ginupload"
"food_delivery/modules/user/usertransport/ginuser"
"food_delivery/pubsub/pblocal"
"food_delivery/routes/restaurantroute"
"food_delivery/skio"
"food_delivery/subscriber"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"net/http"
"os"
)
func main() {
//refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
dsn := os.Getenv("DBConnectionStr")
s3BucketName := os.Getenv("S3BucketName")
s3Region := os.Getenv("S3Region")
s3APIKey := os.Getenv("S3APIKey")
s3SecretKey := os.Getenv("S3SecretKey")
s3Domain := os.Getenv("S3Domain")
secretKey := os.Getenv("SYSTEM_SECRET")
s3Provider := uploadprovider.NewS3Provider(s3BucketName, s3Region, s3APIKey, s3SecretKey, s3Domain)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
fmt.Println(db, err)
db = db.Debug()
if err != nil {
log.Fatalln(err)
}
if err := runService(db, s3Provider, secretKey); err != nil {
log.Fatalln(err)
}
}
func runService(db *gorm.DB, provider uploadprovider.UploadProvider, secretKey string) error {
appCtx := appctx.NewAppContext(db, provider, secretKey, pblocal.NewPubsub())
r := gin.Default()
rtEngine := skio.NewEngine()
if err := rtEngine.Run(appCtx, r); err != nil {
log.Fatalln(err)
}
//deprecated
//subscriber.Setup(appCtx)
// use this line as an alternative for Setup
if err := subscriber.NewEngine(appCtx, rtEngine).Start(); err != nil {
log.Fatalln()
}
r.Use(middleware.Recover(appCtx))
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.StaticFile("/demo/", "./demo.html")
v1 := r.Group("/v1")
v1.POST("/upload", ginupload.Upload(appCtx))
//v1.GET("/presigned-upload-url", func(C *gin.Context) {
// c.JSON(http.StatusOK, gin.H{"data": s3Provider.GetUploadPresignedUrl(c.Request.Context())})
//})
v1.POST("/register", ginuser.Register(appCtx))
v1.POST("/login", ginuser.Login(appCtx))
v1.GET("/profile", middleware.RequireAuth(appCtx), ginuser.GetProfile(appCtx))
v1.GET("/encode-uid", func(c *gin.Context) {
type reqData struct {
DbType int `form:"type"`
RealId int `form:"id"`
}
var d reqData
c.ShouldBind(&d)
c.JSON(http.StatusOK, gin.H{"id": common.NewUID(uint32(d.RealId), d.DbType, 1)})
})
restaurantroute.Routes(v1, appCtx)
admin := v1.Group(
"/admin",
middleware.RequireAuth(appCtx),
middleware.RequireRoles(appCtx, "admin"),
)
{
admin.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, common.SimpleSucessResponse("ok"))
})
}
return r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
//type Permission uint64
//
//const (
// Read Permission = 1 << iota
// Write
// Delete
// Invite
//)
// User 1: Create group FB A (Owner A)
// User 1 invite User 2 into group A (User 2 as a member of A)
// user_id | group_id | permission
// 1 | 1 | 6
//or
// user_id | group_id | permission
// 1 | 1 | read,write,delete,invite