2021-05-11 08:33:00 +00:00
// Package cfg contains global variables that represent the current wiki configuration, including CLI options, configuration file values and header links.
2021-05-09 09:36:39 +00:00
package cfg
2021-03-06 09:40:47 +00:00
import (
"log"
2021-04-28 10:40:53 +00:00
"path/filepath"
2021-03-06 09:40:47 +00:00
"strconv"
"github.com/go-ini/ini"
)
2021-05-11 08:33:00 +00:00
// These variables represent the configuration. You are not meant to modify them after they were set.
//
// See https://mycorrhiza.lesarbr.es/hypha/configuration/fields for their docs.
2021-05-09 09:36:39 +00:00
var (
WikiName string
NaviTitleIcon string
HomeHypha string
UserHypha string
HeaderLinksHypha string
HTTPPort string
URL string
GeminiCertificatePath string
UseFixedAuth bool
FixedAuthCredentialsPath string
UseRegistration bool
RegistrationCredentialsPath string
LimitRegistration int
2021-05-22 18:05:14 +00:00
OmnipresentScripts [ ] string
ViewScripts [ ] string
EditScripts [ ] string
2021-05-09 09:36:39 +00:00
)
2021-05-11 08:33:00 +00:00
// These variables are set before reading the config file, they are set in main.parseCliArgs.
var (
// WikiDir is a full path to the wiki storage directory, which also must be a git repo.
WikiDir string
// ConfigFilePath is a path to the config file. Its value is used when calling ReadConfigFile.
ConfigFilePath string
)
// Config represents a Mycorrhiza wiki configuration file. This type is used only when reading configs.
2021-03-06 09:40:47 +00:00
type Config struct {
WikiName string
NaviTitleIcon string
Hyphae
Network
Authorization
2021-05-22 18:05:14 +00:00
CustomScripts
2021-03-06 09:40:47 +00:00
}
2021-05-09 09:36:39 +00:00
// Hyphae is a section of Config which has fields related to special hyphae.
2021-03-06 09:40:47 +00:00
type Hyphae struct {
HomeHypha string
UserHypha string
HeaderLinksHypha string
}
2021-05-09 09:36:39 +00:00
// Network is a section of Config that has fields related to network stuff: HTTP and Gemini.
2021-03-06 09:40:47 +00:00
type Network struct {
HTTPPort uint64
URL string
GeminiCertificatePath string
}
2021-05-22 18:05:14 +00:00
// CustomScripts is a section with paths to JavaScript files that are loaded on specified pages.
type CustomScripts struct {
// OmnipresentScripts: everywhere...
OmnipresentScripts [ ] string ` delim:"," `
// ViewScripts: /hypha, /rev
ViewScripts [ ] string ` delim:"," `
// Edit: /edit
EditScripts [ ] string ` delim:"," `
}
2021-05-09 09:36:39 +00:00
// Authorization is a section of Config that has fields related to authorization and authentication.
2021-03-06 09:40:47 +00:00
type Authorization struct {
UseFixedAuth bool
FixedAuthCredentialsPath string
2021-04-12 14:26:49 +00:00
UseRegistration bool
RegistrationCredentialsPath string
LimitRegistration uint64
2021-03-06 09:40:47 +00:00
}
2021-05-11 08:33:00 +00:00
// ReadConfigFile reads a config on the given path and stores the configuration. Call it sometime during the initialization.
//
// Note that it may log.Fatal.
func ReadConfigFile ( ) {
2021-03-06 09:40:47 +00:00
cfg := & Config {
WikiName : "MycorrhizaWiki" ,
NaviTitleIcon : "🍄" ,
Hyphae : Hyphae {
HomeHypha : "home" ,
UserHypha : "u" ,
HeaderLinksHypha : "" ,
} ,
Network : Network {
HTTPPort : 1737 ,
URL : "" ,
GeminiCertificatePath : "" ,
} ,
Authorization : Authorization {
UseFixedAuth : false ,
FixedAuthCredentialsPath : "" ,
2021-04-12 14:26:49 +00:00
UseRegistration : false ,
RegistrationCredentialsPath : "" ,
LimitRegistration : 0 ,
2021-03-06 09:40:47 +00:00
} ,
2021-05-22 18:05:14 +00:00
CustomScripts : CustomScripts {
OmnipresentScripts : [ ] string { } ,
ViewScripts : [ ] string { } ,
EditScripts : [ ] string { } ,
} ,
2021-03-06 09:40:47 +00:00
}
2021-03-09 14:27:14 +00:00
2021-05-11 08:33:00 +00:00
if ConfigFilePath != "" {
path , err := filepath . Abs ( ConfigFilePath )
2021-04-28 10:40:53 +00:00
if err != nil {
log . Fatalf ( "cannot expand config file path: %s" , err )
}
2021-03-09 14:27:14 +00:00
log . Println ( "Loading config at" , path )
2021-04-28 10:40:53 +00:00
err = ini . MapTo ( cfg , path )
2021-03-09 14:27:14 +00:00
if err != nil {
log . Fatal ( err )
}
2021-03-06 09:40:47 +00:00
}
2021-04-12 14:26:49 +00:00
// Map the struct to the global variables
2021-05-09 09:36:39 +00:00
WikiName = cfg . WikiName
NaviTitleIcon = cfg . NaviTitleIcon
HomeHypha = cfg . HomeHypha
2021-03-06 09:40:47 +00:00
UserHypha = cfg . UserHypha
HeaderLinksHypha = cfg . HeaderLinksHypha
2021-05-09 09:36:39 +00:00
HTTPPort = strconv . FormatUint ( cfg . HTTPPort , 10 )
2021-03-06 09:40:47 +00:00
URL = cfg . URL
2021-05-09 09:36:39 +00:00
GeminiCertificatePath = cfg . GeminiCertificatePath
2021-03-06 09:40:47 +00:00
UseFixedAuth = cfg . UseFixedAuth
2021-05-09 09:36:39 +00:00
FixedAuthCredentialsPath = cfg . FixedAuthCredentialsPath
2021-04-12 14:26:49 +00:00
UseRegistration = cfg . UseRegistration
RegistrationCredentialsPath = cfg . RegistrationCredentialsPath
2021-04-19 16:39:25 +00:00
LimitRegistration = int ( cfg . LimitRegistration )
2021-05-22 18:05:14 +00:00
OmnipresentScripts = cfg . OmnipresentScripts
ViewScripts = cfg . ViewScripts
EditScripts = cfg . EditScripts
2021-05-11 08:33:00 +00:00
// This URL makes much more sense.
if URL == "" {
URL = "http://0.0.0.0:" + HTTPPort
}
2021-03-06 09:40:47 +00:00
}