From 3764caacc87014ebd41a78e18d8be2d61e729217 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 11 Jul 2017 19:33:32 +1200 Subject: [PATCH] configuration file handling (xml format) --- ServerOptions.go | 2 +- cmd/yatwiki-server/main.go | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/ServerOptions.go b/ServerOptions.go index e541dff..360e26f 100644 --- a/ServerOptions.go +++ b/ServerOptions.go @@ -36,7 +36,7 @@ func DefaultOptions() *ServerOptions { RecentChangesRSS: 10, GzipCompressionLevel: 9, BannedUserIPRegexes: make([]string, 0), - ExternalBaseURL: "/", + ExternalBaseURL: "http://127.0.0.1/", DeclareRSSLanguage: "en-GB", DeclareRSSEmail: `nobody@example.com`, } diff --git a/cmd/yatwiki-server/main.go b/cmd/yatwiki-server/main.go index 8510759..6911753 100644 --- a/cmd/yatwiki-server/main.go +++ b/cmd/yatwiki-server/main.go @@ -1,11 +1,12 @@ package main import ( + "encoding/xml" "flag" "fmt" + "io/ioutil" "net/http" "os" - "time" "code.ivysaur.me/yatwiki3" ) @@ -13,17 +14,36 @@ import ( func main() { bindAddr := flag.String("listen", "127.0.0.1:80", "Bind address") - dbPath := flag.String("database", "wiki.db", "Database file") - faviconPath := flag.String("favicon", "", "Local disk path to favicon.ico file (leave blank for no favicon)") + configPath := flag.String("config", "config.xml", "Configuration file") flag.Parse() - opts := yatwiki3.DefaultOptions() - opts.DBFilePath = *dbPath - opts.FaviconFilePath = *faviconPath - opts.Timezone = time.Local.String() - opts.ExternalBaseURL = "http://" + *bindAddr + "/" + opts := yatwiki3.ServerOptions{} - ws, err := yatwiki3.NewWikiServer(opts) + cfg, err := ioutil.ReadFile(*configPath) + + if err != nil { + if os.IsNotExist(err) { + opts = *yatwiki3.DefaultOptions() + if cfg, err := xml.MarshalIndent(opts, "", "\t"); err == nil { + err := ioutil.WriteFile(*configPath, cfg, 0644) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to save default configuration file: %s", err.Error()) + } + } + } else { + fmt.Fprintf(os.Stderr, "Failed to load configuration file '%s': %s\n", *configPath, err.Error()) + os.Exit(1) + } + } else { + fmt.Printf("Loading configuration from '%s'...\n", *configPath) + err = xml.Unmarshal(cfg, &opts) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to parse configuration file: %s\n", err.Error()) + os.Exit(1) + } + } + + ws, err := yatwiki3.NewWikiServer(&opts) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1)