settingadmin

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

settingadmin

Standalone admin panel for dracory/settingstore, following the same architecture as shopadmin/logadmin/blogadmin (folder-per-controller, embeddable, layout-agnostic).

Features

  • Grouped settings editor — define groups and fields in code; each group renders as a card with its own Save button. Values persist in the settingstore keyed by field name.
  • Field templates — every field type renders through its own embedded template (templates/fields/field_<type>.html), so adding or restyling a type means touching one file.
  • Raw key/value manager — paginated, filterable table for arbitrary settings with save/delete (Vue 3 frontend, ?controller=manager).
  • Template overrides — AdminOptions.FieldTemplates lets the host app override existing types or register new ones.

Screenshots

Grouped editor (?controller=settings) — schema-driven cards, per-group Save, unsaved-changes indicator:

Grouped settings editor

Raw manager (?controller=manager) — key/value table with filter, add, save, delete:

Raw settings manager

Quick start

go get github.com/dracory/settingadmin
store, _ := settingstore.NewStore(settingstore.NewStoreOptions{
    DB: db, SettingTableName: "setting", AutomigrateEnabled: true,
})

admin, _ := settingadmin.New(settingadmin.AdminOptions{
    Store:  store,
    Logger: logger,
    Groups: []settingadmin.SettingGroup{
        {
            ID:    "general",
            Title: "General",
            Fields: []settingadmin.Field{
                {Name: "site.name", Label: "Site Name", Type: settingadmin.FIELD_TYPE_INPUT},
                {Name: "site.maintenance", Label: "Maintenance", Type: settingadmin.FIELD_TYPE_CHECKBOX},
                {Name: "shop.currency", Label: "Currency", Type: settingadmin.FIELD_TYPE_SELECT,
                    Options: []settingadmin.FieldOption{{Value: "USD", Label: "US Dollar"}}},
            },
        },
    },
})

mux.Handle("/admin/settings", http.HandlerFunc(admin.Handle))
mux.Handle("/admin/settings/", http.HandlerFunc(admin.Handle))
  • /admin/settings → grouped editor (?controller=settings)
  • /admin/settings?controller=manager → raw key/value manager

Field types

input, textarea, select, checkbox, number, date, datetime, email, password, url, tel, color, radio, hidden, htmlarea, file, image, raw — each backed by templates/fields/field_<type>.html. Unknown types render via field_fallback.html.

To add/override a type:

settingadmin.New(settingadmin.AdminOptions{
    ...
    FieldTemplates: map[string]string{
        "markdown": `<textarea name="{{.Field.Name}}">{{.Value}}</textarea>`,
    },
})

Example

go run ./example   # serves http://localhost:8080, admin at /admin/settings

Testing

go test ./...      # unit tests (in-memory SQLite)
go test ./e2e      # chromedp e2e (skips when no Chrome available)

License

AGPL-3.0. Commercial license available — see LICENSE_COMMERCIAL.txt.

Documentation

Overview

Package settingadmin provides a standalone settings admin interface following the folder-per-controller pattern. Each controller is in its own subfolder and handles its own views and AJAX data.

It supports two controllers:

  • "settings" — a grouped editor driven by a declarative schema (AdminOptions.Groups); each field type renders through an embedded template file (templates/fields/field_<type>.html).
  • "manager" — a raw key/value table for arbitrary settings.

This module is modeled on github.com/dracory/logadmin and github.com/dracory/shopadmin.

Index

Constants

View Source
const (
	FIELD_TYPE_INPUT    = shared.FIELD_TYPE_INPUT
	FIELD_TYPE_TEXTAREA = shared.FIELD_TYPE_TEXTAREA
	FIELD_TYPE_SELECT   = shared.FIELD_TYPE_SELECT
	FIELD_TYPE_CHECKBOX = shared.FIELD_TYPE_CHECKBOX
	FIELD_TYPE_NUMBER   = shared.FIELD_TYPE_NUMBER
	FIELD_TYPE_DATE     = shared.FIELD_TYPE_DATE
	FIELD_TYPE_DATETIME = shared.FIELD_TYPE_DATETIME
	FIELD_TYPE_EMAIL    = shared.FIELD_TYPE_EMAIL
	FIELD_TYPE_PASSWORD = shared.FIELD_TYPE_PASSWORD
	FIELD_TYPE_URL      = shared.FIELD_TYPE_URL
	FIELD_TYPE_TEL      = shared.FIELD_TYPE_TEL
	FIELD_TYPE_COLOR    = shared.FIELD_TYPE_COLOR
	FIELD_TYPE_RADIO    = shared.FIELD_TYPE_RADIO
	FIELD_TYPE_HIDDEN   = shared.FIELD_TYPE_HIDDEN
	FIELD_TYPE_HTMLAREA = shared.FIELD_TYPE_HTMLAREA
	FIELD_TYPE_FILE     = shared.FIELD_TYPE_FILE
	FIELD_TYPE_IMAGE    = shared.FIELD_TYPE_IMAGE
	FIELD_TYPE_RAW      = shared.FIELD_TYPE_RAW
	FIELD_TYPE_FALLBACK = shared.FIELD_TYPE_FALLBACK
)

