-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (80 loc) · 2.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
package main
import (
"fmt"
"runtime"
"sample/controllers"
"sample/core/authentication"
"sample/settings"
"sample/user_controllers"
"time"
"gopkg.in/mgo.v2"
"github.com/gin-gonic/gin"
)
func main() {
settings.Init()
ConfigRuntime()
StartWorkers()
StartGin()
}
func ConfigRuntime() {
nuCPU := runtime.NumCPU()
runtime.GOMAXPROCS(nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
fmt.Printf("Running with %d1 CPUs\n", nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
fmt.Printf("Running with %d CPUs\n", nuCPU)
}
func StartWorkers() {
go statsWorker()
}
func StartGin() {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
gin.Logger()
router.Use(rateLimit, gin.Recovery())
router.Use(gin.Logger())
router.LoadHTMLGlob("resources/*.templ.html")
router.Static("/static", "resources/static")
router.GET("/", MyBenchLogger(), index)
router.GET("/auth", authentication.RequireTokenAuthentication(), index)
router.POST("/test", controllers.Login)
router.GET("/room/:name", roomGET)
router.POST("/room-post/:roomid", roomPOST)
router.GET("/stream/:roomid", streamRoom)
//mongodb user create
uc := user_controllers.NewUserController(getSession())
router.GET("/user", uc.GetUser)
router.GET("/message", uc.GetMessage)
router.POST("/message", uc.CreateMessage)
router.POST("/user", uc.CreateUser)
router.DELETE("/user/:id", uc.RemoveUser)
router.Run(":5001")
}
func MyBenchLogger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
fmt.Println(start, path)
// Process request
c.Next()
// Stop timer
end := time.Now()
latency := end.Sub(start)
fmt.Println(latency)
}
}
// getSession creates a new mongo session and panics if connection error occurs
func getSession() *mgo.Session {
// Connect to our local mongo
s, err := mgo.Dial("mongodb://localhost")
// Check if connection error, is mongo running?
if err != nil {
panic(err)
}
// Deliver session
return s
}