feat: jwt token generation (todo: create jwt validation middleware)

This commit is contained in:
hexlocation 2025-05-05 23:04:50 +02:00
parent d8caef7e5d
commit b28b719b51
9 changed files with 86 additions and 29 deletions

49
main.go
View file

@ -2,6 +2,7 @@ package main
import (
"errors"
"fmt"
"log"
"os"
@ -23,6 +24,13 @@ func getEnv(key, fallback string) string {
return fallback
}
func requireEnv(key string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
panic(errors.New(fmt.Sprintf("Environment variable %s is required but not specified. Exiting...", key)))
}
func main() {
_ = godotenv.Load()
@ -42,39 +50,40 @@ func main() {
Router: gin.Default(),
ImagePath: imagePath,
Client: client.New(
os.Getenv("REDIRECT_URI"),
os.Getenv("CLIENT_ID"),
os.Getenv("CLIENT_SECRET"),
requireEnv("REDIRECT_URI"),
requireEnv("CLIENT_ID"),
requireEnv("CLIENT_SECRET"),
),
JWTSecret: requireEnv("JWT_SECRET"),
}
switch databaseType {
case "sqlite":
db, err := gorm.Open(sqlite.Open(sqliteFile), &gorm.Config{})
case "sqlite":
db, err := gorm.Open(sqlite.Open(sqliteFile), &gorm.Config{})
c.Database = db
c.Database = db
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
break
break
case "postgres":
db, err := gorm.Open(postgres.Open(os.Getenv("POSTGRES_DSN")), &gorm.Config{})
case "postgres":
db, err := gorm.Open(postgres.Open(requireEnv("POSTGRES_DSN")), &gorm.Config{})
c.Database = db
c.Database = db
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
break
default:
panic(errors.New("Invalid database type was specified."))
break
default:
panic(errors.New("Invalid database type was specified."))
}
c.Database.AutoMigrate(&auth.User{})
c.Database.AutoMigrate(&auth.User{})
api.Register(&c)