phoenix/main.go

195 lines
3.9 KiB
Go
Raw Normal View History

2023-04-06 10:36:11 +05:00
package main
import (
2023-04-06 10:37:48 +05:00
"errors"
2023-04-06 10:36:11 +05:00
"github.com/gin-gonic/gin"
"github.com/ordinary-dev/phoenix/backend"
"github.com/ordinary-dev/phoenix/views"
"log"
"net/http"
2023-04-06 10:37:48 +05:00
"strconv"
2023-04-06 10:36:11 +05:00
)
func main() {
db, err := backend.GetDatabaseConnection()
if err != nil {
log.Fatal(err)
}
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./assets")
2023-04-06 10:37:48 +05:00
// Main page
2023-04-06 10:36:11 +05:00
r.GET("/", func(c *gin.Context) {
views.RequireAuth(c, db)
2023-04-06 10:37:48 +05:00
groups, err := backend.GetGroups(db)
if err != nil {
views.ShowError(c, err)
return
}
2023-04-06 10:36:11 +05:00
c.HTML(http.StatusOK, "index.html.tmpl", gin.H{
2023-04-06 10:37:48 +05:00
"groups": groups,
})
})
// Settings
r.GET("/settings", func(c *gin.Context) {
views.RequireAuth(c, db)
groups, err := backend.GetGroups(db)
if err != nil {
views.ShowError(c, err)
return
}
c.HTML(http.StatusOK, "settings.html.tmpl", gin.H{
"groups": groups,
})
2023-04-06 10:36:11 +05:00
})
2023-04-06 10:37:48 +05:00
// Create new user
2023-04-06 10:36:11 +05:00
r.POST("/users", func(c *gin.Context) {
2023-04-06 10:37:48 +05:00
// 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")
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.
2023-04-06 10:36:11 +05:00
username := c.PostForm("username")
password := c.PostForm("password")
admin, err := backend.CreateAdmin(db, username, password)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
2023-04-06 10:37:48 +05:00
// Generate access token.
2023-04-06 10:36:11 +05:00
token, err := backend.CreateAccessToken(db, admin.ID)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
backend.SetTokenCookie(c, token)
2023-04-06 10:37:48 +05:00
// Redirect to homepage.
2023-04-06 10:36:11 +05:00
c.Redirect(http.StatusFound, "/")
})
2023-04-06 10:37:48 +05:00
// Create new group
2023-04-06 10:36:11 +05:00
r.POST("/groups", func(c *gin.Context) {
2023-04-06 10:37:48 +05:00
views.RequireAuth(c, db)
2023-04-06 10:36:11 +05:00
groupName := c.PostForm("groupName")
_, err := backend.CreateGroup(db, groupName)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
2023-04-06 10:37:48 +05:00
// Redirect to settings.
2023-04-06 10:36:11 +05:00
c.Redirect(http.StatusFound, "/settings")
})
2023-04-06 10:37:48 +05:00
// Create new link
2023-04-06 10:36:11 +05:00
r.POST("/links", func(c *gin.Context) {
2023-04-06 10:37:48 +05:00
views.RequireAuth(c, db)
2023-04-06 10:36:11 +05:00
linkName := c.PostForm("linkName")
2023-04-06 10:37:48 +05:00
href := c.PostForm("href")
groupID, err := strconv.ParseUint(c.PostForm("groupID"), 10, 32)
if err != nil {
views.ShowError(c, err)
return
}
2023-04-06 10:36:11 +05:00
_, err = backend.CreateLink(db, linkName, href, groupID)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
2023-04-06 10:37:48 +05:00
// Redirect to settings.
2023-04-06 10:36:11 +05:00
c.Redirect(http.StatusFound, "/settings")
})
2023-04-06 10:37:48 +05:00
// Update link
r.POST("/links/:id/put", func(c *gin.Context) {
views.RequireAuth(c, db)
2023-04-06 10:36:11 +05:00
2023-04-06 10:37:48 +05:00
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
views.ShowError(c, err)
return
}
2023-04-06 10:36:11 +05:00
linkName := c.PostForm("linkName")
2023-04-06 10:37:48 +05:00
href := c.PostForm("href")
2023-04-06 10:36:11 +05:00
_, err = backend.UpdateLink(db, id, linkName, href)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
2023-04-06 10:37:48 +05:00
// Redirect to settings.
2023-04-06 10:36:11 +05:00
c.Redirect(http.StatusFound, "/settings")
})
2023-04-06 10:37:48 +05:00
// Delete link
r.POST("/links/:id/delete", func(c *gin.Context) {
views.RequireAuth(c, db)
2023-04-06 10:36:11 +05:00
2023-04-06 10:37:48 +05:00
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
views.ShowError(c, err)
return
}
2023-04-06 10:36:11 +05:00
err = backend.DeleteLink(db, id)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
2023-04-06 10:37:48 +05:00
// Redirect to settings.
2023-04-06 10:36:11 +05:00
c.Redirect(http.StatusFound, "/settings")
})
r.POST("/signin", func(c *gin.Context) {
2023-04-06 10:37:48 +05:00
// Check credentials.
2023-04-06 10:36:11 +05:00
username := c.PostForm("username")
password := c.PostForm("password")
admin, err := backend.AuthorizeAdmin(db, username, password)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
2023-04-06 10:37:48 +05:00
// Generate an access token.
2023-04-06 10:36:11 +05:00
token, err := backend.CreateAccessToken(db, admin.ID)
if err != nil {
views.ShowError(c, err)
2023-04-06 10:37:48 +05:00
return
2023-04-06 10:36:11 +05:00
}
backend.SetTokenCookie(c, token)
2023-04-06 10:37:48 +05:00
// Redirect to homepage.
2023-04-06 10:36:11 +05:00
c.Redirect(http.StatusFound, "/")
})
r.Run()
}