~emersion/soju

db55076d04033830fc52c2f910bb514337af049f — Simon Ser 24 days ago 0f6bac3 system-config
wip: systemd-wide config/data paths
5 files changed, 50 insertions(+), 26 deletions(-)

M Makefile
M cmd/soju/main.go
M cmd/sojuctl/main.go
M config/config.go
M doc/soju.1.scd
M Makefile => Makefile +11 -4
@@ 6,15 6,22 @@ RM = rm
SCDOC = scdoc
GOFLAGS =
PREFIX = /usr/local
BINDIR = bin
MANDIR = share/man
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man
SYSCONFDIR = /etc
SHAREDSTATEDIR = /var/lib

all: soju sojuctl doc/soju.1

pkg = git.sr.ht/~emersion/soju
goflags = $(GOFLAGS) \
	-ldflags="-X '$(pkg)/config.sysConfDir=$(SYSCONFDIR)' \
		-X '$(pkg)/config.sharedStateDir=$(SHAREDSTATEDIR)'"

soju:
	$(GO) build $(GOFLAGS) ./cmd/soju
	$(GO) build $(goflags) ./cmd/soju
sojuctl:
	$(GO) build $(GOFLAGS) ./cmd/sojuctl
	$(GO) build $(goflags) ./cmd/sojuctl
doc/soju.1: doc/soju.1.scd
	$(SCDOC) <doc/soju.1.scd >doc/soju.1


M cmd/soju/main.go => cmd/soju/main.go +3 -9
@@ 45,15 45,9 @@ func main() {
	flag.BoolVar(&debug, "debug", false, "enable debug logging")
	flag.Parse()

	var cfg *config.Server
	if configPath != "" {
		var err error
		cfg, err = config.Load(configPath)
		if err != nil {
			log.Fatalf("failed to load config file: %v", err)
		}
	} else {
		cfg = config.Defaults()
	cfg, err := config.Load(configPath)
	if err != nil {
		log.Fatalf("failed to load config file: %v", err)
	}

	cfg.Listen = append(cfg.Listen, listen...)

M cmd/sojuctl/main.go => cmd/sojuctl/main.go +3 -9
@@ 32,15 32,9 @@ func main() {
	flag.StringVar(&configPath, "config", "", "path to configuration file")
	flag.Parse()

	var cfg *config.Server
	if configPath != "" {
		var err error
		cfg, err = config.Load(configPath)
		if err != nil {
			log.Fatalf("failed to load config file: %v", err)
		}
	} else {
		cfg = config.Defaults()
	cfg, err := config.Load(configPath)
	if err != nil {
		log.Fatalf("failed to load config file: %v", err)
	}

	db, err := soju.OpenSQLDB(cfg.SQLDriver, cfg.SQLSource)

M config/config.go => config/config.go +21 -1
@@ 1,13 1,18 @@
package config

import (
	"errors"
	"fmt"
	"net"
	"os"
	"path/filepath"

	"git.sr.ht/~emersion/go-scfg"
)

// These can be set by build scripts
var sysConfDir, sharedStateDir string

type IPSet []*net.IPNet

func (set IPSet) Contains(ip net.IP) bool {


@@ 51,16 56,31 @@ func Defaults() *Server {
	if err != nil {
		hostname = "localhost"
	}
	sqlSource := "soju.db"
	if sharedStateDir != "" {
		sqlSource = filepath.Join(sharedStateDir, "soju", "main.db")
	}
	return &Server{
		Hostname:  hostname,
		SQLDriver: "sqlite3",
		SQLSource: "soju.db",
		SQLSource: sqlSource,
	}
}

func Load(path string) (*Server, error) {
	isDefaultPath := path == ""
	if path == "" && sysConfDir != "" {
		path = filepath.Join(sysConfDir, "soju", "config")
	}
	if path == "" {
		return Defaults(), nil
	}

	cfg, err := scfg.Load(path)
	if err != nil {
		if isDefaultPath && errors.Is(err, os.ErrNotExist) {
			return Defaults(), nil
		}
		return nil, err
	}
	return parse(cfg)

M doc/soju.1.scd => doc/soju.1.scd +12 -3
@@ 52,7 52,8 @@ soju will reload the TLS certificate and key when it receives the HUP signal.
	Show help message and quit.

*-config* <path>
	Path to the config file. If unset, a default config file is used.
	Path to the config file. If unset, _/etc/soju/config_ will be loaded if it
	exists.

*-debug*
	Enable debug logging (this will leak sensitive information such as


@@ 103,8 104,8 @@ The following directives are supported:

*sql* <driver> <source>
	Set the SQL driver settings. The only supported driver is "sqlite3". The
	source is the path to the SQLite database file. By default, the path to the
	database file is "soju.db".
	source is the path to the SQLite database file. By default, the database is
	stored in _/var/lib/soju/main.db_.

*log* <path>
	Path to the bouncer logs root directory, or empty to disable logging. By


@@ 268,6 269,14 @@ abbreviated form, for instance *network* can be abbreviated as *net* or just
*change-password* <new password>
	Change current user password.

# FILES

_/etc/soju/config_
	Default configuration file location.

_/var/lib/soju_
	State files such as the database are stored in this directory.

# AUTHORS

Maintained by Simon Ser <contact@emersion.fr>, who is assisted by other