package main import ( "encoding/json" "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.json", "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 := json.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 = json.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) }