-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
70 lines (56 loc) · 1.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"log"
"os"
"time"
"github.com/gin-contrib/cors"
"github.com/jexlor/cs2api/api"
"github.com/jexlor/cs2api/db"
"github.com/jexlor/cs2api/dev"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
db.InitDB()
port := os.Getenv("PORT")
if port == "" {
port = "8080" // Default to 8080 if not set
log.Printf("No PORT specified, defaulting to %s", port)
}
router := setupRouter()
if err := router.Run(":" + port); err != nil {
log.Fatalf("Error starting server: %v", err)
}
}
func setupRouter() *gin.Engine {
router := gin.Default()
// Update CORS to allow the hx-trigger header and any other headers you may need
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // You can change this to the specific frontend URL if needed
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Content-Type", "Authorization", "hx-trigger"}, // Allow hx-trigger header
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
router.LoadHTMLGlob("templates/*")
// Define your API routes
apiGroup := router.Group("/cs2api")
{
apiGroup.GET("/", api.LandingPage)
apiGroup.GET("/skins", api.GetAllSkins)
apiGroup.GET("/skins/search", api.GetSkinById)
apiGroup.GET("/skins/search/n", api.GetSkinByName)
apiGroup.GET("/collections", api.GetCollections)
apiGroup.GET("/collections/search/n", api.GetCollectionByName)
apiGroup.POST("/skins", dev.AddSkins) // hide for production
apiGroup.DELETE("/skins/delete", dev.DeleteSkinByName) // hide for production
apiGroup.PUT("/skins/edit", dev.UpdateSkinByName) // hide for production
//apiGroup.PATCH("/skins/edit", dev.UpdateSkinPrices)
}
return router
}