forked from jritsema/go-htmx-tailwind-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (73 loc) · 2 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
package main
import (
"context"
"github.com/ValeryVerkhoturov/chat/auth"
"github.com/ValeryVerkhoturov/chat/config"
"github.com/ValeryVerkhoturov/chat/db"
"github.com/ValeryVerkhoturov/chat/handlers"
v1Handlers "github.com/ValeryVerkhoturov/chat/handlers/v1"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/graceful"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
func port() string {
port := config.Port
if len(port) == 0 {
port = "8080"
}
return ":" + port
}
func engine() *graceful.Graceful {
router, err := graceful.Default()
if err != nil {
panic(err)
}
router.Use(cors.Default()) // AllowAllOrigins true
router.Use(gzip.Gzip(gzip.DefaultCompression))
router.Use(sessions.Sessions("chat-session", cookie.NewStore([]byte(config.SessionSecret))))
router.Static("/images/", "./public/images")
router.StaticFile("/css/output-css", "./public/css/output.css")
router.LoadHTMLGlob("templates/templates/*")
router.GET("/", handlers.Index)
router.GET("/index.html", handlers.Index)
v1 := router.Group("/v1")
v1.Use(auth.CreateSessionIfNotExists)
{
v1.GET("/chat-widget", v1Handlers.ChatWidget)
}
v1.Use(auth.SessionRequired)
{
v1.GET("/chat-container", v1Handlers.ChatContainer)
}
return router
}
func main() {
var err error
// Log init
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
// Graceful termination when shutting down a process init
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// MongoDB connect
mongoCtx, cancel := db.InitDB()
defer cancel()
defer func() {
if err = db.MongoClient.Disconnect(mongoCtx); err != nil {
panic(err)
}
}()
// Gin init
log.Info("Starting Server on http://localhost" + port())
router := engine()
defer router.Close()
if err = router.RunWithContext(ctx); err != nil && err != context.Canceled {
log.Fatal("Unable to start:", err)
}
}