-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (44 loc) · 1.19 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
package main
import (
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
"gitlab.com/pragmaticreviews/golang-gin-poc/controller"
"gitlab.com/pragmaticreviews/golang-gin-poc/middlewares"
"gitlab.com/pragmaticreviews/golang-gin-poc/service"
)
var (
videoService service.VideoService = service.New()
videoController controller.VideoController = controller.New(videoService)
)
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())
server.Static("/css", "./templates/css")
server.LoadHTMLGlob("templates/*.html")
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!!"})
}
})
}
viewRoutes := server.Group("/view")
{
viewRoutes.GET("/videos", videoController.ShowAll)
}
server.Run(":8080")
}