Skip to content

Instantly share code, notes, and snippets.

@ringmaster
Created December 2, 2025 23:16
Show Gist options
  • Select an option

  • Save ringmaster/0607af4a3f472af1187f696b37a6bf2b to your computer and use it in GitHub Desktop.

Select an option

Save ringmaster/0607af4a3f472af1187f696b37a6bf2b to your computer and use it in GitHub Desktop.
As simple http server with Gin
import (
"github.com/gin-gonic/gin"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
)
func main() {
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.POST("/login", func(c *gin.Context) {
// You would authenticate the submitted user/password here
session := sessions.Default(c)
session.Set("user", "username") // Set user session
session.Save()
c.JSON(200, gin.H{"message": "Logged in"})
})
r.GET("/protected", func(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")
if user == nil {
c.JSON(401, gin.H{"message": "Unauthorized"})
return
}
c.JSON(200, gin.H{"message": "Welcome!", "user": user})
})
r.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment