yatwiki/cmd/yatwiki-server/main.go

62 lines
1.3 KiB
Go

package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"code.ivysaur.me/yatwiki3"
)
func main() {
bindAddr := flag.String("listen", "127.0.0.1:80", "Bind address")
configPath := flag.String("config", "config.xml", "Configuration file")
flag.Parse()
opts := yatwiki3.ServerOptions{}
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)
}
defer ws.Close()
fmt.Printf("YATWiki now listening on %s\n", *bindAddr)
err = http.ListenAndServe(*bindAddr, ws)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
os.Exit(0)
}