81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"esauth/pkg/handler/oauth"
|
|
"esauth/pkg/repository"
|
|
"esauth/pkg/service"
|
|
"net/http"
|
|
|
|
"esauth/docs"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
swaggerFiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
)
|
|
|
|
type Handler struct {
|
|
services *service.Service
|
|
repo *repository.Repository
|
|
telegramOAuth *oauth.TelegramOAuth
|
|
googleOAuth *oauth.GoogleOAuth
|
|
}
|
|
|
|
func NewHandler(services *service.Service, repo *repository.Repository, telegramOAuth *oauth.TelegramOAuth, googleOAuth *oauth.GoogleOAuth) *Handler {
|
|
return &Handler{services: services, repo: repo, telegramOAuth: telegramOAuth, googleOAuth: googleOAuth}
|
|
}
|
|
|
|
// @title EasyStartup Auth
|
|
// @version 1.0
|
|
// @description Auth server for EasyStartup.
|
|
//
|
|
// @contact.name API Support
|
|
// @contact.email red.tea.dev@gmail.com
|
|
//
|
|
// @host auth.easystartup.su
|
|
// @BasePath /api
|
|
//
|
|
|
|
func (h *Handler) InitRoutes() *gin.Engine {
|
|
router := gin.New()
|
|
docs.SwaggerInfo.BasePath = "/"
|
|
router.StaticFS("/static", http.Dir("./public/resources"))
|
|
router.LoadHTMLGlob("public/html/*")
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
|
"botId": h.telegramOAuth.BotId,
|
|
})
|
|
})
|
|
router.GET("/google-callback", func(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "google-callback.html", gin.H{})
|
|
})
|
|
router.GET("/telegram-callback", func(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "telegram-callback.html", gin.H{})
|
|
})
|
|
|
|
router.GET("/telegram-callback-process", h.telegramOAuth.HandleTelegramOAuthCallback)
|
|
router.GET("/google-callback-process", h.googleOAuth.HandleGoogleCallback)
|
|
router.GET("/redir/google", func(c *gin.Context) {
|
|
url := h.googleOAuth.GoogleOauthConfig.AuthCodeURL(oauth.GOOGLE_RANDOM_STATE)
|
|
logrus.Info(url)
|
|
c.Redirect(http.StatusTemporaryRedirect, url)
|
|
})
|
|
|
|
api := router.Group("/api", h.userIdentity)
|
|
{
|
|
api.GET("user/self", h.profile)
|
|
api.POST("user/self", h.edit)
|
|
api.GET("user/all", h.all)
|
|
api.GET("user/count", h.count)
|
|
api.GET("user/infinity-token", h.infinityToken)
|
|
api.PUT("user", h.create)
|
|
api.GET("user/:id", h.userById)
|
|
api.POST("user/:id", h.editById)
|
|
api.DELETE("user/:id", h.delete)
|
|
}
|
|
|
|
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
return router
|
|
}
|