diff --git a/.gitignore b/.gitignore index e329c02..b1bc5d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ doksie +config.json \ No newline at end of file diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..9765ae6 --- /dev/null +++ b/config.json.example @@ -0,0 +1,3 @@ +{ + "port": 6900 +} \ No newline at end of file diff --git a/main.go b/main.go index d80fc28..8b4b743 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,10 @@ package main import ( + "encoding/json" + "html/template" "log" "os" - "text/template" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" @@ -13,6 +14,10 @@ import ( "net/http" ) +type Config struct { + Port uint +} + type Page struct { Title string MarkDown []byte @@ -20,8 +25,6 @@ type Page struct { Navigation string } -const port = 6900 - func mdToHTML(md []byte) []byte { // create markdown parser with extensions extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock @@ -37,10 +40,26 @@ func mdToHTML(md []byte) []byte { } func main() { - fmt.Printf("Starting listener on :%v", port) + fmt.Println("Loading config.json") + cfgfile, err := os.Open("config.json") + if err != nil { + log.Fatal("Error reading config.json!") + return + } + defer cfgfile.Close() + + decoder := json.NewDecoder(cfgfile) + Config := Config{} + err = decoder.Decode(&Config) + if err != nil { + log.Fatal("Can't decode config.json!") + return + } + + fmt.Printf("Starting listener on :%v", Config.Port) http.Handle("/static/", http.FileServer(http.Dir("./"))) http.HandleFunc("/", httpRootHandler) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", Config.Port), nil)) } func loadPage(fileUrl string) ([]byte, error) {