yatwiki/cmd/yatwiki-server/main.go

59 lines
1.4 KiB
Go
Raw Normal View History

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"
"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
// Create default configuration file if necessary
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
log.Printf("Creating default configuration file at '%s'...\n", *configPath)
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())
}
}
}
// Load configuration
log.Printf("Loading configuration from '%s'...\n", *configPath)
cfg, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatalf("Failed to load configuration file '%s': %s\n", *configPath, err.Error())
}
err = json.Unmarshal(cfg, &opts)
if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err.Error())
}
//
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
}
}