Files
Docwell/Server/cmd/docwell-server/main.go
T
tajniak81andClaude Opus 5.5 03f3434c10 Add organizations with cascading user rights
Same model as DriverVault, with organizations above workspaces:

- users get a role (user, admin, superadmin) and at most one
  organization; existing instance admins become superadmins
- rights cascade: superadmins own everything, org admins own their
  organization's workspaces, others get their workspace role
  (owner > admin > member > guest) while they stay in the org
- nobody grants a role above their own, acts on someone ranked above
  them, or changes their own role; rules live in internal/access as
  pure, tested functions
- self-service organizations: an org-less user creates one and becomes
  its admin; the last admin can delete it and is demoted
- endpoints for users, orgs, workspaces and members; token cache is
  dropped when a user's rights change
- admin panel: Users and Organizations pages scoped to the caller
- web app: organization page, workspace list, workspace settings with
  member management (EN/PL)
- fix: organization name missing after login/refresh, and stale tokens
  redirecting public pages to sign-in

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 22:53:11 +01:00

81 lines
1.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Command docwell-server runs the Docwell API, collaboration relay and admin panel.
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"docwell/server/internal/admin"
"docwell/server/internal/auth"
"docwell/server/internal/config"
"docwell/server/internal/directory"
"docwell/server/internal/httpapi"
"docwell/server/internal/store/pocketbase"
)
// Version is set at build time with -ldflags "-X main.Version=...".
var Version = "dev"
func main() {
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
if err := run(log); err != nil {
log.Error("fatal", "err", err)
os.Exit(1)
}
}
func run(log *slog.Logger) error {
cfg, err := config.Load()
if err != nil {
return err
}
db := pocketbase.New(cfg.PocketBaseURL, cfg.PocketBaseEmail, cfg.PocketBasePassword)
authSvc := auth.NewService(db, auth.Options{SignupEnabled: cfg.SignupEnabled, SignupDomains: cfg.SignupDomains})
dirSvc := directory.New(db, authSvc.Forget)
srv := &http.Server{
Addr: cfg.Addr,
Handler: httpapi.NewRouter(httpapi.Deps{
Version: Version,
CORSOrigins: cfg.CORSOrigins,
DB: db,
Auth: authSvc,
Directory: dirSvc,
Admin: admin.Handler("/admin"),
Log: log,
}),
ReadHeaderTimeout: 10 * time.Second,
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
errCh := make(chan error, 1)
go func() {
log.Info("docwell-server listening", "addr", cfg.Addr, "version", Version, "pocketbase", cfg.PocketBaseURL)
errCh <- srv.ListenAndServe()
}()
select {
case err := <-errCh:
if !errors.Is(err, http.ErrServerClosed) {
return err
}
case <-ctx.Done():
log.Info("shutting down")
}
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownTimeout)
defer cancel()
return srv.Shutdown(shutdownCtx)
}