Field type constants. Each type maps to an embedded template file templates/fields/field_<type>.html. Unknown types fall back to field_fallback.html.

Variables

View Source
var (
	// ErrStoreRequired is returned when Store is not provided
	ErrStoreRequired = errors.New("store is required")

	// ErrLoggerRequired is returned when Logger is not provided
	ErrLoggerRequired = errors.New("logger is required")
)

Functions

This section is empty.

Types

type AdminInterface

type AdminInterface interface {
	Handle(w http.ResponseWriter, r *http.Request)
}

AdminInterface defines the interface for the setting admin

func New

func New(opts AdminOptions) (AdminInterface, error)

New creates a new setting admin instance. Returns ErrStoreRequired if Store is nil, ErrLoggerRequired if Logger is nil.

type AdminOptions

type AdminOptions struct {
	// Store is the settingstore.StoreInterface (required)
	Store settingstore.StoreInterface

	// Logger is required
	Logger *slog.Logger

	// Groups is the declarative schema for the grouped settings editor.
	// Each group's fields are rendered via the FieldRegistry templates.
	// Optional — when empty, the "manager" controller becomes default.
	Groups []SettingGroup

	// FieldTemplates overrides or extends the embedded field templates.
	// Key = field type (e.g. "input", "mytype"), value = template text.
	FieldTemplates map[string]string

	// FuncLayout is an optional function to render the admin interface
	// inside your own layout (branding, menus, etc.). If nil, a default
	// bare-bones HTML page is used (Bootstrap + Vue CDN).
	FuncLayout func(w http.ResponseWriter, r *http.Request, title string, body string, options struct {
		Styles     []string
		StyleURLs  []string
		Scripts    []string
		ScriptURLs []string
	}) string

	// AdminHomeURL is the URL for the admin home page (default: "/admin")
	AdminHomeURL string

	// SettingAdminURL is the base URL for setting admin
	// (default: "/admin/settings")
	SettingAdminURL string

	// FileManagerURL is the URL for the file manager (optional)
	FileManagerURL string

	// AuthUserID returns the authenticated user ID from the request.
	// If it returns "", the user is treated as unauthenticated.
	AuthUserID func(r *http.Request) string
}

AdminOptions contains all dependencies and configuration for the setting admin.

type Field

type Field = shared.Field

Field describes a single editable setting. Field.Name is the key used to persist the value in the settingstore. Field.DefaultValue is used when the store has no value for the key. Alias of shared.Field.

type FieldOption

type FieldOption = shared.FieldOption

FieldOption represents a single selectable option for select, radio, and checkbox fields. Alias of shared.FieldOption.

type FieldRegistry

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

FieldRegistry renders setting fields using one template file per field type. Templates are embedded under templates/fields/field_<type>.html. Host applications can override existing types or register new ones via AdminOptions.FieldTemplates.

func NewFieldRegistry

func NewFieldRegistry(overrides map[string]string) (*FieldRegistry, error)

NewFieldRegistry parses the embedded field templates and merges any overrides (key = field type, value = template text).

func (*FieldRegistry) HasType

func (r *FieldRegistry) HasType(fieldType string) bool

HasType reports whether a template exists for the given field type.

func (*FieldRegistry) Render

func (r *FieldRegistry) Render(field shared.Field, value string) (string, error)

Render renders a single field with the given current value. Unknown types fall back to the fallback template, or a plain text input template when no fallback is registered.

func (*FieldRegistry) Types

func (r *FieldRegistry) Types() []string

Types returns the registered field type names.

type SettingGroup

type SettingGroup = shared.SettingGroup

SettingGroup groups related fields under a titled card in the admin UI. Alias of shared.SettingGroup.

Directories

Path Synopsis
Example settingadmin server.
Example settingadmin server.

Jump to

Keyboard shortcuts

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