admin

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package admin provides Oni Admin — an auto-generated CRUD management panel. Resources are registered with their model type and the panel generates list, create, edit, and delete routes automatically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authorizer

type Authorizer func(r *http.Request) bool

Authorizer decides whether a request may access the admin panel. It runs before every admin route (HTML and JSON API). Return true to allow.

type Option

type Option func(*Panel)

Option configures the panel.

func WithAuth

func WithAuth(fn Authorizer) Option

WithAuth sets the authorizer that guards every admin route. Without it the panel denies all requests.

admin.New(db, admin.WithAuth(func(r *http.Request) bool {
    return guard.IsAdmin(r) // your auth check
}))

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the URL prefix (default: "/admin").

func WithTitle

func WithTitle(title string) Option

WithTitle sets the panel title.

type Panel

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

Panel is the Oni Admin panel.

func New

func New(db *database.DB, opts ...Option) *Panel

New creates an Admin panel bound to the given DB.

The panel exposes full read/write/delete access to every registered table, so it MUST be protected by an Authorizer (see WithAuth). If none is configured the panel fails closed: every route returns 403 until WithAuth is provided. This prevents an unauthenticated admin panel from ever shipping by accident.

func (*Panel) APIHandler

func (p *Panel) APIHandler() http.Handler

APIHandler returns a simple REST JSON API for all registered resources. Mount at a separate prefix for headless admin API access.

func (*Panel) Handler

func (p *Panel) Handler() http.Handler

Handler returns an http.Handler for the admin panel. Mount it on your router at the panel prefix.

func (*Panel) Register

func (p *Panel) Register(r *Resource)

Register adds a model resource to the admin panel.

type Resource

type Resource struct {
	// Name is the human-readable resource name (e.g. "User").
	Name string
	// Slug is the URL segment (e.g. "users"). Defaults to lowercase Name + "s".
	Slug string
	// Model is a pointer to the model struct (for reflection).
	Model any
	// Columns lists which fields to show in the list view. Empty = all exported.
	Columns []string
	// Searchable is the column name to use for search (default: first string col).
	Searchable string
	// PerPage controls pagination (default: 25).
	PerPage int
	// Writable lists the columns that may be set from form input on create and
	// edit. Empty = all Columns except the primary key ("id"). The primary key
	// is never written from form input regardless of this list — on edit the
	// record is addressed by the URL id only.
	Writable []string
}

Resource describes a model that the admin panel should expose.

Jump to

Keyboard shortcuts

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