Gotify Server

repository·master·Indexed 12 days ago

https://github.com/gotify/server

A self-hosted server for sending and receiving real-time messages via REST-API and WebSockets. It serves as a lightweight alternative to commercial push services, featuring a Web-UI, Android app, and CLI. Key capabilities include client management via ClientAPI, WebSocket stream connections, and extensibility through plugins. Supports configuration via environment variables for HTTP/HTTPS settings, Let's Encrypt TLS, OIDC SSO, and multiple database dialects including sqlite3, mysql, and postgres.

Tokens
7.3K
Snippets
30
Records
33
Agent score
97%

What's inside Gotify

  1. Overview of Gotify Server features

    master

    Gotify is a self-hosted server designed for sending and receiving real-time messages. It provides a simple infrastructure for push notifications that can be integrated into various workflows.

    Key capabilities include:

    • Message Transmission: Send messages via a REST-API and receive them in real-time using WebSockets.
    • Management: Full control over users, clients, and applications.
    • Extensibility: Support for Plugins.
    • Ecosystem: Includes a Web-UI, a dedicated CLI for sending messages (gotify/cli), and an Android application.
  2. Manage Gotify clients via ClientAPI

    master

    The ClientAPI struct provides HTTP handlers for managing Gotify clients (applications/tokens). It requires a ClientDatabase implementation for persistence and an ImageDir string. It also supports a NotifyDeleted callback to trigger actions when a client is removed.

    Key operations available through the API include:

    • Create a client: Generates a new client with a unique token.
    • Update a client: Modifies existing client properties like name or inactivity expiration.
    • List clients: Retrieves all clients associated with the authenticated user.
    • Delete a client: Removes a client (requires elevated authentication).
    • Elevate a client: Extends the session's elevated privileges for a specified duration.
  3. Configure Gotify server via environment variables

    master

    Gotify can be configured using environment variables. You can save these in a file named gotify-server.env in your working directory, or export them directly to your shell.

    Configuration Loading Order

    If the $GOTIFY_CONFIG_FILE environment variable is set, Gotify loads that file exclusively. If it is unset, Gotify searches for configuration files in this order:

    1. gotify-server.env (in the working directory)
    2. $XDG_CONFIG_HOME/gotify/gotify-server.env (defaults to $HOME/.config/gotify/gotify-server.env)
    3. /etc/gotify/server.env

    Note: Variables already exported in the process environment always take precedence over values loaded from these files.

    Value Types

    • text: A plain string.
    • number: An integer.
    • boolean: true or false.
    • text-list: A comma-separated list (e.g., a,b,c). Use quotes to include commas in a single entry (e.g., "a,b",c).
    • json-map: A JSON object mapping strings to strings (e.g., {"X-Foo":"bar"}).

    Secret Management

    Every variable supports a _FILE suffix to read the value from a file path. This is recommended for Docker or Kubernetes secrets. Example: GOTIFY_DEFAULTUSER_PASS_FILE=/run/secrets/admin_pass

  4. Implement a Gotify plugin

    master

    To create a Gotify plugin, you must implement the plugin.Plugin interface and provide a factory function named NewGotifyPluginInstance.

    Key requirements:

    1. GetGotifyPluginInfo() plugin.Info: An exported function that returns metadata about the plugin, including its Name, Description, and ModulePath.
    2. NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin: An exported factory function that initializes and returns a new instance of your plugin implementation for a specific user context.
    3. plugin.Plugin Interface: Your plugin struct must implement:
      • Enable() error: Logic to start the plugin's background tasks or services.
      • Disable() error: Logic to gracefully shut down the plugin.
      • SetMessageHandler(h plugin.MessageHandler): A method to receive the message handler provided by the Gotify server, allowing the plugin to send messages.

    Note: The main function should not contain logic; the resulting binary is intended to be loaded as a Go plugin.

    import (
    	"github.com/gotify/plugin-api"
    )
    
    // 1. Provide metadata
    func GetGotifyPluginInfo() plugin.Info {
    	return plugin.Info{
    		Name:        "clock",
    		Description: "Sends an hourly reminder",
    		ModulePath:  "github.com/gotify/server/v2/example/clock",
    	}
    }
    
    // 2. Implement the Plugin interface
    type Plugin struct {
    	msgHandler  plugin.MessageHandler
    	enabled     bool
    	cronHandler *cron.Cron
    }
    
    func (c *Plugin) Enable() error {
    	// Start background tasks
    	return nil
    }
    
    func (c *Plugin) Disable() error {
    	// Stop background tasks
    	return nil
    }
    
    func (c *Plugin) SetMessageHandler(h plugin.MessageHandler) {
    	c.msgHandler = h
    }
    
    // 3. Provide the factory function
    func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
    	return &Plugin{}
    }
  5. Configure OpenID Connect (OIDC) SSO

    master

    Enable Single Sign-On via an external identity provider (e.g., Authelia, Dex, Keycloak). The provider must support PKCE.

    VariableTypeDescription
    GOTIFY_OIDC_ENABLEDbooleanEnable OIDC authentication.
    GOTIFY_OIDC_ISSUERtextBase URL of the identity provider (discovery endpoint).
    GOTIFY_OIDC_CLIENTIDtextClient ID registered with the provider.
    GOTIFY_OIDC_CLIENTSECRETtextClient secret paired with the ID.
    GOTIFY_OIDC_REDIRECTURLtextCallback URL. Must end with /auth/oidc/callback. For Android app support, also register gotify://oidc/callback at your provider.
    GOTIFY_OIDC_AUTOREGISTERbooleanIf true, automatically creates a local user on first login.
    GOTIFY_OIDC_LINK_BY_USERNAMEbooleanIf true, binds OIDC identity to an existing local user with a matching username.
    GOTIFY_OIDC_USERNAMECLAIMtextThe OIDC ID-token claim used as the local username (e.g., email or preferred_username).
    GOTIFY_OIDC_SCOPEStext-listOIDC scopes to request (e.g., openid,profile,email).
    GOTIFY_OIDC_ENABLED=false
    GOTIFY_OIDC_ISSUER=https://auth.example.com/realms/myrealm
    GOTIFY_OIDC_CLIENTID=gotify
    GOTIFY_OIDC_CLIENTSECRET=super-secret
    GOTIFY_OIDC_REDIRECTURL=https://gotify.example.org/auth/oidc/callback
    GOTIFY_OIDC_AUTOREGISTER=true
    GOTIFY_OIDC_USERNAMECLAIM=preferred_username
    GOTIFY_OIDC_SCOPES=openid,profile,email
  6. Configure the Gotify UI via the global window.config object

    master

    The Gotify UI configuration is driven by a global window.config object. This object allows you to override default settings such as the server URL, registration permissions, and OIDC status. The UI merges these values into its internal configuration state.

    Available configuration keys in window.config:

    • url (string): The base URL of the Gotify server.
    • register (boolean): Whether new users are allowed to register.
    • version (IVersion): The version information (commit, buildDate, version).
    • oidc (boolean): Whether OpenID Connect is enabled.
    // Example of setting configuration on the window object before the UI initializes
    window.config = {
      url: 'https://gotify.example.com',
      register: true,
      oidc: false
    };
  7. Configure User Registration and Security

    master

    Manage user creation and security parameters.

    VariableTypeDescription
    GOTIFY_DEFAULTUSER_NAMEtextUsername for the initial admin account (only applied on first database creation).
    GOTIFY_DEFAULTUSER_PASStextPassword for the initial admin account (only applied on first database creation).
    GOTIFY_PASSSTRENGTHnumberBcrypt cost factor for password hashes. Higher is more secure but slower.
    GOTIFY_REGISTRATIONbooleanIf true, allows unauthenticated users to register via the public endpoint.
    GOTIFY_SERVER_SECURECOOKIEbooleanIf true, sets the Secure flag on session cookies (requires HTTPS).
    GOTIFY_DEFAULTUSER_NAME=admin
    GOTIFY_DEFAULTUSER_PASS=admin
    GOTIFY_PASSSTRENGTH=10
    GOTIFY_REGISTRATION=false
    GOTIFY_SERVER_SECURECOOKIE=false
  8. Configure Database and Storage

    master

    Define how Gotify stores its data and where it keeps uploaded files.

    VariableTypeDescription
    GOTIFY_DATABASE_DIALECTtextDriver to use: sqlite3, mysql, or postgres.
    GOTIFY_DATABASE_CONNECTIONtextConnection string. Format depends on dialect.
    GOTIFY_UPLOADEDIMAGESDIRtextDirectory for application icons and uploaded images. Must be writable.
    GOTIFY_PLUGINSDIRtextDirectory scanned for plugin shared libraries on startup.
    GOTIFY_DATABASE_DIALECT=sqlite3
    GOTIFY_DATABASE_CONNECTION=data/gotify.db
    GOTIFY_UPLOADEDIMAGESDIR=data/images
    GOTIFY_PLUGINSDIR=data/plugins
  9. Configure HTTP and HTTPS server settings

    master

    Use these variables to define how the Gotify server listens for connections.

    VariableTypeDescription
    GOTIFY_SERVER_LISTENADDRtextNetwork address to bind to. Leave empty for all interfaces. Prefix with unix: for a Unix domain socket (e.g., unix:/tmp/gotify.sock).
    GOTIFY_SERVER_PORTnumberPort for the HTTP server.
    GOTIFY_SERVER_SSL_ENABLEDbooleanEnable the HTTPS listener. Requires CERTFILE+CERTKEY or Let's Encrypt.
    GOTIFY_SERVER_SSL_REDIRECTTOHTTPSbooleanRedirect plain HTTP requests to HTTPS (only effective if SSL_ENABLED=true).
    GOTIFY_SERVER_SSL_LISTENADDRtextNetwork address for the HTTPS server. Prefix with unix: for a Unix domain socket.
    GOTIFY_SERVER_SSL_PORTnumberPort for the HTTPS server.
    GOTIFY_SERVER_SSL_CERTFILEtextPath to the TLS certificate.
    GOTIFY_SERVER_SSL_CERTKEYtextPath to the TLS private key.
    GOTIFY_SERVER_KEEPALIVEPERIODSECONDSnumberTCP keepalive interval. 0 uses Go default (15s), -1 disables entirely.
    GOTIFY_SERVER_LISTENADDR=192.168.178.2
    GOTIFY_SERVER_PORT=80
    GOTIFY_SERVER_SSL_ENABLED=false
    GOTIFY_SERVER_SSL_REDIRECTTOHTTPS=true
  10. Configure Let's Encrypt automatic TLS

    master

    Gotify can automatically obtain and manage TLS certificates via Let's Encrypt. This requires GOTIFY_SERVER_SSL_ENABLED=true and GOTIFY_SERVER_SSL_LETSENCRYPT_ACCEPTTOS=true.

    VariableTypeDescription
    GOTIFY_SERVER_SSL_LETSENCRYPT_ENABLEDbooleanEnable automatic Let's Encrypt certificate acquisition.
    GOTIFY_SERVER_SSL_LETSENCRYPT_ACCEPTTOSbooleanMust be true to use Let's Encrypt.
    GOTIFY_SERVER_SSL_LETSENCRYPT_CACHEtextDirectory where certificates and ACME data are stored. Must be writable.
    GOTIFY_SERVER_SSL_LETSENCRYPT_DIRECTORYURLtextOverride the ACME directory URL (e.g., for the staging server).
    GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTStext-listList of hosts for which to issue certificates. Each must resolve to this server.
    GOTIFY_SERVER_SSL_LETSENCRYPT_ENABLED=false
    GOTIFY_SERVER_SSL_LETSENCRYPT_ACCEPTTOS=false
    GOTIFY_SERVER_SSL_LETSENCRYPT_CACHE=data/certs
    GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS=mydomain.tld,myotherdomain.tld
  11. Configure CORS and WebSocket settings

    master

    Control cross-origin requests and WebSocket behavior.

    CORS (Cross-Origin Resource Sharing)

    VariableTypeDescription
    GOTIFY_SERVER_CORS_ALLOWORIGINStext-listRegex of allowed origins. Setting any CORS value enables CORS handling.
    GOTIFY_SERVER_CORS_ALLOWMETHODStext-listPermitted HTTP methods (e.g., GET,POST).
    GOTIFY_SERVER_CORS_ALLOWHEADERStext-listPermitted request headers (e.g., Authorization,content-type).

    WebSockets

    VariableTypeDescription
    GOTIFY_SERVER_STREAM_PINGPERIODSECONDSnumberInterval in seconds between WebSocket ping frames.
    GOTIFY_SERVER_STREAM_ALLOWEDORIGINStext-listRegex of allowed origins for WebSocket upgrade requests. Same-origin is always permitted.
    GOTIFY_SERVER_CORS_ALLOWORIGINS=.+\.example\.com,otherdomain\.com
    GOTIFY_SERVER_CORS_ALLOWMETHODS=GET,POST
    GOTIFY_SERVER_CORS_ALLOWHEADERS=Authorization,content-type
    GOTIFY_SERVER_STREAM_PINGPERIODSECONDS=45
  12. Access and modify UI configuration using set() and get()

    master

    The UI provides set and get functions to interact with the application configuration programmatically. These functions operate on the internal config object which is initialized from window.config.

    • set<Key>(key, value): Updates a specific configuration key with a new value.
    • get<K>(key): Retrieves the current value of a configuration key.
    import { get, set } from './config';
    
    // Retrieve the server URL
    const serverUrl = get('url');
    
    // Update the registration setting
    set('register', true);