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"
|
2017-07-11 07:33:32 +00:00
|
|
|
"io/ioutil"
|
2018-04-02 04:51:08 +00:00
|
|
|
"log"
|
2017-07-08 23:13:36 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2017-07-12 06:43:11 +00:00
|
|
|
"code.ivysaur.me/yatwiki"
|
2017-07-08 23:13:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-12 06:43:11 +00:00
|
|
|
opts := yatwiki.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) {
|
2017-07-12 06:43:11 +00:00
|
|
|
opts = *yatwiki.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 {
|
2018-04-02 04:51:08 +00:00
|
|
|
log.Printf("Failed to save default configuration file: %s", err.Error())
|
2017-07-11 07:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-02 04:51:08 +00:00
|
|
|
log.Fatalf("Failed to load configuration file '%s': %s\n", *configPath, err.Error())
|
2017-07-11 07:33:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-04-02 04:51:08 +00:00
|
|
|
log.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 {
|
2018-04-02 04:51:08 +00:00
|
|
|
log.Fatalf("Failed to parse configuration file: %s\n", err.Error())
|
2017-07-11 07:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 06:43:11 +00:00
|
|
|
ws, err := yatwiki.NewWikiServer(&opts)
|
2017-07-08 23:13:36 +00:00
|
|
|
if err != nil {
|
2018-04-02 04:51:08 +00:00
|
|
|
log.Fatalln(err.Error())
|
2017-07-08 23:13:36 +00:00
|
|
|
}
|
|
|
|
defer ws.Close()
|
|
|
|
|
2018-04-02 04:51:08 +00:00
|
|
|
log.Printf("YATWiki now listening on %s\n", *bindAddr)
|
2017-07-08 23:13:36 +00:00
|
|
|
err = http.ListenAndServe(*bindAddr, ws)
|
|
|
|
if err != nil {
|
2018-04-02 04:51:08 +00:00
|
|
|
log.Fatalln(err.Error())
|
2017-07-08 23:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|