feat: connection to db logic added to main
This commit is contained in:
parent
eda59c68d3
commit
102d15c5fd
3 changed files with 65 additions and 25 deletions
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue