first commmit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"fishfish/database"
|
||||
"fishfish/handlers"
|
||||
"fishfish/middleware"
|
||||
)
|
||||
|
||||
func main() {
|
||||
database.InitDB()
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Println("Файл .env не был найден в папке backend, использую системные переменные")
|
||||
}
|
||||
|
||||
// CORS Middleware
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "https://fish.kkhome.ru")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
|
||||
api := r.Group("/api")
|
||||
{
|
||||
api.POST("/login", handlers.Login)
|
||||
protected := api.Group("/")
|
||||
protected.Use(middleware.AuthRequired())
|
||||
{
|
||||
protected.GET("/items/:id", handlers.GetItem)
|
||||
protected.GET("/items/", handlers.GetAllItems)
|
||||
protected.DELETE("/items/:id", handlers.DeleteItem)
|
||||
protected.PATCH("/items/:id", handlers.UpdateItem)
|
||||
protected.POST("/items", handlers.AddItem)
|
||||
}
|
||||
|
||||
admin := protected.Group("/")
|
||||
admin.Use(middleware.AdminOnly())
|
||||
{
|
||||
admin.POST("/register", handlers.Register)
|
||||
}
|
||||
}
|
||||
|
||||
r.Run(":8081")
|
||||
}
|
||||
Reference in New Issue
Block a user