-port flag -> PORT variable

Don't rely on this! It'll be changed to something else, I'm just
cleaning things up. The preferred way is to set the port in the config
file.
This commit is contained in:
handlerug 2021-07-05 11:11:45 +07:00
parent 019e78663d
commit 9dfbcedd19
No known key found for this signature in database
GPG Key ID: 38009F0605051491
2 changed files with 26 additions and 36 deletions

View File

@ -1,4 +1,6 @@
// Package cfg contains global variables that represent the current wiki configuration, including CLI options, configuration file values and header links. // Package cfg contains global variables that represent the current wiki
// configuration, including CLI options, configuration file values and header
// links.
package cfg package cfg
import ( import (
@ -46,7 +48,7 @@ type Config struct {
NaviTitleIcon string `comment:"This icon is used in the breadcrumbs bar."` NaviTitleIcon string `comment:"This icon is used in the breadcrumbs bar."`
Hyphae Hyphae
Network Network
Authorization `comment:""` Authorization
CustomScripts `comment:"You can specify additional scripts to load on different kinds of pages, delimited by a comma ',' sign."` CustomScripts `comment:"You can specify additional scripts to load on different kinds of pages, delimited by a comma ',' sign."`
} }
@ -60,8 +62,8 @@ type Hyphae struct {
// Network is a section of Config that has fields related to network stuff: // Network is a section of Config that has fields related to network stuff:
// HTTP and Gemini. // HTTP and Gemini.
type Network struct { type Network struct {
HTTPPort uint64 HTTPPort uint64
URL string `comment:"Set your wiki's public URL here. It's used for OpenGraph generation and syndication feeds."` URL string `comment:"Set your wiki's public URL here. It's used for OpenGraph generation and syndication feeds."`
} }
// CustomScripts is a section with paths to JavaScript files that are loaded on // CustomScripts is a section with paths to JavaScript files that are loaded on
@ -95,9 +97,8 @@ func ReadConfigFile(path string) error {
HeaderLinksHypha: "", HeaderLinksHypha: "",
}, },
Network: Network{ Network: Network{
HTTPPort: 1737, HTTPPort: 1737,
URL: "", URL: "",
GeminiCertificatePath: "",
}, },
Authorization: Authorization{ Authorization: Authorization{
UseAuth: false, UseAuth: false,
@ -111,13 +112,23 @@ func ReadConfigFile(path string) error {
}, },
} }
dirty := false
f, err := ini.Load(path) f, err := ini.Load(path)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
f = ini.Empty() f = ini.Empty()
dirty = true
// Save the default configuration
err = f.ReflectFrom(cfg)
if err != nil {
return fmt.Errorf("Failed to serialize the config: %w", err)
}
// Disable key-value auto-aligning, but retain spaces around '=' sign
ini.PrettyFormat = false
ini.PrettyEqual = true
if err = f.SaveTo(path); err != nil {
return fmt.Errorf("Failed to save the config file: %w", err)
}
} else { } else {
return fmt.Errorf("Failed to open the config file: %w", err) return fmt.Errorf("Failed to open the config file: %w", err)
} }
@ -127,30 +138,11 @@ func ReadConfigFile(path string) error {
// doesn't exist or is empty. // doesn't exist or is empty.
f.MapTo(cfg) f.MapTo(cfg)
// Update the port if it's set externally and is different from what's in // Check for PORT env var and use it, if present
// the config file if os.Getenv("PORT") != "" {
if HTTPPort != "" && HTTPPort != strconv.FormatUint(cfg.Network.HTTPPort, 10) { port, err := strconv.ParseUint(os.Getenv("PORT"), 10, 64)
port, err := strconv.ParseUint(HTTPPort, 10, 64) if err == nil {
if err != nil { cfg.Network.HTTPPort = port
return fmt.Errorf("Failed to parse the port from command-line arguments: %w", err)
}
cfg.Network.HTTPPort = port
dirty = true
}
// Save changes, if there are any
if dirty {
err = f.ReflectFrom(cfg)
if err != nil {
return fmt.Errorf("Failed to serialize the config: %w", err)
}
// Disable key-value auto-aligning, but retain spaces around '=' sign
ini.PrettyFormat = false
ini.PrettyEqual = true
if err = f.SaveTo(path); err != nil {
return fmt.Errorf("Failed to save the config file: %w", err)
} }
} }
@ -162,7 +154,6 @@ func ReadConfigFile(path string) error {
HeaderLinksHypha = cfg.HeaderLinksHypha HeaderLinksHypha = cfg.HeaderLinksHypha
HTTPPort = strconv.FormatUint(cfg.HTTPPort, 10) HTTPPort = strconv.FormatUint(cfg.HTTPPort, 10)
URL = cfg.URL URL = cfg.URL
GeminiCertificatePath = cfg.GeminiCertificatePath
UseAuth = cfg.UseAuth UseAuth = cfg.UseAuth
AllowRegistration = cfg.AllowRegistration AllowRegistration = cfg.AllowRegistration
RegistrationLimit = cfg.RegistrationLimit RegistrationLimit = cfg.RegistrationLimit

View File

@ -37,7 +37,6 @@ func parseCliArgs() {
var createAdminName string var createAdminName string
flag.StringVar(&createAdminName, "create-admin", "", "Create a new admin. The password will be prompted in the terminal.") flag.StringVar(&createAdminName, "create-admin", "", "Create a new admin. The password will be prompted in the terminal.")
flag.StringVar(&cfg.HTTPPort, "port", "", "Listen on another port. This option also updates the config file for your convenience.")
flag.Usage = printHelp flag.Usage = printHelp
flag.Parse() flag.Parse()