Implementing config.json

This commit is contained in:
Jarkko Toivanen 2024-12-27 20:48:19 +02:00
parent ca00ee623d
commit 41fa10862d
Signed by: jt
GPG key ID: 9151B109B73ECAD5
3 changed files with 28 additions and 5 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
doksie doksie
config.json

3
config.json.example Normal file
View file

@ -0,0 +1,3 @@
{
"port": 6900
}

29
main.go
View file

@ -1,9 +1,10 @@
package main package main
import ( import (
"encoding/json"
"html/template"
"log" "log"
"os" "os"
"text/template"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/html"
@ -13,6 +14,10 @@ import (
"net/http" "net/http"
) )
type Config struct {
Port uint
}
type Page struct { type Page struct {
Title string Title string
MarkDown []byte MarkDown []byte
@ -20,8 +25,6 @@ type Page struct {
Navigation string Navigation string
} }
const port = 6900
func mdToHTML(md []byte) []byte { func mdToHTML(md []byte) []byte {
// create markdown parser with extensions // create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
@ -37,10 +40,26 @@ func mdToHTML(md []byte) []byte {
} }
func main() { 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.Handle("/static/", http.FileServer(http.Dir("./")))
http.HandleFunc("/", httpRootHandler) 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) { func loadPage(fileUrl string) ([]byte, error) {