caddypocketbase

package module
v0.0.0-...-2d6216b Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

README

Caddy PocketBase Module

A Caddy module that integrates PocketBase(Repository) as a Caddy application, allowing you to run PocketBase embedded in your Caddy server.

Features

  • Run PocketBase as a native Caddy module
  • Admin API endpoints for managing superusers
  • Configurable data directory and origins
  • Automatic port allocation if none specified
  • Full integration with Caddy's configuration and lifecycle

Configuration

Example Caddyfile configuration:

{
    order pocketbase before file_server
}
example.com {
    pocketbase
}

The embedded pocketbase app can be customized (with limitations) as follows:

{
	pocketbase {
		# optional: port pocketbase will listen to; defaults to randomly allocated port.
		listen 8080
		
		# optional: default data directory; defaults to ./pb_data.
		data_dir /var/pb

		# optional: all origins allowed when empty; defaults to all origins.
		origins example.com example.net localhost
	}
	order pocketbase before file_server
}

example.com {
	pocketbase
}

Key Components

  • PocketBase Integration: Runs PocketBase within Caddy.
  • Admin API: Provides endpoints for superuser management.
  • Configuration: Allows customization of data directory and origins.

Usage

This module enables you to run PocketBase as part of your Caddy server, simplifying deployment and management of both services. The configuration options allow for easy customization to fit various deployment scenarios.

Admin API Endpoints

The module provides admin API endpoints under /pocketbase/:

  • POST /pocketbase/superuser - Create a new superuser
  • PUT /pocketbase/superuser - Upsert a superuser
  • PATCH /pocketbase/superuser - Update superuser password
  • DELETE /pocketbase/superuser - Delete a superuser
  • POST /pocketbase/superuser/{email}/otp - Generate OTP for superuser

All the above endpoints require a JSON payload, except for the OTP endpoint. The JSON payload for the superuser endpoints is as follows:

{
		"email_address": "...",
		"password": "..."
}

The DELETE endpoint does not expect the password field.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// The listen address of the PocketBase server. If empty, a free port on
	// 127.0.0.1 will be used.
	Listen string `json:"listen,omitempty"`

	// The data directory of PocketBase. If empty, a directory named `pb_data` in
	// the Caddy data directory will be used. Refer to [Caddy data directory](https://caddyserver.com/docs/conventions#data-directory)
	// for more information.
	DataDir string `json:"data_dir,omitempty"`

	// The allowed origins for the PocketBase server with respect to CORS.
	// If empty, all origins are allowed.
	Origins []string `json:"origins,omitempty"`
	// contains filtered or unexported fields
}

App is a Caddy module that provides an embedded PocketBase server.

The module provides admin API endpoints under `/pocketbase/`:

- `POST /pocketbase/superuser` - Create a new superuser - `PUT /pocketbase/superuser` - Upsert a superuser - `PATCH /pocketbase/superuser` - Update superuser password - `DELETE /pocketbase/superuser` - Delete a superuser - `POST /pocketbase/superuser/{email}/otp` - Generate OTP for superuser

All the above endpoints require a JSON payload, except for the OTP endpoint. The JSON payload for the superuser endpoints is as follows:

{
	"email_address": "...",
	"password": "..."
}

The `DELETE` endpoint does not expect the `password` field.

Although PocketBase prints a URL in the logs to create the first superuser, the host part of the URL is not correct. You can either replace the host part with the host defined in your Caddy configuration, or use the admin API endpoint to create the first superuser.

The app can be configured in the Caddyfile through the `pocketbase` block in the global options section. Syntax:

pocketbase {
    data_dir <path>
    listen   <addr>
    origins  <origin...>
}

If the block is omitted, the default values are used.

func (*App) CaddyModule

func (a *App) CaddyModule() caddy.ModuleInfo

CaddyModule implements caddy.Module.

func (*App) Provision

func (a *App) Provision(ctx caddy.Context) error

Provision sets up the needs of the PocketBase server. If `data_dir` is not set, a directory named `pb_data` is created in the Caddy data directory. If `listen` is not set, a free port on 127.0.0.1 is used. The PocketBase app is bootstrapped.

func (*App) Start

func (a *App) Start() error

Start starts the PocketBase server in a goroutine

func (*App) Stop

func (a *App) Stop() error

Stop implements caddy.App.

type Handler

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

Handler implements an HTTP handler that proxies requests internally to a PocketBase server. It can be used in conjunction with the PocketBase app. If the PocketBase app is not explicitly configured, a PocketBase app with default config is used.

func (*Handler) CaddyModule

func (h *Handler) CaddyModule() caddy.ModuleInfo

CaddyModule implements caddy.Module.

func (*Handler) Provision

func (h *Handler) Provision(ctx caddy.Context) error

Provision implements caddy.Provisioner.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request, _ caddyhttp.Handler) error

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*Handler) UnmarshalCaddyfile

func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

Jump to

Keyboard shortcuts

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