diff --git a/util/config.go b/cfg/config.go similarity index 58% rename from util/config.go rename to cfg/config.go index 6b1212e..09f4088 100644 --- a/util/config.go +++ b/cfg/config.go @@ -1,4 +1,4 @@ -package util +package cfg import ( "log" @@ -8,7 +8,31 @@ import ( "github.com/go-ini/ini" ) -// See https://mycorrhiza.lesarbr.es/hypha/configuration/fields +var ( + WikiName string + NaviTitleIcon string + + HomeHypha string + UserHypha string + HeaderLinksHypha string + + HTTPPort string + URL string + GeminiCertificatePath string + + WikiDir string + ConfigFilePath string + + UseFixedAuth bool + FixedAuthCredentialsPath string + UseRegistration bool + RegistrationCredentialsPath string + LimitRegistration int +) + +// Config represents a Mycorrhiza wiki configuration file. +// +// See https://mycorrhiza.lesarbr.es/hypha/configuration/fields for fields' docs. type Config struct { WikiName string NaviTitleIcon string @@ -17,18 +41,21 @@ type Config struct { Authorization } +// Hyphae is a section of Config which has fields related to special hyphae. type Hyphae struct { HomeHypha string UserHypha string HeaderLinksHypha string } +// Network is a section of Config that has fields related to network stuff: HTTP and Gemini. type Network struct { HTTPPort uint64 URL string GeminiCertificatePath string } +// Authorization is a section of Config that has fields related to authorization and authentication. type Authorization struct { UseFixedAuth bool FixedAuthCredentialsPath string @@ -38,6 +65,7 @@ type Authorization struct { LimitRegistration uint64 } +// ReadConfigFile reads a config on the given path and stores the configuration. func ReadConfigFile(path string) { cfg := &Config{ WikiName: "MycorrhizaWiki", @@ -76,16 +104,16 @@ func ReadConfigFile(path string) { } // Map the struct to the global variables - SiteName = cfg.WikiName - SiteNavIcon = cfg.NaviTitleIcon - HomePage = cfg.HomeHypha + WikiName = cfg.WikiName + NaviTitleIcon = cfg.NaviTitleIcon + HomeHypha = cfg.HomeHypha UserHypha = cfg.UserHypha HeaderLinksHypha = cfg.HeaderLinksHypha - ServerPort = strconv.FormatUint(cfg.HTTPPort, 10) + HTTPPort = strconv.FormatUint(cfg.HTTPPort, 10) URL = cfg.URL - GeminiCertPath = cfg.GeminiCertificatePath + GeminiCertificatePath = cfg.GeminiCertificatePath UseFixedAuth = cfg.UseFixedAuth - FixedCredentialsPath = cfg.FixedAuthCredentialsPath + FixedAuthCredentialsPath = cfg.FixedAuthCredentialsPath UseRegistration = cfg.UseRegistration RegistrationCredentialsPath = cfg.RegistrationCredentialsPath LimitRegistration = int(cfg.LimitRegistration) diff --git a/files/files.go b/files/files.go index 5f03a10..634cc16 100644 --- a/files/files.go +++ b/files/files.go @@ -3,11 +3,11 @@ package files import ( "errors" "fmt" + "github.com/bouncepaw/mycorrhiza/cfg" "path/filepath" "strings" "github.com/adrg/xdg" - "github.com/bouncepaw/mycorrhiza/util" "github.com/mitchellh/go-homedir" ) @@ -49,7 +49,7 @@ func tokenStoragePath() (string, error) { if err != nil { return "", err } - if strings.HasPrefix(dir, util.WikiDir) { + if strings.HasPrefix(dir, cfg.WikiDir) { return "", errors.New("wiki storage directory includes private config files") } return dir, nil @@ -57,7 +57,7 @@ func tokenStoragePath() (string, error) { func registrationCredentialsPath() (string, error) { var err error - path := util.RegistrationCredentialsPath + path := cfg.RegistrationCredentialsPath if len(path) == 0 { path, err = xdg.DataFile("mycorrhiza/registration.json") @@ -81,7 +81,7 @@ func registrationCredentialsPath() (string, error) { func fixedCredentialsPath() (string, error) { var err error - path := util.FixedCredentialsPath + path := cfg.FixedAuthCredentialsPath if len(path) > 0 { path, err = homedir.Expand(path) diff --git a/flag.go b/flag.go index 6cb3fef..35bb9da 100644 --- a/flag.go +++ b/flag.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/bouncepaw/mycorrhiza/cfg" "log" "os" "path/filepath" @@ -14,7 +15,7 @@ import ( var printExampleConfig bool func init() { - flag.StringVar(&util.ConfigFilePath, "config-path", "", "Path to a configuration file. Leave empty if you don't want to use it.") + flag.StringVar(&cfg.ConfigFilePath, "config-path", "", "Path to a configuration file. Leave empty if you don't want to use it.") flag.BoolVar(&printExampleConfig, "print-example-config", false, "If true, print an example configuration file contents and exit. You can save the output to a file and base your own configuration on it.") flag.Usage = func() { fmt.Fprintf( @@ -42,16 +43,16 @@ func parseCliArgs() { var err error WikiDir, err = filepath.Abs(args[0]) - util.WikiDir = WikiDir + cfg.WikiDir = WikiDir if err != nil { log.Fatal(err) } - if util.URL == "" { - util.URL = "http://0.0.0.0:" + util.ServerPort + if cfg.URL == "" { + cfg.URL = "http://0.0.0.0:" + cfg.HTTPPort } - util.HomePage = util.CanonicalName(util.HomePage) - util.UserHypha = util.CanonicalName(util.UserHypha) - util.HeaderLinksHypha = util.CanonicalName(util.HeaderLinksHypha) + cfg.HomeHypha = util.CanonicalName(cfg.HomeHypha) + cfg.UserHypha = util.CanonicalName(cfg.UserHypha) + cfg.HeaderLinksHypha = util.CanonicalName(cfg.HeaderLinksHypha) } diff --git a/gemini.go b/gemini.go index b86c3a4..050edc1 100644 --- a/gemini.go +++ b/gemini.go @@ -3,6 +3,7 @@ package main import ( "crypto/tls" "crypto/x509/pkix" + "github.com/bouncepaw/mycorrhiza/cfg" "io/ioutil" "log" "path/filepath" @@ -13,7 +14,6 @@ import ( "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/markup" - "github.com/bouncepaw/mycorrhiza/util" ) func geminiHomeHypha(w *gemini.ResponseWriter, rq *gemini.Request) { @@ -23,7 +23,7 @@ func geminiHomeHypha(w *gemini.ResponseWriter, rq *gemini.Request) { You have successfully served the wiki through Gemini. Currently, support is really work-in-progress; you should resort to using Mycorrhiza through the web protocols. Visit home hypha: -=> /hypha/` + util.HomePage)) +=> /hypha/` + cfg.HomeHypha)) } func geminiHypha(w *gemini.ResponseWriter, rq *gemini.Request) { @@ -48,10 +48,10 @@ func geminiHypha(w *gemini.ResponseWriter, rq *gemini.Request) { } func handleGemini() { - if util.GeminiCertPath == "" { + if cfg.GeminiCertificatePath == "" { return } - certPath, err := filepath.Abs(util.GeminiCertPath) + certPath, err := filepath.Abs(cfg.GeminiCertificatePath) if err != nil { log.Fatal(err) } diff --git a/history/history.go b/history/history.go index dd53203..b94ee51 100644 --- a/history/history.go +++ b/history/history.go @@ -3,6 +3,7 @@ package history import ( "bytes" "fmt" + "github.com/bouncepaw/mycorrhiza/cfg" "html" "log" "os/exec" @@ -167,7 +168,7 @@ func (rev *Revision) bestLink() string { func gitsh(args ...string) (out bytes.Buffer, err error) { fmt.Printf("$ %v\n", args) cmd := exec.Command(gitpath, args...) - cmd.Dir = util.WikiDir + cmd.Dir = cfg.WikiDir b, err := cmd.CombinedOutput() if err != nil { @@ -179,7 +180,7 @@ func gitsh(args ...string) (out bytes.Buffer, err error) { // silentGitsh is like gitsh, except it writes less to the stdout. func silentGitsh(args ...string) (out bytes.Buffer, err error) { cmd := exec.Command(gitpath, args...) - cmd.Dir = util.WikiDir + cmd.Dir = cfg.WikiDir b, err := cmd.CombinedOutput() return *bytes.NewBuffer(b), err diff --git a/history/information.go b/history/information.go index 75b7501..0570e6e 100644 --- a/history/information.go +++ b/history/information.go @@ -4,20 +4,20 @@ package history import ( "fmt" + "github.com/bouncepaw/mycorrhiza/cfg" "log" "regexp" "strconv" "strings" "time" - "github.com/bouncepaw/mycorrhiza/util" "github.com/gorilla/feeds" ) func recentChangesFeed() *feeds.Feed { feed := &feeds.Feed{ Title: "Recent changes", - Link: &feeds.Link{Href: util.URL}, + Link: &feeds.Link{Href: cfg.URL}, Description: "List of 30 recent changes on the wiki", Author: &feeds.Author{Name: "Wikimind", Email: "wikimind@mycorrhiza"}, Updated: time.Now(), @@ -44,7 +44,7 @@ func recentChangesFeed() *feeds.Feed { Description: rev.descriptionForFeed(), Created: rev.Time, Updated: rev.Time, - Link: &feeds.Link{Href: util.URL + rev.bestLink()}, + Link: &feeds.Link{Href: cfg.URL + rev.bestLink()}, }) } return feed @@ -141,7 +141,7 @@ func (rev *Revision) asHistoryEntry(hyphaName string) (html string) { author := "" if rev.Username != "anon" { author = fmt.Sprintf(` - by
  • - {% elseif util.UseFixedAuth %} + {% elseif cfg.UseFixedAuth %}

    Administrators have forbidden registration for this wiki. Administrators can make an account for you by hand; contact them.

    ← Go back

    {% else %} @@ -43,7 +43,7 @@ {% if user.AuthUsed %}
  • {% endif %} diff --git a/views/nav.qtpl.go b/views/nav.qtpl.go index c3b14ec..2cbdcce 100644 --- a/views/nav.qtpl.go +++ b/views/nav.qtpl.go @@ -11,10 +11,10 @@ import "net/http" import "strings" //line views/nav.qtpl:3 -import "github.com/bouncepaw/mycorrhiza/user" +import "github.com/bouncepaw/mycorrhiza/cfg" //line views/nav.qtpl:4 -import "github.com/bouncepaw/mycorrhiza/util" +import "github.com/bouncepaw/mycorrhiza/user" // This is the