2017-07-08 23:13:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-11 07:34:49 +00:00
|
|
|
"encoding/json"
|
2017-07-08 23:13:36 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-07-11 07:33:32 +00:00
|
|
|
"io/ioutil"
|
2017-07-08 23:13:36 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"code.ivysaur.me/yatwiki3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
bindAddr := flag.String("listen", "127.0.0.1:80", "Bind address")
|
2017-07-11 07:34:49 +00:00
|
|
|
configPath := flag.String("config", "config.json", "Configuration file")
|
2017-07-08 23:13:36 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-07-11 07:33:32 +00:00
|
|
|
opts := yatwiki3.ServerOptions{}
|
2017-07-08 23:13:36 +00:00
|
|
|
|
2017-07-11 07:33:32 +00:00
|
|
|
cfg, err := ioutil.ReadFile(*configPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
opts = *yatwiki3.DefaultOptions()
|
2017-07-11 07:34:49 +00:00
|
|
|
if cfg, err := json.MarshalIndent(opts, "", "\t"); err == nil {
|
2017-07-11 07:33:32 +00:00
|
|
|
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)
|
2017-07-11 07:34:49 +00:00
|
|
|
err = json.Unmarshal(cfg, &opts)
|
2017-07-11 07:33:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to parse configuration file: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := yatwiki3.NewWikiServer(&opts)
|
2017-07-08 23:13:36 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
2017-07-09 00:14:59 +00:00
|
|
|
fmt.Printf("YATWiki now listening on %s\n", *bindAddr)
|
2017-07-08 23:13:36 +00:00
|
|
|
err = http.ListenAndServe(*bindAddr, ws)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|