package main import ( "encoding/json" "flag" "io/ioutil" "log" "net/http" "os" "code.ivysaur.me/yatwiki" ) func main() { bindAddr := flag.String("listen", "127.0.0.1:80", "Bind address") configPath := flag.String("config", "config.json", "Configuration file") flag.Parse() opts := yatwiki.ServerOptions{} cfg, err := ioutil.ReadFile(*configPath) if err != nil { if os.IsNotExist(err) { opts = *yatwiki.DefaultOptions() if cfg, err := json.MarshalIndent(opts, "", "\t"); err == nil { err := ioutil.WriteFile(*configPath, cfg, 0644) if err != nil { log.Printf("Failed to save default configuration file: %s", err.Error()) } } } else { log.Fatalf("Failed to load configuration file '%s': %s\n", *configPath, err.Error()) } } else { log.Printf("Loading configuration from '%s'...\n", *configPath) err = json.Unmarshal(cfg, &opts) if err != nil { log.Fatalf("Failed to parse configuration file: %s\n", err.Error()) } } ws, err := yatwiki.NewWikiServer(&opts) if err != nil { log.Fatalln(err.Error()) } defer ws.Close() log.Printf("YATWiki now listening on %s\n", *bindAddr) err = http.ListenAndServe(*bindAddr, ws) if err != nil { log.Fatalln(err.Error()) } os.Exit(0) }