feat: add independent Bell authentication and RBAC (#12)

This commit is contained in:
QiuSW
2026-08-12 17:44:31 +08:00
parent a5d846a437
commit a5fc8f5490
28 changed files with 840 additions and 22 deletions
+32 -3
View File
@@ -8,10 +8,15 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"git.ilapage.cn/ila/yovision/Bell/server/app/audit"
"git.ilapage.cn/ila/yovision/Bell/server/app/auth"
"git.ilapage.cn/ila/yovision/Bell/server/config"
"git.ilapage.cn/ila/yovision/Bell/server/internal/platform"
"git.ilapage.cn/ila/yovision/Bell/server/migrations"
)
const Version = "0.1.0"
@@ -25,8 +30,8 @@ func Run(ctx context.Context, args []string) error {
fmt.Println(Version)
return nil
}
if command != "serve" && command != "migrate" {
return fmt.Errorf("unknown command %q (use serve, migrate, or version)", command)
if command != "serve" && command != "migrate" && command != "create-admin" {
return fmt.Errorf("unknown command %q (use serve, migrate, create-admin, or version)", command)
}
cfg, err := config.Load()
if err != nil {
@@ -37,13 +42,28 @@ func Run(ctx context.Context, args []string) error {
return err
}
defer db.Close()
if err := platform.Migrate(ctx, db); err != nil {
if err := migrations.Apply(ctx, db); err != nil {
return err
}
if command == "migrate" {
return nil
}
auditStore := audit.Store{DB: db}
authStore := auth.Store{DB: db}
authService := auth.Service{Store: authStore, Audit: auditStore, Secret: []byte(cfg.SessionSecret), SessionTTL: 8 * time.Hour}
if command == "create-admin" {
username, displayName := argument(args, "--username"), argument(args, "--display-name")
password := os.Getenv("BELL_BOOTSTRAP_PASSWORD")
if displayName == "" {
displayName = username
}
if password == "" {
return fmt.Errorf("BELL_BOOTSTRAP_PASSWORD is required for create-admin")
}
return authService.BootstrapAdministrator(ctx, username, displayName, password)
}
app := platform.NewHTTPApp(db)
auth.HTTP{Service: authService, Store: authStore, Audit: auditStore, SecureCookie: cfg.CookieSecure}.Register(app.Router())
server := &http.Server{Addr: cfg.HTTPAddress, Handler: app.Handler()}
serverCtx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
@@ -59,3 +79,12 @@ func Run(ctx context.Context, args []string) error {
}
return nil
}
func argument(args []string, name string) string {
for i := 1; i < len(args)-1; i++ {
if strings.EqualFold(args[i], name) {
return args[i+1]
}
}
return ""
}