configuration file handling (xml format)

This commit is contained in:
mappu 2017-07-11 19:33:32 +12:00
parent 73becc8729
commit 3764caacc8
2 changed files with 30 additions and 10 deletions

View File

@ -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`,
}

View File

@ -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)