184 lines
4.3 KiB
Go
184 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
/*err := logic.SubmitItems(context.Background())
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}*/
|
|
|
|
go Routine(context.Background())
|
|
|
|
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.POST("/like/:id", func(c *gin.Context) {
|
|
session := sessions.Default(c)
|
|
userid := session.Get("userid")
|
|
id := c.Param("id")
|
|
|
|
err := logic.UpsertFavorites(c, []models.GorseFavorite{{
|
|
Comment: "",
|
|
FeedbackType: "like",
|
|
ItemId: id,
|
|
Timestamp: time.Now().String(),
|
|
UserId: userid.(string),
|
|
}})
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"upsert favorite error": err.Error()})
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"item": id})
|
|
})
|
|
|
|
router.Run(":8080")
|
|
|
|
}
|
|
|
|
func Routine(c context.Context) {
|
|
now := time.Now()
|
|
next := time.Date(now.Year(), now.Month(), now.Day(), 4, 0, 0, 0, now.Location())
|
|
if now.After(next) {
|
|
next = next.Add(24 * time.Hour)
|
|
}
|
|
duration := next.Sub(now)
|
|
time.Sleep(duration)
|
|
ticker := time.NewTicker(24 * time.Hour)
|
|
|
|
for {
|
|
select {
|
|
case <-c.Done():
|
|
ticker.Stop()
|
|
return
|
|
case <-ticker.C:
|
|
logic.SubmitItems(c)
|
|
}
|
|
}
|
|
|
|
}
|