RESTful API に対する認証モデルを作成したいと考えています。API トークンを使用したいと考えています。私は Web サービスで MVC を使用しており、このような auth.go コントローラーを作成しました。
package controllers
import (
"github.com/gin-gonic/gin"
"os"
//"github.com/jinzhu/gorm"
)
type AdsControllerAuth struct {
}
func (ac *AdsControllerAuth)TokenAuthMiddleware gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Request.FormValue("api_token")
if token == "" {
respondWithError(401, "API token required", c)
return
}
if token != os.Getenv("API_TOKEN") {
respondWithError(401, "Invalid API token", c)
return
}
c.Next()
}
}
func respondWithError(code int,message string,c *gin.Context) {
resp := map[string]string{"error": message}
c.JSON(code, resp)
//c.Abort(code)
}
今のところ機能していませんが、これを行うのに役立つ人がいますか、参照する例はありますか?