configuration file handling (xml format)
This commit is contained in:
parent
73becc8729
commit
3764caacc8
@ -36,7 +36,7 @@ func DefaultOptions() *ServerOptions {
|
|||||||
RecentChangesRSS: 10,
|
RecentChangesRSS: 10,
|
||||||
GzipCompressionLevel: 9,
|
GzipCompressionLevel: 9,
|
||||||
BannedUserIPRegexes: make([]string, 0),
|
BannedUserIPRegexes: make([]string, 0),
|
||||||
ExternalBaseURL: "/",
|
ExternalBaseURL: "http://127.0.0.1/",
|
||||||
DeclareRSSLanguage: "en-GB",
|
DeclareRSSLanguage: "en-GB",
|
||||||
DeclareRSSEmail: `nobody@example.com`,
|
DeclareRSSEmail: `nobody@example.com`,
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/xml"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"code.ivysaur.me/yatwiki3"
|
"code.ivysaur.me/yatwiki3"
|
||||||
)
|
)
|
||||||
@ -13,17 +14,36 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
bindAddr := flag.String("listen", "127.0.0.1:80", "Bind address")
|
bindAddr := flag.String("listen", "127.0.0.1:80", "Bind address")
|
||||||
dbPath := flag.String("database", "wiki.db", "Database file")
|
configPath := flag.String("config", "config.xml", "Configuration file")
|
||||||
faviconPath := flag.String("favicon", "", "Local disk path to favicon.ico file (leave blank for no favicon)")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
opts := yatwiki3.DefaultOptions()
|
opts := yatwiki3.ServerOptions{}
|
||||||
opts.DBFilePath = *dbPath
|
|
||||||
opts.FaviconFilePath = *faviconPath
|
|
||||||
opts.Timezone = time.Local.String()
|
|
||||||
opts.ExternalBaseURL = "http://" + *bindAddr + "/"
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err.Error())
|
fmt.Fprintln(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user