89 lines
3.7 KiB
Go
89 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/gob"
|
|
"log"
|
|
"os"
|
|
|
|
"ordr-api/auth"
|
|
"ordr-api/auth/middleware"
|
|
"ordr-api/controllers"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
"github.com/gin-contrib/sessions/cookie"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func init_db_pool(databaseUrl string) (*pgxpool.Pool, error) {
|
|
config, err := pgxpool.ParseConfig(databaseUrl)
|
|
if err != nil {
|
|
log.Fatalf("Failed to load pgx pool config %s....", err)
|
|
}
|
|
|
|
pool, err := pgxpool.NewWithConfig(context.Background(), config)
|
|
|
|
err = pool.Ping(context.Background())
|
|
if err != nil {
|
|
log.Fatalf("Unable to ping database: %v\n", err)
|
|
}
|
|
|
|
return pool, err
|
|
}
|
|
|
|
func main() {
|
|
if err := godotenv.Load(); err != nil {
|
|
log.Fatalf("Failed to load the env vars: %v", err)
|
|
}
|
|
|
|
authenticator, auth_err := auth.New()
|
|
if auth_err != nil {
|
|
log.Fatal("ERROR: Failed to initialize Authenticator")
|
|
return
|
|
}
|
|
|
|
pool, pool_err := init_db_pool(os.Getenv("CONNECTION_STRING"))
|
|
|
|
if pool_err != nil {
|
|
log.Fatal("ERROR: Failed to initialize DB pooL")
|
|
}
|
|
|
|
router := gin.Default()
|
|
|
|
store := cookie.NewStore([]byte(os.Getenv("COOKIE_SECRET")))
|
|
router.Use(sessions.Sessions("auth-session", store))
|
|
|
|
// Middleware Function Declarations
|
|
user_authenticated := middleware.IsAuthenticated(authenticator)
|
|
user_in_db := middleware.UserInDatabase(pool)
|
|
user_active := middleware.UserIsActive(pool)
|
|
user_is_admin := middleware.IsAdmin(pool)
|
|
|
|
gob.Register(map[string]interface{}{})
|
|
router.GET("/", user_authenticated, middleware.GetUserProfile, controllers.BaseFunction)
|
|
router.GET("/auth/login", auth.LoginHandler(authenticator))
|
|
router.GET("/auth/logout", auth.LogoutHandler)
|
|
router.GET("/auth/logout_callback", auth.LogoutCallbackHandler(store))
|
|
router.GET("/callback", auth.AuthenticationCallbackHandler(authenticator))
|
|
router.GET("/users", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.GetUserTable(pool))
|
|
router.GET("/user/current", user_authenticated, middleware.GetUserProfile, user_in_db, controllers.GetCurrentAuthenticatedUser(pool))
|
|
router.GET("/item/price/current", user_authenticated, middleware.GetUserProfile, user_in_db, controllers.GetCurrentItemPrice(pool))
|
|
|
|
router.POST("/position/create", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.CreatePosition(pool))
|
|
router.POST("/item/create", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.CreateItem(pool))
|
|
router.POST("/order/create", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, controllers.CreateOrder(pool))
|
|
|
|
router.PUT("/user/name", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, controllers.SetUserName(pool))
|
|
router.PUT("/user/promote", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.PromoteUser(pool))
|
|
router.PUT("/user/demote", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.DemoteUser(pool))
|
|
router.PUT("/user/position", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.SetUserPosition(pool))
|
|
router.PUT("/item/price", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.SetItemPrice(pool))
|
|
router.PUT("/order/item", user_authenticated, middleware.GetUserProfile, user_in_db, user_active, controllers.AddItemToOrder(pool))
|
|
|
|
router.DELETE("/user/deactivate", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, user_in_db, user_active, user_is_admin, controllers.DeactivateUser(pool))
|
|
|
|
router.Run("localhost:8080")
|
|
}
|