Use gin-gonic instead of httprouter

This commit is contained in:
nemunaire 2022-07-09 19:42:00 +02:00
parent 7c719d9fd5
commit a203cdc36a
22 changed files with 1668 additions and 1392 deletions

37
help.go
View file

@ -1,18 +1,39 @@
package main
import (
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
)
func init() {
router.GET("/api/help", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse {
return formatApiResponse(getNeedHelps())
}, adminRestricted))
router.POST("/api/help", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse {
return formatApiResponse(u.NewNeedHelp())
}, loggedUser))
func declareAPIAdminHelpRoutes(router *gin.RouterGroup) {
router.GET("/help", func(c *gin.Context) {
nhs, err := getNeedHelps()
if err != nil {
log.Println("Unable to getNeedHelps:", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during need helps retrieval. Please retry."})
return
}
c.JSON(http.StatusOK, nhs)
})
}
func declareAPIAuthHelpRoutes(router *gin.RouterGroup) {
router.POST("/help", func(c *gin.Context) {
u := c.MustGet("LoggedUser").(*User)
nh, err := u.NewNeedHelp()
if err != nil {
log.Printf("Unable to NewNeedHelp(uid=%d): %s", u.Id, err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Sorry, something went wrong. Please retry in a few moment."})
return
}
c.JSON(http.StatusOK, nh)
})
}
type NeedHelp struct {