2023-04-06 10:36:11 +05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ordinary-dev/phoenix/backend"
|
2023-07-22 11:33:14 +05:00
|
|
|
"github.com/ordinary-dev/phoenix/config"
|
2023-04-06 10:36:11 +05:00
|
|
|
"github.com/ordinary-dev/phoenix/views"
|
2023-07-22 11:33:14 +05:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-04-06 10:36:11 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-07-22 11:33:14 +05:00
|
|
|
// Configure logger
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Read config
|
|
|
|
cfg, err := config.GetConfig()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set log level
|
|
|
|
logLevel := cfg.GetLogLevel()
|
|
|
|
logrus.SetLevel(logLevel)
|
|
|
|
logrus.Infof("Setting log level to %v", logLevel)
|
|
|
|
|
|
|
|
// Connect to the database
|
|
|
|
db, err := backend.GetDatabaseConnection(cfg)
|
2023-04-06 10:36:11 +05:00
|
|
|
if err != nil {
|
2023-07-22 11:33:14 +05:00
|
|
|
logrus.Fatalf("%v", err)
|
2023-04-06 10:36:11 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
r.LoadHTMLGlob("templates/*")
|
|
|
|
r.Static("/assets", "./assets")
|
|
|
|
|
2023-07-22 13:42:43 +05:00
|
|
|
r.GET("/signin", func(c *gin.Context) {
|
|
|
|
views.ShowLoginForm(c)
|
|
|
|
})
|
|
|
|
r.POST("/signin", func(c *gin.Context) {
|
|
|
|
views.AuthorizeUser(c, db, cfg)
|
|
|
|
})
|
|
|
|
|
|
|
|
protected := r.Group("/")
|
|
|
|
protected.Use(func(c *gin.Context) {
|
|
|
|
views.AuthMiddleware(c, cfg)
|
|
|
|
})
|
|
|
|
|
2023-04-06 10:37:48 +05:00
|
|
|
// Main page
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.GET("/", func(c *gin.Context) {
|
2023-04-09 11:22:48 +05:00
|
|
|
views.ShowMainPage(c, db)
|
2023-04-06 10:37:48 +05:00
|
|
|
})
|
|
|
|
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.GET("/settings", func(c *gin.Context) {
|
2023-04-09 11:22:48 +05:00
|
|
|
views.ShowSettings(c, db)
|
2023-04-06 10:36:11 +05:00
|
|
|
})
|
|
|
|
|
2023-04-06 10:37:48 +05:00
|
|
|
// Create new user
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/users", func(c *gin.Context) {
|
|
|
|
views.CreateUser(c, db, cfg)
|
2023-04-06 10:36:11 +05:00
|
|
|
})
|
2023-04-06 10:37:48 +05:00
|
|
|
|
|
|
|
// Create new group
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/groups", func(c *gin.Context) {
|
2023-04-09 11:22:48 +05:00
|
|
|
views.CreateGroup(c, db)
|
2023-04-06 10:36:11 +05:00
|
|
|
})
|
2023-04-06 10:37:48 +05:00
|
|
|
|
2023-04-09 11:55:08 +05:00
|
|
|
// Update group
|
|
|
|
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/groups/:id/put", func(c *gin.Context) {
|
2023-04-09 11:55:08 +05:00
|
|
|
views.UpdateGroup(c, db)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Delete group
|
|
|
|
// HTML forms cannot be submitted using the DELETE method without javascript.
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/groups/:id/delete", func(c *gin.Context) {
|
2023-04-09 11:55:08 +05:00
|
|
|
views.DeleteGroup(c, db)
|
|
|
|
})
|
|
|
|
|
2023-04-06 10:37:48 +05:00
|
|
|
// Create new link
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/links", func(c *gin.Context) {
|
2023-04-09 11:22:48 +05:00
|
|
|
views.CreateLink(c, db)
|
2023-04-06 10:36:11 +05:00
|
|
|
})
|
|
|
|
|
2023-04-09 11:22:48 +05:00
|
|
|
// Update link.
|
|
|
|
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/links/:id/put", func(c *gin.Context) {
|
2023-04-09 11:22:48 +05:00
|
|
|
views.UpdateLink(c, db)
|
2023-04-06 10:36:11 +05:00
|
|
|
})
|
|
|
|
|
2023-04-06 10:37:48 +05:00
|
|
|
// Delete link
|
2023-04-09 11:22:48 +05:00
|
|
|
// HTML forms cannot be submitted using the DELETE method without javascript.
|
2023-07-22 13:42:43 +05:00
|
|
|
protected.POST("/links/:id/delete", func(c *gin.Context) {
|
2023-04-09 11:22:48 +05:00
|
|
|
views.DeleteLink(c, db)
|
2023-04-06 10:36:11 +05:00
|
|
|
})
|
|
|
|
|
2023-04-09 12:23:58 +05:00
|
|
|
r.Run(":8080")
|
2023-04-06 10:36:11 +05:00
|
|
|
}
|