server

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package server provides a drop-in HTTP handler for a GoMyAdmin admin panel.

Any existing Go HTTP server can mount an admin panel in three steps:

  1. Define your resources with admin.App.
  2. Create an AdminServer with New.
  3. Mount the handler returned by Handler().

Example:

app := admin.New("My App")
app.Resource(User{}).Label("Users").Field("ID").UUID().Primary().Readonly().
    Field("Email").Email().Required().Searchable()

srv, err := server.New(ctx, server.Config{
    DatabaseURL:  os.Getenv("DATABASE_URL"),
    App:          app,
    Authenticate: myAuthFunc,
})
if err != nil { log.Fatal(err) }
defer srv.Close()

mux.Handle("/admin/", srv.Handler())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdminServer

type AdminServer struct {
	// contains filtered or unexported fields
}

AdminServer is a self-contained HTTP handler that serves a GoMyAdmin admin panel. Use New to create one, Handler to get the http.Handler, and Close when done.

func New

func New(ctx context.Context, cfg Config) (*AdminServer, error)

New creates an AdminServer. It connects to PostgreSQL when DatabaseURL is set, creates the sessions table (via auth.PGSessionStore.Migrate) and internal audit/ file tables, then returns a server ready to handle requests.

func (*AdminServer) App

func (s *AdminServer) App() *admin.App

App returns the admin.App resource registry so callers can inspect or add resources after construction (before any requests are served).

func (*AdminServer) Close

func (s *AdminServer) Close()

Close releases the database pool when AdminServer created it (DatabaseURL was used). When you supplied your own Pool, close it yourself — AdminServer will not close it.

func (*AdminServer) Handler

func (s *AdminServer) Handler() http.Handler

Handler returns an http.Handler that serves all admin API routes under /admin/api/. Mount it at your mux root so the /admin/ prefix is routed here:

mux.Handle("/admin/", adminServer.Handler())

The handler is safe to call multiple times; it returns the same router each call.

type Config

type Config struct {
	// DatabaseURL is a PostgreSQL connection string (e.g. "postgres://user:pass@host/db").
	// Mutually exclusive with Pool.
	DatabaseURL string

	// Pool is a pre-existing *pgxpool.Pool.
	// Mutually exclusive with DatabaseURL.
	// The caller remains responsible for closing it; AdminServer.Close() will not close it.
	Pool *pgxpool.Pool

	// App is the resource registry built with admin.New and app.Resource().
	// An empty App is created automatically when nil.
	App *admin.App

	// Authenticate is called on every login attempt.
	// Return (actor, true, nil) on success or (zero, false, nil) on bad credentials.
	// When nil, all login attempts return 401 Unauthorized.
	Authenticate func(ctx context.Context, email, password string) (admin.Actor, bool, error)

	// Tenants is an optional callback that returns the list of tenants the actor
	// may switch to. The response appears in the "me" endpoint under "tenants".
	// When nil, an empty list is returned.
	Tenants func(ctx context.Context, actor admin.Actor) ([]map[string]string, error)

	// Log is the structured logger. Defaults to JSON on stderr.
	Log *slog.Logger

	// UploadDir is the local directory used by the built-in file upload handler.
	// Defaults to "tmp/uploads". Supply a storage.Storage via the Uploads field
	// instead to use S3, R2, or MinIO.
	UploadDir string

	// Uploads overrides the default local file storage adapter.
	// Set this to use S3, R2, or MinIO for file uploads.
	Uploads storage.Storage

	// PublicURL is the externally reachable base URL of this server.
	// Used as the Access-Control-Allow-Origin header value.
	// Defaults to GOMYADMIN_PUBLIC_URL env var, then "http://localhost:8080".
	PublicURL string
	// contains filtered or unexported fields
}

Config holds all options for an AdminServer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL