Skip to content

Commit

Permalink
feat: sessions, engine rework
Browse files Browse the repository at this point in the history
  • Loading branch information
konotorii committed Jun 2, 2024
1 parent c64e584 commit 975acbf
Showing 1 changed file with 101 additions and 8 deletions.
109 changes: 101 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,114 @@
package main

//import "net/http"
//import "io"
//import "log"

//import "github.com/sunshineplan/imgconv"

import (
"fmt"
_ "io"
"log"
"net/http"
"os"
"strings"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)

const userKey = "user"

var secret = []byte(os.Getenv("SECRET"))

func main() {
router := gin.Default()
r := engine()
r.Use(gin.Logger())
if err := engine().Run(":8080"); err != nil {
log.Fatal("unable to start:", err)
}
}

func engine() *gin.Engine {
r := gin.New()

r.Use(sessions.Sessions("session", cookie.NewStore(secret)))

r.GET("/img", getServeImage)
r.POST("/login", login)
r.GET("/logout", logout)

admin := r.Group("/admin")
admin.Use(AuthRequired)
{
admin.POST("/upload", postImage)
}

return r
}

func getServeImage(c *gin.Context) {

}

func postImage(c *gin.Context) {
// Single file
file, _ := c.FormFile("file")
log.Println(file.Filename)

// Upload the file to specific dst.
c.SaveUploadedFile(file, "./public")

c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
}

func AuthRequired(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(userKey)
if user == nil {
// Abort the request with the appropriate error code
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
// Continue down the chain to handler etc
c.Next()
}

func login(c *gin.Context) {
session := sessions.Default(c)
username := c.PostForm("username")
password := c.PostForm("password")

// Validate form input
if strings.Trim(username, " ") == "" || strings.Trim(password, " ") == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Parameters can't be empty"})
return
}

// Check for username and password match, usually from a database
if username != "hello" || password != "itsme" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"})
return
}

router.Run("localhost:8000")
// Save the username in the session
session.Set(userKey, username) // In real world usage you'd set this to the users ID
if err := session.Save(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully authenticated user"})
}

func convertImage(c *gin.Context) {
url := c.Param("url")
func logout(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(userKey)
if user == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session token"})
return
}
session.Delete(userKey)
if err := session.Save(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Successfully logged out"})
}

0 comments on commit 975acbf

Please sign in to comment.