-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (51 loc) · 1.37 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
package main
import (
"log"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/littlegiftz/todo-app-go/controller"
"github.com/littlegiftz/todo-app-go/db"
"github.com/littlegiftz/todo-app-go/model"
)
func main() {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error getting env, %v", err)
}
// Initilize database
db.Init()
jwtMiddleware := middleware.JWTWithConfig(middleware.JWTConfig{
Claims: &model.CustomClaims{},
SigningKey: []byte(os.Getenv("JWT_SECRET")),
})
// Create routes
e := echo.New()
// Middleware
e.HTTPErrorHandler = customHTTPErrorHandler
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.POST("/login", controller.Login)
e.POST("/register", controller.CreateUser)
u := e.Group("/user")
u.Use(jwtMiddleware)
u.POST("/change-password", controller.SavePassword)
t := e.Group("/todo")
t.Use(jwtMiddleware)
t.GET("/", controller.GetTasks)
t.POST("/", controller.AddTask)
t.POST("/:id", controller.SaveTask)
t.DELETE("/:id", controller.DeleteTask)
e.Logger.Fatal(e.Start(":1323"))
}
func customHTTPErrorHandler(err error, c echo.Context) {
if err == middleware.ErrJWTMissing {
c.Error(echo.NewHTTPError(http.StatusUnauthorized, "Login required"))
return
}
c.Echo().DefaultHTTPErrorHandler(err, c)
}