1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-08-04 12:57:39 +00:00

-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
import (
@ -46,7 +48,7 @@ type Config struct {
NaviTitleIcon string `comment:"This icon is used in the breadcrumbs bar."`
Hyphae
Network
Authorization `comment:""`
Authorization
CustomScripts `comment:"You can specify additional scripts to load on different kinds of pages, delimited by a comma ',' sign."`
}
@ -97,7 +99,6 @@ func ReadConfigFile(path string) error {
Network: Network{
HTTPPort: 1737,
URL: "",
GeminiCertificatePath: "",
},
Authorization: Authorization{
UseAuth: false,
@ -111,36 +112,12 @@ func ReadConfigFile(path string) error {
},
}
dirty := false
f, err := ini.Load(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
f = ini.Empty()
dirty = true
} else {
return fmt.Errorf("Failed to open the config file: %w", err)
}
}
// Map the config file to the config struct. It'll do nothing if the file
// doesn't exist or is empty.
f.MapTo(cfg)
// Update the port if it's set externally and is different from what's in
// the config file
if HTTPPort != "" && HTTPPort != strconv.FormatUint(cfg.Network.HTTPPort, 10) {
port, err := strconv.ParseUint(HTTPPort, 10, 64)
if err != nil {
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 {
// Save the default configuration
err = f.ReflectFrom(cfg)
if err != nil {
return fmt.Errorf("Failed to serialize the config: %w", err)
@ -152,6 +129,21 @@ func ReadConfigFile(path string) error {
if err = f.SaveTo(path); err != nil {
return fmt.Errorf("Failed to save the config file: %w", err)
}
} else {
return fmt.Errorf("Failed to open the config file: %w", err)
}
}
// Map the config file to the config struct. It'll do nothing if the file
// doesn't exist or is empty.
f.MapTo(cfg)
// Check for PORT env var and use it, if present
if os.Getenv("PORT") != "" {
port, err := strconv.ParseUint(os.Getenv("PORT"), 10, 64)
if err == nil {
cfg.Network.HTTPPort = port
}
}
// Map the struct to the global variables
@ -162,7 +154,6 @@ func ReadConfigFile(path string) error {
HeaderLinksHypha = cfg.HeaderLinksHypha
HTTPPort = strconv.FormatUint(cfg.HTTPPort, 10)
URL = cfg.URL
GeminiCertificatePath = cfg.GeminiCertificatePath
UseAuth = cfg.UseAuth
AllowRegistration = cfg.AllowRegistration
RegistrationLimit = cfg.RegistrationLimit

View File

@ -37,7 +37,6 @@ func parseCliArgs() {
var createAdminName string
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.Parse()