feat: user CRUD
This commit is contained in:
74
api/main.go
Normal file
74
api/main.go
Normal file
@@ -0,0 +1,74 @@
|
||||
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"))
|
||||
log.Printf("%s", pool)
|
||||
|
||||
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))
|
||||
|
||||
gob.Register(map[string]interface{}{})
|
||||
router.GET("/", middleware.IsAuthenticated(authenticator), 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("/public", controllers.PublicEndpoint)
|
||||
|
||||
router.POST("/user/create", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), controllers.CreateUser(pool))
|
||||
|
||||
router.PUT("/user/promote", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), middleware.IsAdmin(pool), controllers.PromoteUser(pool))
|
||||
router.PUT("/user/demote", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), middleware.IsAdmin(pool), controllers.DemoteUser(pool))
|
||||
router.DELETE("/user/deactivate", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), middleware.IsAdmin(pool), controllers.DeactivateUser(pool))
|
||||
|
||||
router.Run("localhost:8080")
|
||||
}
|
||||
Reference in New Issue
Block a user