package webpage import ( "app/database" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" "net/http" "time" ) func (wp *Webpage) defineRoutes() { wp.Router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", gin.H{ "title": "Register", }) }) wp.Router.GET("/login", func(c *gin.Context) { c.HTML(http.StatusOK, "login.tmpl", gin.H{ "title": "Login", }) }) wp.Router.GET("/register", func(c *gin.Context) { c.HTML(http.StatusOK, "register.tmpl", gin.H{ "title": "Register", }) }) wp.Router.GET("/health", func(c *gin.Context) { currentTime := time.Now().UnixMilli() c.JSON(http.StatusOK, gin.H{ "timestamp": currentTime, }) }) wp.Router.NoRoute(func(c *gin.Context) { c.Redirect(http.StatusTemporaryRedirect, "/") }) wp.Router.POST("/register", func(c *gin.Context) { username, err := c.GetPostForm("username") password, err := c.GetPostForm("password") if err == false { c.JSON(400, gin.H{"message": "bad post form"}) return } user := database.User{Username: username, Password: password} result := wp.Database.ORM.Create(&user) if result.Error != nil { c.JSON(200, gin.H{"message": "cannot create user"}) } c.JSON(200, gin.H{"message": "user registered"}) }) } func (wp *Webpage) initialize() { wp.Database = database.Database{Location: "sqlite.db"} wp.Database.Initialize() } func (wp *Webpage) Run() { wp.initialize() wp.Router = gin.New() wp.Router.Use(gin.Recovery()) wp.Router.Use(gin.Logger()) wp.Router.SetTrustedProxies(nil) wp.Router.Use(static.Serve("/static", static.LocalFile("./static", false))) wp.Router.LoadHTMLGlob("templates/*") wp.defineRoutes() wp.Router.Run() }