bookmarks

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

README

Bookmarks Module

A custom module example demonstrating the oCMS module system. Provides bookmark management with favorites, CRUD operations, and a standalone admin interface.

Features

  • Public JSON API for listing bookmarks
  • Admin dashboard with embedded HTML template
  • Bookmark CRUD (create, list, toggle favorite, delete)
  • Template functions for use in themes
  • Hook handler for page save events
  • Database migrations with rollback support
  • Embedded i18n translations (English, Russian)
  • Self-registration via init() pattern

Routes

Public Routes
Method Path Description
GET /bookmarks List all bookmarks (JSON)

Response format:

{
    "bookmarks": [
        {
            "id": 1,
            "title": "Go Documentation",
            "url": "https://go.dev",
            "description": "Official Go docs",
            "is_favorite": true,
            "created_at": "2026-01-15T10:30:00Z"
        }
    ],
    "total": 1
}
Admin Routes
Method Path Description
GET /admin/bookmarks Admin dashboard
POST /admin/bookmarks Create bookmark
POST /admin/bookmarks/{id}/toggle Toggle favorite
DELETE /admin/bookmarks/{id} Delete bookmark

Template Functions

Available in all theme templates when the module is active:

<!-- Total bookmark count -->
<p>You have {{ bookmarkCount }} bookmarks saved.</p>

<!-- List favorite bookmarks -->
{{ range bookmarkFavorites }}
    <a href="{{ .URL }}">{{ .Title }}</a>
{{ end }}
Function Returns Description
bookmarkCount int Total number of bookmarks
bookmarkFavorites []Bookmark All bookmarks marked favorite

Hook Handlers

Hook Handler Name Priority Description
page.after_save bookmarks_page_saved 20 Logs page save events

The hook handler demonstrates how custom modules can react to page lifecycle events. In practice, this could be used to auto-bookmark pages, send notifications, or trigger external integrations.

Database Schema

CREATE TABLE bookmarks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    url TEXT NOT NULL,
    description TEXT DEFAULT '',
    is_favorite BOOLEAN NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

Module Structure

custom/modules/bookmarks/
├── module.go          # Module definition, lifecycle, hooks, migrations
├── handlers.go        # HTTP handlers and database operations
├── register.go        # init() self-registration
├── bookmarks_test.go  # Comprehensive test suite
├── README.md          # This file
├── templates/
│   └── admin.html     # Embedded admin dashboard template
└── locales/
    ├── en/messages.json
    └── ru/messages.json

Self-Registration

The module uses Go's init() pattern for auto-registration:

// register.go
package bookmarks

import "github.com/olegiv/ocms-go/internal/module"

func init() {
    module.RegisterCustomModule(New())
}

Enabled by a blank import in custom/modules/imports.go:

import _ "github.com/olegiv/ocms-go/custom/modules/bookmarks"

Internationalization

Translations are embedded in locales/{lang}/messages.json and loaded automatically by the module registry.

Supported languages:

  • English (en)
  • Russian (ru)

Translation keys follow the bookmarks.* namespace convention. Add new languages by creating locales/{lang}/messages.json.

Testing

OCMS_SESSION_SECRET=test-secret-key-32-bytes-long!!! go test -v ./custom/modules/bookmarks/...

The test suite covers:

  • Module metadata (name, version, description, admin URL, sidebar label)
  • Database migrations (up and down)
  • CRUD operations (create, list, toggle favorite, delete)
  • Template functions (with and without data)
  • Hook registration and execution
  • HTTP handlers (public API, admin dashboard, create, toggle, delete)
  • Translations filesystem embedding
  • Error handling (not found conditions)

Documentation

Overview

Package bookmarks provides a custom bookmarks module for oCMS. It demonstrates how to create a fully self-contained custom module in the custom/modules/ directory with database migrations, embedded admin template, public routes, template functions, hooks, and i18n translations.

The module self-registers via init() in register.go. To enable it, add a blank import to custom/modules/imports.go:

_ "github.com/olegiv/ocms-go/custom/modules/bookmarks"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bookmark

type Bookmark struct {
	ID                 int64     `json:"id"`
	Title              string    `json:"title"`
	URL                string    `json:"url"`
	Description        string    `json:"description"`
	IsFavorite         bool      `json:"is_favorite"`
	CreatedAt          time.Time `json:"created_at"`
	CreatedAtFormatted string    `json:"-"`
}

Bookmark represents a saved bookmark.

type Module

type Module struct {
	module.BaseModule
	// contains filtered or unexported fields
}

Module implements the module.Module interface for bookmarks.

func New

func New() *Module

New creates a new instance of the bookmarks module.

func (*Module) AdminURL

func (m *Module) AdminURL() string

AdminURL returns the admin dashboard URL for the module.

func (*Module) Init

func (m *Module) Init(ctx *module.Context) error

Init initializes the module with the given context.

func (*Module) Migrations

func (m *Module) Migrations() []module.Migration

Migrations returns database migrations for the module.

func (*Module) RegisterAdminRoutes

func (m *Module) RegisterAdminRoutes(r chi.Router)

RegisterAdminRoutes registers admin routes for the module.

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(r chi.Router)

RegisterRoutes registers public routes for the module.

func (*Module) Shutdown

func (m *Module) Shutdown() error

Shutdown performs cleanup when the module is shutting down.

func (*Module) SidebarLabel

func (m *Module) SidebarLabel() string

SidebarLabel returns the display label for the admin sidebar.

func (*Module) TemplateFuncs

func (m *Module) TemplateFuncs() template.FuncMap

TemplateFuncs returns template functions provided by the module.

func (*Module) TranslationsFS

func (m *Module) TranslationsFS() embed.FS

TranslationsFS returns the embedded filesystem containing module translations.

Jump to

Keyboard shortcuts

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