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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/ordinary-dev/phoenix/backend"
|
"github.com/ordinary-dev/phoenix/backend"
|
||||||
"github.com/ordinary-dev/phoenix/views"
|
"github.com/ordinary-dev/phoenix/views"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -22,186 +19,42 @@ func main() {
|
||||||
|
|
||||||
// Main page
|
// Main page
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/", func(c *gin.Context) {
|
||||||
if err := views.RequireAuth(c, db); err != nil {
|
views.ShowMainPage(c, db)
|
||||||
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,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Settings
|
|
||||||
r.GET("/settings", func(c *gin.Context) {
|
r.GET("/settings", func(c *gin.Context) {
|
||||||
if err := views.RequireAuth(c, db); err != nil {
|
views.ShowSettings(c, db)
|
||||||
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,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Create new user
|
// Create new user
|
||||||
r.POST("/users", func(c *gin.Context) {
|
r.POST("/users", func(c *gin.Context) {
|
||||||
// If at least 1 administator exists, require authorization
|
views.CreateUser(c, db)
|
||||||
if backend.CountAdmins(db) > 0 {
|
})
|
||||||
tokenValue, err := c.Cookie("phoenix-token")
|
|
||||||
|
|
||||||
// Anonymous visitor
|
r.POST("/signin", func(c *gin.Context) {
|
||||||
if err != nil {
|
views.AuthorizeUser(c, db)
|
||||||
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, "/")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Create new group
|
// Create new group
|
||||||
r.POST("/groups", func(c *gin.Context) {
|
r.POST("/groups", func(c *gin.Context) {
|
||||||
if err := views.RequireAuth(c, db); err != nil {
|
views.CreateGroup(c, db)
|
||||||
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")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Create new link
|
// Create new link
|
||||||
r.POST("/links", func(c *gin.Context) {
|
r.POST("/links", func(c *gin.Context) {
|
||||||
if err := views.RequireAuth(c, db); err != nil {
|
views.CreateLink(c, db)
|
||||||
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")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// 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) {
|
r.POST("/links/:id/put", func(c *gin.Context) {
|
||||||
if err := views.RequireAuth(c, db); err != nil {
|
views.UpdateLink(c, db)
|
||||||
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")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Delete link
|
// Delete link
|
||||||
|
// HTML forms cannot be submitted using the DELETE method without javascript.
|
||||||
r.POST("/links/:id/delete", func(c *gin.Context) {
|
r.POST("/links/:id/delete", func(c *gin.Context) {
|
||||||
if err := views.RequireAuth(c, db); err != nil {
|
views.DeleteLink(c, db)
|
||||||
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, "/")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run()
|
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