feat: full redy blog and admin panel
This commit is contained in:
@@ -3,7 +3,6 @@ package handlers
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"strings"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/auth"
|
||||
@@ -16,15 +15,17 @@ import (
|
||||
)
|
||||
|
||||
type AuthHandlers struct {
|
||||
repo repositories.AuthRepository
|
||||
logger *logger.Logger
|
||||
config *oauth2.Config
|
||||
repo repositories.AuthRepository
|
||||
logger *logger.Logger
|
||||
config *oauth2.Config
|
||||
frontendURL string
|
||||
}
|
||||
|
||||
func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
|
||||
clientID := os.Getenv("GITHUB_CLIENT_ID")
|
||||
clientSecret := os.Getenv("GITHUB_CLIENT_SECRET")
|
||||
redirectURL := os.Getenv("REDIRECT_URL")
|
||||
frontendURL := os.Getenv("FRONTEND_URL")
|
||||
|
||||
if clientID == "" || clientSecret == "" {
|
||||
panic("GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET must be set")
|
||||
@@ -32,10 +33,14 @@ func NewAuthHandlers(repo repositories.AuthRepository) *AuthHandlers {
|
||||
if redirectURL == "" {
|
||||
redirectURL = "http://localhost:8080/api/v1/callback/github"
|
||||
}
|
||||
if frontendURL == "" {
|
||||
frontendURL = "https://d3m0k1d.ru"
|
||||
}
|
||||
|
||||
return &AuthHandlers{
|
||||
repo: repo,
|
||||
logger: logger.New(false),
|
||||
repo: repo,
|
||||
logger: logger.New(false),
|
||||
frontendURL: frontendURL,
|
||||
config: &oauth2.Config{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
@@ -75,7 +80,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
code := c.Query("code")
|
||||
if code == "" {
|
||||
h.logger.Error("missing code")
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=missing_code")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=missing_code")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -84,7 +89,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
token, err := h.config.Exchange(c.Request.Context(), code)
|
||||
if err != nil {
|
||||
h.logger.Error("Exchange failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=auth_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=auth_failed")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -92,7 +97,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
resp, err := client.Get("https://api.github.com/user")
|
||||
if err != nil {
|
||||
h.logger.Error("Get failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=github_api_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=github_api_failed")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -100,14 +105,14 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
err = json.NewDecoder(resp.Body).Decode(&ghUser)
|
||||
if err != nil {
|
||||
h.logger.Error("Decode failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=decode_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=decode_failed")
|
||||
return
|
||||
}
|
||||
|
||||
isreg, err := h.repo.IsRegistered(c.Request.Context(), ghUser.GithubID)
|
||||
if err != nil {
|
||||
h.logger.Error("Database check failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=database_error")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=database_error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,7 +121,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
id, err = h.repo.Register(c.Request.Context(), ghUser)
|
||||
if err != nil {
|
||||
h.logger.Error("Registration failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=registration_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=registration_failed")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -124,7 +129,7 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
user, err := h.repo.GetUserByGithubID(c.Request.Context(), ghUser.GithubID)
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to fetch user: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=user_fetch_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=user_fetch_failed")
|
||||
return
|
||||
}
|
||||
id = user.ID
|
||||
@@ -144,13 +149,13 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
||||
jwtToken, err := auth.GenerateJWT(user)
|
||||
if err != nil {
|
||||
h.logger.Error("JWT generation failed: " + err.Error())
|
||||
c.Redirect(302, "https://d3m0k1d.ru/login?error=token_failed")
|
||||
c.Redirect(302, h.frontendURL+"/login?error=token_failed")
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("Authentication successful for user: " + ghUser.GithubLogin)
|
||||
|
||||
c.Redirect(302, "https://d3m0k1d.ru/auth/callback#token="+jwtToken)
|
||||
c.Redirect(302, h.frontendURL+"/auth/callback#token="+jwtToken)
|
||||
}
|
||||
|
||||
// GetSession godoc
|
||||
|
||||
@@ -20,7 +20,7 @@ func NewPostHandlers(repo repositories.PostRepository) *PostHandlers {
|
||||
}
|
||||
|
||||
// GetPosts godoc
|
||||
// @Summary Get all posts
|
||||
// @Summary Get all published posts
|
||||
// @Description Get all posts
|
||||
// @Tags posts
|
||||
// @Accept json
|
||||
@@ -31,7 +31,7 @@ func NewPostHandlers(repo repositories.PostRepository) *PostHandlers {
|
||||
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
|
||||
// @Router /posts [get]
|
||||
func (h *PostHandlers) GetPosts(c *gin.Context) {
|
||||
var result []storage.PostReq
|
||||
result := []storage.PostReq{}
|
||||
result, err := h.repo.GetAll(c.Request.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
@@ -192,3 +192,32 @@ func (h *PostHandlers) DeletePost(c *gin.Context) {
|
||||
models.Success(c, "Post deleted")
|
||||
|
||||
}
|
||||
|
||||
// GetPosts godoc
|
||||
// @Summary Get all posts
|
||||
// @Description Get all posts
|
||||
// @Tags admin
|
||||
// @Security Bearer
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} models.SuccessResponse{data=[]storage.PostReq}
|
||||
// @Failure 404 {object} models.ErrorResponse "No Post found"
|
||||
// @Failure 500 {object} models.ErrorResponse "Internal server error"
|
||||
// @Failure 400 {object} models.ErrorResponse "Invalid ID format"
|
||||
// @Router /admin/posts [get]
|
||||
func (h *PostHandlers) AdminGetAll(c *gin.Context) {
|
||||
result := []storage.PostReq{}
|
||||
result, err := h.repo.GetAllAdmin(c.Request.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("error request: " + err.Error())
|
||||
models.Error(c, 500, "Internal server error", err.Error())
|
||||
return
|
||||
}
|
||||
if result == nil {
|
||||
models.Error(c, 404, "No Post found", "")
|
||||
return
|
||||
}
|
||||
h.logger.Info("200 OK GET /posts")
|
||||
models.Success(c, result)
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ func Register(router *gin.Engine, db *sql.DB) {
|
||||
handler_static := NewStaticHandlers()
|
||||
router.GET("/health", func(c *gin.Context) { c.Status(200) })
|
||||
v1 := router.Group("api/v1")
|
||||
admin := v1.Group("admin")
|
||||
{
|
||||
admin.GET("/posts", auth.JWTMiddleware(), auth.RequireAdmin(), handler_posts.AdminGetAll)
|
||||
}
|
||||
v1.Static("/uploads", "/data/uploads")
|
||||
v1.POST("/upload", auth.JWTMiddleware(), auth.RequireAdmin(), handler_static.PostStatic)
|
||||
v1.GET("/upload/:file", handler_static.GetStatic)
|
||||
|
||||
Reference in New Issue
Block a user