This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
swb6-it-sec/Lab01/app/webpage/webpage.go

110 lines
2.6 KiB
Go
Raw Normal View History

2022-03-30 14:54:01 +02:00
package webpage
import (
"app/database"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"net/http"
)
2022-04-04 10:16:29 +02:00
func (wp *Webpage) redirectHome(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/")
}
func (wp *Webpage) userIsLoggedIn(c *gin.Context) bool {
if wp.isLoggedIn {
wp.redirectHome(c)
return true
}
return false
}
2022-03-30 14:54:01 +02:00
func (wp *Webpage) defineRoutes() {
wp.Router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
2022-04-04 10:16:29 +02:00
"title": "SuperSafe",
"loggedIn": wp.isLoggedIn,
2022-03-30 14:54:01 +02:00
})
})
2022-04-04 10:16:29 +02:00
2022-03-30 14:54:01 +02:00
wp.Router.GET("/login", func(c *gin.Context) {
2022-04-04 10:16:29 +02:00
if wp.userIsLoggedIn(c) {
return
}
2022-03-30 14:54:01 +02:00
c.HTML(http.StatusOK, "login.tmpl", gin.H{
2022-04-04 10:16:29 +02:00
"title": "Login",
"loggedIn": wp.isLoggedIn,
2022-03-30 14:54:01 +02:00
})
})
2022-04-04 10:16:29 +02:00
wp.Router.GET("/logout", func(c *gin.Context) {
wp.isLoggedIn = false
wp.redirectHome(c)
})
2022-03-30 14:54:01 +02:00
wp.Router.GET("/register", func(c *gin.Context) {
2022-04-04 10:16:29 +02:00
wp.userIsLoggedIn(c)
2022-03-30 14:54:01 +02:00
c.HTML(http.StatusOK, "register.tmpl", gin.H{
2022-04-04 10:16:29 +02:00
"title": "Register",
"loggedIn": wp.isLoggedIn,
2022-03-30 14:54:01 +02:00
})
})
wp.Router.POST("/login", func(c *gin.Context) {
2022-04-04 10:16:29 +02:00
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
2022-04-04 10:16:29 +02:00
success, err := wp.Database.LoginUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
if success == true {
wp.isLoggedIn = true
c.JSON(200, gin.H{"message": "login successfull"})
return
}
wp.isLoggedIn = false
c.JSON(401, gin.H{"message": "user or password not found"})
})
2022-03-30 14:54:01 +02:00
wp.Router.POST("/register", func(c *gin.Context) {
2022-04-04 10:16:29 +02:00
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
2022-03-30 14:54:01 +02:00
c.JSON(400, gin.H{"message": "bad post form"})
return
}
2022-04-04 10:16:29 +02:00
err := wp.Database.CreateUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
2022-03-30 15:11:58 +02:00
return
2022-03-30 14:54:01 +02:00
}
c.JSON(200, gin.H{"message": "user registered"})
})
2022-04-04 10:16:29 +02:00
wp.Router.NoRoute(func(c *gin.Context) {
wp.redirectHome(c)
})
2022-03-30 14:54:01 +02:00
}
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.SetTrustedProxies(nil)
wp.Router.Use(static.Serve("/static", static.LocalFile("./static", false)))
wp.Router.LoadHTMLGlob("templates/*")
wp.defineRoutes()
wp.Router.Run()
}