-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
139 lines (116 loc) · 4.35 KB
/
server.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
"gitlab.com/voideus/go-rest-api/controller"
"gitlab.com/voideus/go-rest-api/middlewares"
"gitlab.com/voideus/go-rest-api/repository"
"gitlab.com/voideus/go-rest-api/service"
)
var (
videoRepository repository.VideoRepository = repository.NewVideoRepository()
articleRepository repository.ArticleRepository = repository.NewArticleRepository()
creditCardRepository repository.CreditCardRepository = repository.NewCreditCardRepo()
personCardRepository repository.PersonRepo = repository.NewPersonRepo()
videoService service.VideoService = service.New(videoRepository)
articleService service.ArticleService = service.NewArticleRepoService(articleRepository)
creditCardService service.CreditCardService = service.NewCreditCardService(creditCardRepository)
personService service.PersonService = service.NewPersonService(personCardRepository)
loginService service.LoginService = service.NewLoginService()
jwtService service.JWTService = service.NewJWTService()
videoController controller.VideoController = controller.New(videoService)
articleController controller.ArticleController = controller.NewArticleController(articleService)
creditCardController controller.CreditCardController = controller.NewCreditCardController(creditCardService)
personController controller.PersonController = controller.NewPersonController(personService)
loginController controller.LoginController = controller.NewLoginController(loginService, jwtService)
)
func setLogOutput() {
f, err := os.Create("gin.log")
fmt.Println(err)
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}
func main() {
defer videoRepository.CloseDB()
setLogOutput()
server := gin.New()
// Serve static resources
server.Static("/css", "./templates/css")
server.LoadHTMLGlob("templates/*.html")
// Middlewares
server.Use(gin.Recovery(), middlewares.Logger())
server.GET("/test", func(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"message": "OK!",
})
})
// Login: Authentication + Token Creation
server.POST("/login", func(ctx *gin.Context) {
token := loginController.Login(ctx)
if token != "" {
ctx.JSON(http.StatusOK, gin.H{
"token": token,
})
} else {
ctx.JSON(http.StatusUnauthorized, nil)
}
})
// apiRoutes := server.Group("/api", middlewares.AuthorizeJWT())
apiRoutes := server.Group("/api")
{
apiRoutes.GET("/videos", func(ctx *gin.Context) {
ctx.JSON(200, videoController.FindAll())
})
apiRoutes.POST("/videos", func(ctx *gin.Context) {
err := videoController.Save(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
ctx.JSON(http.StatusOK, gin.H{"message": "Video Input is Valid"})
}
})
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.StatusOK, gin.H{"message": "Video updated successfully"})
}
})
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.StatusOK, gin.H{"message": "Video deleted successfully"})
}
})
apiRoutes.POST("/articles", func(ctx *gin.Context) {
err := articleController.Save(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
ctx.JSON(http.StatusOK, gin.H{"message": "Article added success."})
}
})
apiRoutes.GET("/articles", func(ctx *gin.Context) {
ctx.JSON(200, articleController.FindAll())
})
apiRoutes.GET("/articles/:id", articleController.FindArticle)
apiRoutes.POST("/articles/:id/comments", articleController.AddCommentToArticle)
apiRoutes.GET("/credit-cards", creditCardController.FindAll)
apiRoutes.POST("/credit-cards", creditCardController.Save)
apiRoutes.GET("/people", personController.FindAll)
apiRoutes.GET("/people/:id/languages", personController.GetLanguagesOfPerson)
apiRoutes.POST("/people/:id/languages", personController.AddLanguagesToPerson)
}
viewRoutes := server.Group("/view")
{
viewRoutes.GET("/videos", func(ctx *gin.Context) {
videoController.ShowAll(ctx)
})
}
server.Run()
}