From bf4c52e68bae8a5976fdf19dbf3de2140e49e9f8 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Sun, 9 Apr 2023 11:22:48 +0500 Subject: [PATCH] Move pages into separate modules --- main.go | 173 ++++------------------------------------------ views/groups.go | 24 +++++++ views/index.go | 25 +++++++ views/links.go | 73 +++++++++++++++++++ views/settings.go | 25 +++++++ views/users.go | 72 +++++++++++++++++++ 6 files changed, 232 insertions(+), 160 deletions(-) create mode 100644 views/groups.go create mode 100644 views/index.go create mode 100644 views/links.go create mode 100644 views/settings.go create mode 100644 views/users.go diff --git a/main.go b/main.go index fc4908b..1dbfc6a 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,10 @@ package main import ( - "errors" "github.com/gin-gonic/gin" "github.com/ordinary-dev/phoenix/backend" "github.com/ordinary-dev/phoenix/views" "log" - "net/http" - "strconv" ) func main() { @@ -22,186 +19,42 @@ func main() { // Main page r.GET("/", func(c *gin.Context) { - if err := views.RequireAuth(c, db); err != nil { - return - } - - groups, err := backend.GetGroups(db) - if err != nil { - views.ShowError(c, err) - return - } - c.HTML(http.StatusOK, "index.html.tmpl", gin.H{ - "groups": groups, - }) + views.ShowMainPage(c, db) }) - // Settings r.GET("/settings", func(c *gin.Context) { - if err := views.RequireAuth(c, db); err != nil { - return - } - - groups, err := backend.GetGroups(db) - if err != nil { - views.ShowError(c, err) - return - } - - c.HTML(http.StatusOK, "settings.html.tmpl", gin.H{ - "groups": groups, - }) + views.ShowSettings(c, db) }) // Create new user r.POST("/users", func(c *gin.Context) { - // If at least 1 administator exists, require authorization - if backend.CountAdmins(db) > 0 { - tokenValue, err := c.Cookie("phoenix-token") + views.CreateUser(c, db) + }) - // Anonymous visitor - if err != nil { - err = errors.New("At least 1 user exists, you have to sign in first") - views.ShowError(c, err) - return - } - - err = backend.ValidateToken(db, tokenValue) - if err != nil { - views.ShowError(c, err) - return - } - } - - // User is authorized or no user exists. - // Try to create a user. - username := c.PostForm("username") - password := c.PostForm("password") - admin, err := backend.CreateAdmin(db, username, password) - if err != nil { - views.ShowError(c, err) - return - } - - // Generate access token. - token, err := backend.CreateAccessToken(db, admin.ID) - if err != nil { - views.ShowError(c, err) - return - } - backend.SetTokenCookie(c, token) - - // Redirect to homepage. - c.Redirect(http.StatusFound, "/") + r.POST("/signin", func(c *gin.Context) { + views.AuthorizeUser(c, db) }) // Create new group r.POST("/groups", func(c *gin.Context) { - if err := views.RequireAuth(c, db); err != nil { - return - } - - groupName := c.PostForm("groupName") - _, err := backend.CreateGroup(db, groupName) - if err != nil { - views.ShowError(c, err) - return - } - - // Redirect to settings. - c.Redirect(http.StatusFound, "/settings") + views.CreateGroup(c, db) }) // Create new link r.POST("/links", func(c *gin.Context) { - if err := views.RequireAuth(c, db); err != nil { - return - } - - linkName := c.PostForm("linkName") - href := c.PostForm("href") - groupID, err := strconv.ParseUint(c.PostForm("groupID"), 10, 32) - if err != nil { - views.ShowError(c, err) - return - } - - _, err = backend.CreateLink(db, linkName, href, groupID) - if err != nil { - views.ShowError(c, err) - return - } - - // Redirect to settings. - c.Redirect(http.StatusFound, "/settings") + views.CreateLink(c, db) }) - // Update link + // Update link. + // HTML forms cannot be submitted using PUT or PATCH methods without javascript. r.POST("/links/:id/put", func(c *gin.Context) { - if err := views.RequireAuth(c, db); err != nil { - return - } - - id, err := strconv.ParseUint(c.Param("id"), 10, 64) - if err != nil { - views.ShowError(c, err) - return - } - linkName := c.PostForm("linkName") - href := c.PostForm("href") - - _, err = backend.UpdateLink(db, id, linkName, href) - if err != nil { - views.ShowError(c, err) - return - } - - // Redirect to settings. - c.Redirect(http.StatusFound, "/settings") + views.UpdateLink(c, db) }) // Delete link + // HTML forms cannot be submitted using the DELETE method without javascript. r.POST("/links/:id/delete", func(c *gin.Context) { - if err := views.RequireAuth(c, db); err != nil { - return - } - - id, err := strconv.ParseUint(c.Param("id"), 10, 64) - if err != nil { - views.ShowError(c, err) - return - } - - err = backend.DeleteLink(db, id) - if err != nil { - views.ShowError(c, err) - return - } - - // Redirect to settings. - c.Redirect(http.StatusFound, "/settings") - }) - - r.POST("/signin", func(c *gin.Context) { - // Check credentials. - username := c.PostForm("username") - password := c.PostForm("password") - admin, err := backend.AuthorizeAdmin(db, username, password) - if err != nil { - views.ShowError(c, err) - return - } - - // Generate an access token. - token, err := backend.CreateAccessToken(db, admin.ID) - if err != nil { - views.ShowError(c, err) - return - } - backend.SetTokenCookie(c, token) - - // Redirect to homepage. - c.Redirect(http.StatusFound, "/") + views.DeleteLink(c, db) }) r.Run() diff --git a/views/groups.go b/views/groups.go new file mode 100644 index 0000000..9e627bf --- /dev/null +++ b/views/groups.go @@ -0,0 +1,24 @@ +package views + +import ( + "github.com/gin-gonic/gin" + "github.com/ordinary-dev/phoenix/backend" + "gorm.io/gorm" + "net/http" +) + +func CreateGroup(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + // Save new group to the database. + groupName := c.PostForm("groupName") + if _, err := backend.CreateGroup(db, groupName); err != nil { + ShowError(c, err) + return + } + + // This page is called from the settings, return the user back. + c.Redirect(http.StatusFound, "/settings") +} diff --git a/views/index.go b/views/index.go new file mode 100644 index 0000000..ac81923 --- /dev/null +++ b/views/index.go @@ -0,0 +1,25 @@ +package views + +import ( + "github.com/gin-gonic/gin" + "github.com/ordinary-dev/phoenix/backend" + "gorm.io/gorm" + "net/http" +) + +func ShowMainPage(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + // Get a list of groups with links + groups, err := backend.GetGroups(db) + if err != nil { + ShowError(c, err) + return + } + + c.HTML(http.StatusOK, "index.html.tmpl", gin.H{ + "groups": groups, + }) +} diff --git a/views/links.go b/views/links.go new file mode 100644 index 0000000..ad71408 --- /dev/null +++ b/views/links.go @@ -0,0 +1,73 @@ +package views + +import ( + "github.com/gin-gonic/gin" + "github.com/ordinary-dev/phoenix/backend" + "gorm.io/gorm" + "net/http" + "strconv" +) + +func CreateLink(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + linkName := c.PostForm("linkName") + href := c.PostForm("href") + groupID, err := strconv.ParseUint(c.PostForm("groupID"), 10, 32) + if err != nil { + ShowError(c, err) + return + } + + if _, err = backend.CreateLink(db, linkName, href, groupID); err != nil { + ShowError(c, err) + return + } + + // Redirect to settings. + c.Redirect(http.StatusFound, "/settings") +} + +func UpdateLink(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + id, err := strconv.ParseUint(c.Param("id"), 10, 64) + if err != nil { + ShowError(c, err) + return + } + linkName := c.PostForm("linkName") + href := c.PostForm("href") + + if _, err = backend.UpdateLink(db, id, linkName, href); err != nil { + ShowError(c, err) + return + } + + // Redirect to settings. + c.Redirect(http.StatusFound, "/settings") +} + +func DeleteLink(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + id, err := strconv.ParseUint(c.Param("id"), 10, 64) + if err != nil { + ShowError(c, err) + return + } + + if err = backend.DeleteLink(db, id); err != nil { + ShowError(c, err) + return + } + + // Redirect to settings. + c.Redirect(http.StatusFound, "/settings") +} diff --git a/views/settings.go b/views/settings.go new file mode 100644 index 0000000..0f7cf1f --- /dev/null +++ b/views/settings.go @@ -0,0 +1,25 @@ +package views + +import ( + "github.com/gin-gonic/gin" + "github.com/ordinary-dev/phoenix/backend" + "gorm.io/gorm" + "net/http" +) + +func ShowSettings(c *gin.Context, db *gorm.DB) { + if err := RequireAuth(c, db); err != nil { + return + } + + // Get a list of groups with links + groups, err := backend.GetGroups(db) + if err != nil { + ShowError(c, err) + return + } + + c.HTML(http.StatusOK, "settings.html.tmpl", gin.H{ + "groups": groups, + }) +} diff --git a/views/users.go b/views/users.go new file mode 100644 index 0000000..954572c --- /dev/null +++ b/views/users.go @@ -0,0 +1,72 @@ +package views + +import ( + "errors" + "github.com/gin-gonic/gin" + "github.com/ordinary-dev/phoenix/backend" + "gorm.io/gorm" + "net/http" +) + +func CreateUser(c *gin.Context, db *gorm.DB) { + // If at least 1 administator exists, require authorization + if backend.CountAdmins(db) > 0 { + tokenValue, err := c.Cookie("phoenix-token") + + // Anonymous visitor + if err != nil { + err = errors.New("At least 1 user exists, you have to sign in first") + ShowError(c, err) + return + } + + err = backend.ValidateToken(db, tokenValue) + if err != nil { + ShowError(c, err) + return + } + } + + // User is authorized or no user exists. + // Try to create a user. + username := c.PostForm("username") + password := c.PostForm("password") + admin, err := backend.CreateAdmin(db, username, password) + if err != nil { + ShowError(c, err) + return + } + + // Generate access token. + token, err := backend.CreateAccessToken(db, admin.ID) + if err != nil { + ShowError(c, err) + return + } + backend.SetTokenCookie(c, token) + + // Redirect to homepage. + c.Redirect(http.StatusFound, "/") +} + +func AuthorizeUser(c *gin.Context, db *gorm.DB) { + // Check credentials. + username := c.PostForm("username") + password := c.PostForm("password") + admin, err := backend.AuthorizeAdmin(db, username, password) + if err != nil { + ShowError(c, err) + return + } + + // Generate an access token. + token, err := backend.CreateAccessToken(db, admin.ID) + if err != nil { + ShowError(c, err) + return + } + backend.SetTokenCookie(c, token) + + // Redirect to homepage. + c.Redirect(http.StatusFound, "/") +}