-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (71 loc) · 2.24 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
package main
import (
"golang-gin-poc/controllers"
"golang-gin-poc/middlewares"
"golang-gin-poc/repositories"
"golang-gin-poc/services"
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
var (
videoRepository repositories.VideoRepository = repositories.NewVideoRepository()
videoService services.VideoService = services.NewVideoService(videoRepository)
loginService services.LoginService = services.NewLoginService()
videoController controllers.VideoController = controllers.New(videoService)
jwtService services.JWTService = services.NewJWTService()
loginController controllers.LoginController = controllers.NewLoginController(loginService, jwtService)
)
func setupLogOutput() {
f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}
func main() {
setupLogOutput()
server := gin.New()
// server.Use(gin.Recovery(), middlewares.Logger(), middlewares.BasicAuth(), gindump.Dump())
server.Use(gin.Recovery(), gin.Logger())
// Login Endpoint: Authentication + Token creation
server.POST("/login", func(ctx *gin.Context) {
token := loginController.Login(ctx)
if token != "" {
ctx.JSON(http.StatusCreated, gin.H{
"token": token,
})
} else {
ctx.JSON(http.StatusUnauthorized, nil)
}
})
apiRoutes := server.Group("/api", middlewares.AuthorizeJWT())
{
apiRoutes.GET("/videos", func(ctx *gin.Context) {
ctx.JSON(200, videoController.FindAll())
})
apiRoutes.POST("/videos", func(ctx *gin.Context) {
err := videoController.Create(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
ctx.JSON(http.StatusCreated, gin.H{"message": "Video created"})
}
})
apiRoutes.PUT("/videos/:id", func(ctx *gin.Context) {
err := videoController.Update(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
ctx.JSON(http.StatusNoContent, gin.H{"message": "Video updated"})
}
})
apiRoutes.DELETE("/videos/:id", func(ctx *gin.Context) {
err := videoController.Delete(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
ctx.JSON(http.StatusNoContent, gin.H{"message": "Video deleted"})
}
})
}
server.Run(":8080")
}