feat: full redy blog and admin panel
This commit is contained in:
@@ -30,7 +30,7 @@ docker:
|
||||
docker build -t backend .
|
||||
|
||||
docker-run:
|
||||
docker run --rm -p 8080:8080 --env-file .env -v /opt/d3m0k1d.ru/data:/data backend:latest
|
||||
docker run --rm -p 8080:8080 --env-file .env --network host -v /opt/d3m0k1d.ru/data:/data backend:latest
|
||||
|
||||
run-docker:
|
||||
docker build -t backend .
|
||||
|
||||
@@ -15,6 +15,67 @@ const docTemplate = `{
|
||||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {
|
||||
"/admin/posts": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"description": "Get all posts",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"admin"
|
||||
],
|
||||
"summary": "Get all posts",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.SuccessResponse"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_storage.PostReq"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Invalid ID format",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse"
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "No Post found",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse"
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal server error",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/auth/github": {
|
||||
"get": {
|
||||
"description": "Redirects to GitHub authorization",
|
||||
@@ -89,7 +150,7 @@ const docTemplate = `{
|
||||
"tags": [
|
||||
"posts"
|
||||
],
|
||||
"summary": "Get all posts",
|
||||
"summary": "Get all published posts",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
@@ -565,6 +626,9 @@ const docTemplate = `{
|
||||
"content": {
|
||||
"type": "string"
|
||||
},
|
||||
"created_at": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "integer"
|
||||
},
|
||||
|
||||
@@ -4,6 +4,67 @@
|
||||
"contact": {}
|
||||
},
|
||||
"paths": {
|
||||
"/admin/posts": {
|
||||
"get": {
|
||||
"security": [
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"description": "Get all posts",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"admin"
|
||||
],
|
||||
"summary": "Get all posts",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.SuccessResponse"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"data": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_storage.PostReq"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Invalid ID format",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse"
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "No Post found",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse"
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "Internal server error",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/auth/github": {
|
||||
"get": {
|
||||
"description": "Redirects to GitHub authorization",
|
||||
@@ -78,7 +139,7 @@
|
||||
"tags": [
|
||||
"posts"
|
||||
],
|
||||
"summary": "Get all posts",
|
||||
"summary": "Get all published posts",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
@@ -554,6 +615,9 @@
|
||||
"content": {
|
||||
"type": "string"
|
||||
},
|
||||
"created_at": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "integer"
|
||||
},
|
||||
|
||||
@@ -43,6 +43,8 @@ definitions:
|
||||
properties:
|
||||
content:
|
||||
type: string
|
||||
created_at:
|
||||
type: string
|
||||
id:
|
||||
type: integer
|
||||
title:
|
||||
@@ -51,6 +53,42 @@ definitions:
|
||||
info:
|
||||
contact: {}
|
||||
paths:
|
||||
/admin/posts:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Get all posts
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.SuccessResponse'
|
||||
- properties:
|
||||
data:
|
||||
items:
|
||||
$ref: '#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_storage.PostReq'
|
||||
type: array
|
||||
type: object
|
||||
"400":
|
||||
description: Invalid ID format
|
||||
schema:
|
||||
$ref: '#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse'
|
||||
"404":
|
||||
description: No Post found
|
||||
schema:
|
||||
$ref: '#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse'
|
||||
"500":
|
||||
description: Internal server error
|
||||
schema:
|
||||
$ref: '#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse'
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Get all posts
|
||||
tags:
|
||||
- admin
|
||||
/auth/github:
|
||||
get:
|
||||
description: Redirects to GitHub authorization
|
||||
@@ -123,7 +161,7 @@ paths:
|
||||
description: Internal server error
|
||||
schema:
|
||||
$ref: '#/definitions/gitea_d3m0k1d_ru_d3m0k1d_d3m0k1d_ru_backend_internal_models.ErrorResponse'
|
||||
summary: Get all posts
|
||||
summary: Get all published posts
|
||||
tags:
|
||||
- posts
|
||||
post:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
type PostRepository interface {
|
||||
GetAll(ctx context.Context) ([]storage.PostReq, error)
|
||||
GetAllAdmin(ctx context.Context) ([]storage.PostReq, error)
|
||||
GetByID(ctx context.Context, id int) (storage.PostReq, error)
|
||||
GetLastID(ctx context.Context) (int, error)
|
||||
IsExist(ctx context.Context, id int) bool
|
||||
|
||||
@@ -22,7 +22,7 @@ func NewPostRepository(db *sql.DB) PostRepository {
|
||||
}
|
||||
func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error) {
|
||||
var result []storage.PostReq
|
||||
rows, err := p.db.Query("SELECT id, title, content FROM posts WHERE published = 1")
|
||||
rows, err := p.db.Query("SELECT id, title, content, CREATED_AT FROM posts WHERE published = 1")
|
||||
if err != nil {
|
||||
p.logger.Error(err.Error())
|
||||
return nil, err
|
||||
@@ -31,14 +31,16 @@ func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error)
|
||||
var title string
|
||||
var content string
|
||||
var id int
|
||||
err := rows.Scan(&id, &title, &content)
|
||||
var createdAt string
|
||||
err := rows.Scan(&id, &title, &content, &createdAt)
|
||||
if err != nil {
|
||||
p.logger.Error("error scan: " + err.Error())
|
||||
}
|
||||
result = append(result, storage.PostReq{
|
||||
ID: id,
|
||||
Title: title,
|
||||
Content: content,
|
||||
ID: id,
|
||||
Title: title,
|
||||
Content: content,
|
||||
CreatedAt: createdAt,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
@@ -46,17 +48,19 @@ func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error)
|
||||
|
||||
func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq, error) {
|
||||
var result storage.PostReq
|
||||
row := p.db.QueryRow("SELECT title, content FROM posts WHERE id = ? AND published = 1", id)
|
||||
row := p.db.QueryRow("SELECT title, content, CREATED_AT FROM posts WHERE id = ? AND published = 1", id)
|
||||
var title string
|
||||
var content string
|
||||
err := row.Scan(&title, &content)
|
||||
var createdAt string
|
||||
err := row.Scan(&title, &content, &createdAt)
|
||||
if err != nil {
|
||||
p.logger.Error("error scan: " + err.Error())
|
||||
}
|
||||
result = storage.PostReq{
|
||||
ID: id,
|
||||
Title: title,
|
||||
Content: content,
|
||||
ID: id,
|
||||
Title: title,
|
||||
Content: content,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -135,3 +139,27 @@ func (p *postRepository) IsExist(ctx context.Context, id int) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *postRepository) GetAllAdmin(ctx context.Context) ([]storage.PostReq, error) {
|
||||
result := []storage.PostReq{}
|
||||
rows, err := p.db.Query("SELECT id, title, content FROM posts")
|
||||
if err != nil {
|
||||
p.logger.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var title string
|
||||
var content string
|
||||
var id int
|
||||
err := rows.Scan(&id, &title, &content)
|
||||
if err != nil {
|
||||
p.logger.Error("error scan: " + err.Error())
|
||||
}
|
||||
result = append(result, storage.PostReq{
|
||||
ID: id,
|
||||
Title: title,
|
||||
Content: content,
|
||||
})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ CREATE TABLE IF NOT EXISTS posts(
|
||||
title TEXT NOT NULL,
|
||||
published BOOLEAN DEFAULT 0,
|
||||
content TEXT NOT NULL,
|
||||
CREATED_AT DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
CREATED_AT DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users(
|
||||
|
||||
@@ -9,9 +9,10 @@ type Post struct {
|
||||
}
|
||||
|
||||
type PostReq struct {
|
||||
ID int `db:"id" json:"id"`
|
||||
Title string `db:"title" json:"title"`
|
||||
Content string `db:"content" json:"content"`
|
||||
ID int `db:"id" json:"id"`
|
||||
Title string `db:"title" json:"title"`
|
||||
Content string `db:"content" json:"content"`
|
||||
CreatedAt string `db:"created" json:"created_at"`
|
||||
}
|
||||
|
||||
type PostCreate struct {
|
||||
|
||||
Reference in New Issue
Block a user