Add dynamic admin panel for managing entities (#108)

This commit is contained in:
Mike Stefanello 2025-04-22 08:26:35 -04:00 committed by GitHub
parent 60009df0bf
commit 1a6874fd82
47 changed files with 2173 additions and 320 deletions

63
cmd/admin/main.go Normal file
View file

@ -0,0 +1,63 @@
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/services"
)
// main creates a new admin user with the email passed in via the flag.
func main() {
// Start a new container.
c := services.NewContainer()
defer func() {
// Gracefully shutdown all services.
if err := c.Shutdown(); err != nil {
log.Default().Error("shutdown failed", "error", err)
}
}()
var email string
flag.StringVar(&email, "email", "", "email address for the admin user")
flag.Parse()
if len(email) == 0 {
invalid("email is required")
}
// Generate a password.
pw, err := c.Auth.RandomToken(10)
if err != nil {
invalid("failed to generate a random password")
}
// Create the admin user.
err = c.ORM.User.
Create().
SetEmail(email).
SetName("Admin").
SetAdmin(true).
SetVerified(true).
SetPassword(pw).
Exec(context.Background())
if err != nil {
invalid(err.Error())
}
fmt.Println("")
fmt.Println("-- ADMIN USER CREATED --")
fmt.Printf("Email: %s\n", email)
fmt.Printf("Password: %s\n", pw)
fmt.Println("----")
fmt.Println("")
}
func invalid(msg string) {
fmt.Printf("[ERROR] %s\n", msg)
os.Exit(1)
}