From 227028f99de219581d2196916d5dea47859613d7 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Mon, 19 Dec 2016 18:56:24 +0100 Subject: [PATCH] Try to extract http --- cmd/budgeteer/main.go | 38 ++++++-------------------------- http/http.go | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 http/http.go diff --git a/cmd/budgeteer/main.go b/cmd/budgeteer/main.go index 3f7d80c..67e5f86 100644 --- a/cmd/budgeteer/main.go +++ b/cmd/budgeteer/main.go @@ -3,42 +3,16 @@ package main import ( "net/http" - "gopkg.in/gin-gonic/gin.v1" + "github.com/gin-gonic/gin" + "git.javil.eu/jacob1123/budgeteer/http" + "git.javil.eu/jacob1123/budgeteer/postgres" ) func main() { - router := gin.Default() + us := &postgres.UserService{} - router.LoadHTMLGlob("./templates/*") - router.Static("/static", "./static") - - router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index", nil) }) - router.GET("/login", login) - api := router.Group("/api/v1") - { - api.GET("/logout", logout) - api.GET("/login", func(c *gin.Context) { - c.Redirect(http.StatusPermanentRedirect, "/login") - }) - api.POST("/login", loginPost) - - // Unauthenticated routes - api.GET("/check", func(c *gin.Context) { - c.String(http.StatusOK, "Accessible") - }) - api.GET("/hello", func(c *gin.Context) { - c.String(http.StatusOK, "Hello, World!") - }) - - // Restricted group - r := api.Group("/restricted") - { - //r.Use(middleware.JWT([]byte(secret))) - r.GET("", restricted) - } - } - - router.Run(":1323") + h := &http.Handler{UserService=us} + h.Serve() } func restricted(c *gin.Context) { diff --git a/http/http.go b/http/http.go new file mode 100644 index 0000000..822bc42 --- /dev/null +++ b/http/http.go @@ -0,0 +1,50 @@ +package http + +import ( + "net/http" + + "git.javil.eu/jacob1123/budgeteer" + + "gopkg.in/gin-gonic/gin.v1" +) + +// Handler handles incoming requests +type Handler struct { + UserService budgeteer.UserService +} + +func (h *Handler) Serve() { + + router := gin.Default() + + router.LoadHTMLGlob("./templates/*") + router.Static("/static", "./static") + + router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index", nil) }) + router.GET("/login", login) + api := router.Group("/api/v1") + { + api.GET("/logout", logout) + api.GET("/login", func(c *gin.Context) { + c.Redirect(http.StatusPermanentRedirect, "/login") + }) + api.POST("/login", loginPost) + + // Unauthenticated routes + api.GET("/check", func(c *gin.Context) { + c.String(http.StatusOK, "Accessible") + }) + api.GET("/hello", func(c *gin.Context) { + c.String(http.StatusOK, "Hello, World!") + }) + + // Restricted group + r := api.Group("/restricted") + { + //r.Use(middleware.JWT([]byte(secret))) + r.GET("", restricted) + } + } + + router.Run(":1323") +}