szoe-pontok/api/endpotins.go
2024-10-11 15:58:12 +02:00

74 lines
1.5 KiB
Go

package api
import (
"net/http"
"time"
"git.tek.govt.hu/dowerx/szoe-pontok/api/auth"
"git.tek.govt.hu/dowerx/szoe-pontok/api/task"
"github.com/gin-gonic/gin"
)
func Listen(address string, path string) {
router := gin.Default()
api := router.Group(path)
{
api.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
"date": time.Now().UTC().Unix(),
})
})
apiAuth := api.Group("auth")
{
apiAuth.POST("register", auth.Register)
apiAuth.POST("login", auth.Login)
}
apiAdmin := api.Group("admin").Use(auth.LoggedIn).Use(auth.IsAdmin)
{
apiAdmin.POST("task/add", task.Add)
apiAdmin.POST("task/remove/:id", task.Remove)
apiAdmin.GET("task/list/:neptun", task.ListUser)
}
apiUser := api.Group("user").Use(auth.LoggedIn)
{
apiUser.GET("list", task.List)
}
apiTest := api.Group("test").Use(auth.LoggedIn).Use(auth.IsAdmin)
{
apiTest.GET("logged_in", func(c *gin.Context) {
neptun, _ := c.Get("neptun")
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "if you see this you are logged in",
"neptun": neptun,
})
})
apiTest.GET("is_admin", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "if you see this you are an admin",
})
})
}
}
server := &http.Server{
Addr: address,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}