mirror of
https://github.com/ordinary-dev/phoenix
synced 2024-09-20 03:40:29 +05:00
Move pages into separate modules
This commit is contained in:
parent
ad0b4b8d9c
commit
bf4c52e68b
173
main.go
173
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()
|
||||
|
|
24
views/groups.go
Normal file
24
views/groups.go
Normal file
|
@ -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")
|
||||
}
|
25
views/index.go
Normal file
25
views/index.go
Normal file
|
@ -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,
|
||||
})
|
||||
}
|
73
views/links.go
Normal file
73
views/links.go
Normal file
|
@ -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")
|
||||
}
|
25
views/settings.go
Normal file
25
views/settings.go
Normal file
|
@ -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,
|
||||
})
|
||||
}
|
72
views/users.go
Normal file
72
views/users.go
Normal file
|
@ -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, "/")
|
||||
}
|
Loading…
Reference in a new issue