logadmin

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

README

logadmin

Tests Status Go Report Card PkgGoDev

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/agpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Introduction

Admin interface for github.com/dracory/logstore. Provides a ready-to-use admin panel for viewing, filtering, and deleting application logs.

Modeled after github.com/dracory/shopadmin and github.com/dracory/blogadmin — same folder-per-controller pattern, same UiConfig/UiBase conventions.

Features

  • Log viewing — paginated table with sortable columns
  • Filtering — filter by level, message contains/not-contains, context contains/not-contains, and date range
  • Bulk actions — delete selected logs or delete all logs matching current filters
  • Context viewer — view full log entry including context data
  • Shareable URLs — filter and pagination state preserved in URL parameters
  • Custom layouts — bring your own layout via FuncLayout
  • Bootstrap + Vue CDN — default UI works out of the box

Installation

go get github.com/dracory/logadmin

Quick Start

package main

import (
    "log/slog"
    "net/http"
    "os"

    "github.com/dracory/logadmin"
    "github.com/dracory/logstore"
)

func main() {
    store, err := logstore.NewStore(logstore.NewStoreOptions{
        DB:                 yourDB,
        LogTableName:       "log",
        AutomigrateEnabled: true,
    })
    if err != nil {
        log.Fatal(err)
    }

    admin, err := logadmin.New(logadmin.AdminOptions{
        Store:        store,
        Logger:       slog.New(slog.NewTextHandler(os.Stderr, nil)),
        AdminHomeURL: "/admin",
        LogAdminURL:  "/admin/logs",
    })
    if err != nil {
        log.Fatal(err)
    }

    http.Handle("/admin/logs", http.HandlerFunc(admin.Handle))
    http.ListenAndServe(":8080", nil)
}

See example/ for a complete runnable server with in-memory SQLite and seed data.

Integration with a Router

logadmin.AdminInterface exposes Handle(w, r), which is an http.HandlerFunc-compatible method. Wire it into any router that accepts standard http.Handler:

// stdlib
mux.Handle("/admin/logs", http.HandlerFunc(admin.Handle))

// github.com/dracory/rtr
route := rtr.NewRoute().
    SetName("Admin > Logs").
    SetPath("/admin/logs").
    SetHTMLHandler(admin.Handle)

Custom Layout

By default, logadmin renders a bare-bones HTML page with Bootstrap and Vue from CDN. To embed the admin inside your own layout (branding, menus, etc.), provide FuncLayout:

admin, _ := logadmin.New(logadmin.AdminOptions{
    Store:  store,
    Logger: logger,
    FuncLayout: func(w http.ResponseWriter, r *http.Request, title, body string, opts struct {
        Styles     []string
        StyleURLs  []string
        Scripts    []string
        ScriptURLs []string
    }) string {
        return myLayout(w, r, title, body, opts)
    },
})

The anonymous struct matches shopadmin/blogadmin exactly, so you can reuse your existing shopadmin/blogadmin layout function.

Testing

go test ./...

Tests use an in-memory SQLite database via modernc.org/sqlite — no external services required.

Documentation

Overview

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

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

Index

Constants

This section is empty.

Variables

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

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

Common errors

Functions

This section is empty.

Types

type AdminInterface

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

AdminInterface defines the interface for the log admin

func New

func New(opts AdminOptions) (AdminInterface, error)

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

type AdminOptions

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

	// Logger is required
	Logger *slog.Logger

	// FuncLayout is an optional function to render the admin interface
	// inside your own layout (branding, menus, etc.). It receives the
	// request and response writer so the host project can access request
	// context (auth user, locale, etc.) when rendering the layout.
	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

	// LogAdminURL is the base URL for log admin (default: "/admin/logs")
	LogAdminURL 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 log admin.

Store and Logger replace the in-repo version's Registry field (which was app.AppInterface). This matches the shopadmin/blogadmin convention where stores are passed directly.

FuncLayout is an optional function to render the admin interface inside your own layout (branding, menus, etc.). It receives the request and response writer so the host project can access request context (auth user, locale, etc.) when rendering the layout. If nil, a default bare-bones HTML page is used (Bootstrap + Vue CDN). Uses anonymous struct to match shopadmin/blogadmin exactly, so consumers can reuse their shopadmin/blogadmin layout function for logadmin.

Directories

Path Synopsis
Example logadmin server.
Example logadmin server.

Jump to

Keyboard shortcuts

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