Skip to content

Commit

Permalink
code clenup
Browse files Browse the repository at this point in the history
  • Loading branch information
alnck committed Feb 14, 2022
1 parent 2a002a3 commit 7c1136f
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package main
import (
"blog-on-containers/handler"
"blog-on-containers/middleware"
"blog-on-containers/models"
"blog-on-containers/templates"
"net/http"

"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
Expand All @@ -26,25 +24,30 @@ func main() {
r = gin.New()
r.Use(gin.Logger())

r.LoadHTMLGlob("templates/views/*.html")
r.Static("/css", "./static/css")

r.GET("/loginpage", templates.LoginPage)
r.POST("/registerpage", templates.RegisterPage)

r.POST("/login", handler.LoginHandler)
r.POST("/user", handler.CreateUser)

api := r.Group("/api")

validateTokenHandle := middleware.ValidateToken()
authHandle := middleware.Authorization([]int{1})

initTemplatesRouteMap(r)
initBlogRouteMap(api, validateTokenHandle, authHandle)
initUserRouteMap(api)

r.Run(":5000")
}

func initTemplatesRouteMap(route *gin.Engine) {
route.Use(gin.Logger())

route.LoadHTMLGlob("templates/views/*.html")
route.Static("/css", "./static/css")

route.GET("/loginpage", templates.LoginPage)
route.GET("/registerpage", templates.RegisterPage)
}

func initBlogRouteMap(route *gin.RouterGroup, validateTokenHandle, authHandle gin.HandlerFunc) {
blog := route.Group("/blog")
blog.Use(validateTokenHandle, authHandle)
Expand All @@ -57,17 +60,3 @@ func initBlogRouteMap(route *gin.RouterGroup, validateTokenHandle, authHandle gi

blogWithoutAuth.GET("/", handler.GetStories)
}

func initUserRouteMap(route *gin.RouterGroup) {
user := route.Group("/user")

user.GET("/", func(context *gin.Context) {
context.AbortWithStatusJSON(http.StatusOK, models.Response{
Data: "ok",
Status: http.StatusOK,
Message: "tes",
})
})

user.POST("/", handler.CreateUser)
}

0 comments on commit 7c1136f

Please sign in to comment.