-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (62 loc) · 1.86 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
package main
import (
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
_ "github.com/mattn/go-sqlite3"
"github.com/swaggo/echo-swagger"
"hottub/database"
_ "hottub/docs"
"hottub/handler"
"hottub/types"
)
var h *handler.Handler
var e *echo.Echo
// @title Crowdcontrol Hottub
// @version 1.0
// @description The API that controls authentication and user-management in CrowdControl
// @host hottub.crowdcontrol.network
// @BasePath /
func main() {
db := database.New()
initializeEcho()
database.AutoMigrate(db)
h = handler.NewHandler(db, types.NewUserStore(db))
registerRoutes()
e.Logger.Fatal(e.Start(":1323"))
}
func registerRoutes() {
e.GET("/", h.MainRoute)
e.GET("/users", h.GetUsers)
e.GET("/users/:id", h.GetUsersById)
e.POST("/users", h.CreateUser)
e.PUT("/users/:id", h.UpdateUser)
e.DELETE("/users/:id", h.DeleteUser)
e.POST("/login", h.Login)
e.POST("/register", h.Register)
e.GET("/swagger/*", echoSwagger.WrapHandler)
}
func initializeEcho() {
e = echo.New()
e.Logger.SetLevel(log.DEBUG)
e.Validator = types.NewValidator()
e.Use(middleware.Logger())
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte(handler.Key),
Skipper: func(c echo.Context) bool {
// Skip authentication for and signup login requests
if c.Path() == "/login" || c.Path() == "/register" || c.Path() == "/" || c.Path() == "/swagger/*" {
return true
}
return false
},
}))
e.Use(middleware.Recover())
}