nginx

package
v1.4.15 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

README

nginx

Handler to start, stop, test and reload nginx programatically, assuming you have nginx installed. The following code will start an nginx server, and then stop it after 10 seconds:

package main

import (
    "github.com/mutablelogic/go-server/pkg/handler/nginx"
)

func main() { 
    if nginx, err := nginx.New(nginx.Config{}); err != nil {
        log.Fatal(err)
    }

    // Create a cancellable context
    ctx, cancel := context.WithTimeout(context.Background(),10 * time.Second)
    defer cancel()

    // Run nginx in foreground until timeout is reached
    if err := nginx.Run(ctx); err != nil {
        t.Error(err)
    }
}

Some additional methods can be used to stop, test and reload the server:

type Nginx interface {
    // test the configuration and return an error if it fails
    Test() error

    // test the configuration and then reload it (the SIGHUP signal)
    Reload() error

    // reopen log files (the SIGUSR1 signal)
    Reopen() error

    // return the nginx version string
    Version() string
}

Configuration

The configuration for the nginx server is embedded (no external configuration files are required). By default, a Hello, World static server is setup on port 80. In reality, you'll want to override this configuration with your own.

TODO

API

The commands can be called through an API.

Method Path Scope Description
GET / read Return the nginx version and uptime
PUT /test write Test the server configuration
PUT /reload write Test the configuration and then reload it
PUT /reopen write Reopen log files
GET /config read Read the current set of configurations
GET /config/{id} read Read a specific configuration file
DELETE /config/{id} write Delete a configuration, and reload
POST /config/{id} write Create a new configuration, then reload
PATCH /config/{id} write Update a configuration enabled or body, and reload on change

The body of the POST request should be a JSON object with the following fields:

  • enabled: A boolean value to enable or disable the configuration
  • body: A string value which contains the content of the configuration file

The scopes are (not yet implemented):

  • read: github.com/mutablelogic/go-server/handler/nginx.read
  • write: github.com/mutablelogic/go-server/handler/nginx.write

Documentation

Overview

The `nginx` task spawns an nginx server, and can test and reload the nginx configuration programmatically or through the API gateway.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(c *Config) (*nginx, error)

Create a new http server from the configuration

Types

type Config

type Config struct {
	BinaryPath string            `hcl:"binary_path" description:"Path to nginx binary"`
	ConfigPath string            `hcl:"config" description:"Path to persistent configuration"`
	DataPath   string            `hcl:"data" description:"Path to ephermeral data directory"`
	LogPath    string            `hcl:"log" description:"Path to log directory"`
	LogRotate  time.Duration     `hcl:"log_rotate_period" description:"TODO: Period for log rotations (1d)"`
	LogKeep    time.Duration     `hcl:"log_keep_period" description:"TODO: Period for log deletions (28d)"`
	Env        map[string]string `hcl:"env" description:"Environment variables to set"`
	Directives map[string]string `hcl:"directives" description:"Directives to set in nginx configuration"`
	// contains filtered or unexported fields
}

func (*Config) ConfigDir added in v1.4.10

func (c *Config) ConfigDir() (string, error)

Return the path to the persistent data (configurations)

func (*Config) DataDir added in v1.4.10

func (c *Config) DataDir() (string, error)

Return the path to temporary data (pid, sockets)

func (Config) Description

func (Config) Description() string

Description returns the description of the service

func (Config) ExecFile added in v1.4.10

func (c Config) ExecFile() string

Return the path to the nginx binary

func (Config) Flags

func (c Config) Flags(configDir, prefix string) []string

Return the flags for the nginx binary

func (Config) LogDir added in v1.4.10

func (c Config) LogDir(configDir string) (string, error)

Return the path to logging data, relative to the configuration directory

func (Config) Name

func (Config) Name() string

Name returns the name of the service

func (Config) New

func (c Config) New() (server.Task, error)

Create a new task from the configuration

type Nginx

type Nginx interface {
	// test the configuration and return an error if it fails
	Test() error

	// test the configuration and then reload it (the SIGHUP signal)
	Reload() error

	// reopen log files (the SIGUSR1 signal)
	Reopen() error

	// return the nginx version string
	Version() string

	// return the persistent config path
	ConfigPath() string

	// return the ephermeral data path
	DataPath() string

	// return logfile path
	LogPath() string
}

Directories

Path Synopsis
Implements an API client for the nginx API (https://github.com/mutablelogic/go-server/pkg/handler/nginx)
Implements an API client for the nginx API (https://github.com/mutablelogic/go-server/pkg/handler/nginx)
Manages the lifecycle of configuration folders for nginx
Manages the lifecycle of configuration folders for nginx

Jump to

Keyboard shortcuts

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