Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/goit/config.go (124 lines, 2.4 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
// Copyright (C) 2024, Jakob Wakeling
// All rights reserved.

package goit

import (
	"encoding/json"
	"errors"
	"os"
	"path/filepath"
)

type config struct {
	DataPath    string `json:"data_path"`
	LogsPath    string `json:"logs_path"`
	RuntimePath string `json:"runtime_path"`
	HttpAddr    string `json:"http_addr"`
	HttpPort    string `json:"http_port"`
	GitPath     string `json:"git_path"`
	IpSessions  bool   `json:"ip_sessions"`
	UsesHttps   bool   `json:"uses_https"`
	IpForwarded bool   `json:"ip_forwarded"`
	CsrfSecret  string `json:"csrf_secret"`
}

func loadConfig() (config, error) {
	conf := config{
		DataPath:    dataPath(),
		LogsPath:    logsPath(),
		RuntimePath: runtimePath(),
		HttpAddr:    "",
		HttpPort:    "8080",
		GitPath:     "git",
		IpSessions:  true,
		UsesHttps:   false,
		IpForwarded: false,
		CsrfSecret:  "1234567890abcdef1234567890abcdef",
	}

	/* Load config file(s) */
	configs := []string{
		filepath.Join("/etc", "goit", "goit.conf"),
	}

	if os.Getuid() != 0 {
		configs = append(configs, filepath.Join(userConfigBase(), "goit", "goit.conf"))
	}

	for _, file := range configs {
		if data, err := os.ReadFile(file); err != nil {
			if !errors.Is(err, os.ErrNotExist) {
				return config{}, err
			}
		} else if data != nil {
			if err := json.Unmarshal(data, &conf); err != nil {
				return config{}, err
			}
		}
	}

	/* Check required config values */
	if conf.DataPath == "" {
		return config{}, errors.New("data path unset")
	}

	return conf, nil
}

func userConfigBase() string {
	if path := os.Getenv("XDG_CONFIG_HOME"); path != "" {
		return path
	}

	if path := os.Getenv("HOME"); path != "" {
		return filepath.Join(path, ".config")
	}

	return ""
}

func dataPath() string {
	if os.Getuid() == 0 {
		return "/var/lib/goit"
	}

	if path := os.Getenv("XDG_DATA_HOME"); path != "" {
		return filepath.Join(path, "goit")
	}

	if path := os.Getenv("HOME"); path != "" {
		return filepath.Join(path, ".local", "share", "goit")
	}

	return ""
}

func logsPath() string {
	if os.Getuid() == 0 {
		return "/var/log/goit"
	}

	if path := os.Getenv("XDG_STATE_HOME"); path != "" {
		return filepath.Join(path, "goit")
	}

	if path := os.Getenv("HOME"); path != "" {
		return filepath.Join(path, ".local", "state", "goit")
	}

	return ""
}

func runtimePath() string {
	if os.Getuid() == 0 {
		return "/run"
	}

	if path := os.Getenv("XDG_RUNTIME_DIR"); path != "" {
		return filepath.Join(path)
	}

	return ""
}