feat: connection to db logic added to main
Some checks failed
Build and Deploy Go App / build (push) Successful in 8m52s
Build and Deploy Go App / deploy (push) Has been cancelled

This commit is contained in:
Iron_Felix 2025-10-11 02:19:33 +03:00
parent eda59c68d3
commit 102d15c5fd
3 changed files with 65 additions and 25 deletions

View file

@ -1,35 +1,48 @@
package main
import (
"context"
"fmt"
"net/http"
"nyanimedb-server/api"
"nyanimedb-server/db"
"os"
"reflect"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"github.com/pelletier/go-toml/v2"
log "github.com/sirupsen/logrus"
)
var AppConfig Config
func main() {
if len(os.Args) != 2 {
AppConfig.Mode = "env"
} else {
AppConfig.Mode = "argv"
// if len(os.Args) != 2 {
// AppConfig.Mode = "env"
// } else {
// AppConfig.Mode = "argv"
// }
// err := InitConfig()
// if err != nil {
// log.Fatalf("Failed to init config: %v\n", err)
// }
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
err := InitConfig()
if err != nil {
log.Fatalf("Failed to init config: %v\n", err)
}
defer conn.Close(context.Background())
r := gin.Default()
r.LoadHTMLGlob("templates/*")
queries := db.New(conn)
server := api.NewServer(queries)
// r.LoadHTMLGlob("templates/*")
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // allow all origins, change to specific domains in production
@ -40,22 +53,27 @@ func main() {
MaxAge: 12 * time.Hour,
}))
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Welcome Page",
"message": "Hello, Gin with HTML templates!",
})
})
api.RegisterHandlers(r, api.NewStrictHandler(
server,
// сюда можно добавить middlewares, если нужно
[]api.StrictMiddlewareFunc{},
))
// r.GET("/", func(c *gin.Context) {
// c.HTML(http.StatusOK, "index.html", gin.H{
// "title": "Welcome Page",
// "message": "Hello, Gin with HTML templates!",
// })
// })
r.GET("/api", func(c *gin.Context) {
items := []Item{
{ID: 1, Title: "First Item", Description: "This is the description of the first item."},
{ID: 2, Title: "Second Item", Description: "This is the description of the second item."},
{ID: 3, Title: "Third Item", Description: "This is the description of the third item."},
}
// r.GET("/api", func(c *gin.Context) {
// items := []Item{
// {ID: 1, Title: "First Item", Description: "This is the description of the first item."},
// {ID: 2, Title: "Second Item", Description: "This is the description of the second item."},
// {ID: 3, Title: "Third Item", Description: "This is the description of the third item."},
// }
c.JSON(http.StatusOK, items)
})
// c.JSON(http.StatusOK, items)
// })
r.Run(":8080")
}