133 lines
3.3 KiB
Go

package main
import (
"git.anthrove.art/Anthrove/gorse-playground/internal/logic"
"git.anthrove.art/Anthrove/gorse-playground/pkg/models"
"git.anthrove.art/Anthrove/gorse-playground/pkg/utils"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
func main() {
router := gin.Default()
store := cookie.NewStore([]byte("secret"))
router.Use(sessions.Sessions("mysession", store))
router.Use(func(c *gin.Context) {
session := sessions.Default(c)
userid := session.Get("userid")
if userid == nil && c.FullPath() != "/login" {
c.Redirect(http.StatusFound, "/login")
c.Abort()
return
}
c.Next()
})
router.LoadHTMLGlob("web/template/**/*")
router.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.gohtml", nil)
})
router.POST("/login", func(c *gin.Context) {
session := sessions.Default(c)
userID := c.PostForm("userid")
// Handle the user ID as needed
session.Set("userid", userID)
err := session.Save()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.Redirect(http.StatusFound, "/profile")
})
router.GET("/logout", func(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
c.Redirect(http.StatusFound, "/login")
})
router.GET("/profile", func(c *gin.Context) {
session := sessions.Default(c)
userid := session.Get("userid")
c.HTML(http.StatusOK, "profile.gohtml", gin.H{"userid": userid})
})
router.POST("/scrape", func(c *gin.Context) {
session := sessions.Default(c)
userid := session.Get("userid")
useridInt, err := strconv.Atoi(userid.(string))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"convert error": err.Error()})
return
}
allFaves, err := logic.GetAllFavorites(c, useridInt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"get faves error": err.Error()})
return
}
items, faves, err := utils.ConvertE6ToGorse(userid.(string), allFaves)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"convert to gorse error": err.Error()})
return
}
err = logic.UpsertUser(c, models.GorseUser{
Comment: "",
Labels: nil,
Subscribe: nil,
UserId: userid.(string),
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"upsert user error": err.Error()})
return
}
err = logic.UpsertItems(c, items)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"upsert items error": err.Error()})
return
}
err = logic.UpsertFavorites(c, faves)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"upsert faves error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"items": items})
})
router.GET("/post", func(c *gin.Context) {
session := sessions.Default(c)
userid := session.Get("userid")
page := c.DefaultQuery("page", "1")
pageInt, err := strconv.Atoi(page)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
recs, err := logic.GetUserFavorites(c, userid.(string), pageInt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.HTML(http.StatusOK, "post.gohtml", gin.H{"recs": recs, "next_page": pageInt + 1, "last_page": pageInt - 1})
})
router.Run(":8080")
}