diff --git a/.gitignore b/.gitignore index d344ba6..686addc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config.json +content/ diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..a39c290 --- /dev/null +++ b/handler/handler.go @@ -0,0 +1,17 @@ +package handler + +import "net/http" + +// Index is the handler that displays the index page. +func Index(w http.ResponseWriter, r *http.Request) { + CurrentTheme.ExecuteTemplate(w, "index.tmpl", nil) +} + +// Handler is the mux-ing handler. +func Handler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + Index(w, r) + return + } + Post(w, r) +} diff --git a/handler/post.go b/handler/post.go new file mode 100644 index 0000000..d18377f --- /dev/null +++ b/handler/post.go @@ -0,0 +1,27 @@ +package handler + +import ( + "net/http" + "os" +) + +// Post is the handler that renders a post. +func Post(w http.ResponseWriter, r *http.Request) { + fileName := "content" + r.URL.Path + ".md" + + // Check if the post exists and is not a directory. + info, err := os.Stat(fileName) + if os.IsNotExist(err) || (info != nil && info.IsDir()) { + w.WriteHeader(http.StatusNotFound) + CurrentTheme.ExecuteTemplate(w, "404.tmpl", nil) + return + } + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + CurrentTheme.ExecuteTemplate(w, "500.tmpl", err) + return + } + + // TODO Make it actually pass in a post. + CurrentTheme.ExecuteTemplate(w, "post.tmpl", nil) +} diff --git a/handler/template.go b/handler/template.go new file mode 100644 index 0000000..dfb5ff8 --- /dev/null +++ b/handler/template.go @@ -0,0 +1,21 @@ +package handler + +import "html/template" + +// CurrentTheme is the set of template being used. +var CurrentTheme *template.Template + +// SetTheme parses the provided theme name and sets it as the current theme. +func SetTheme(themeName string) error { + var err error + + folder := "templates/" + themeName + "/" + CurrentTheme, err = template.ParseFiles( + folder+"index.tmpl", // Homepage (listing). + folder+"404.tmpl", // Post not found. + folder+"500.tmpl", // Internal error. + folder+"post.tmpl", // Page for posts. + ) + + return err +} diff --git a/main.go b/main.go index f003a4b..7fa9668 100644 --- a/main.go +++ b/main.go @@ -5,25 +5,36 @@ import ( "strconv" "github.com/sirupsen/logrus" + + "gitea.teamortix.com/Team-Ortix/blgo/handler" ) func main() { created, err := createDefaultConfig() - if err != nil { - logrus.Fatalf("error while reading config file: %v", err) - } - if created { + switch { + case err != nil: + logrus.Fatalf("Error while reading config file: %v", err) + case created: logrus.Info("The default config file has been created. Please edit it and re-run the program.") return } cfg, err := parseConfig() if err != nil { - logrus.Fatalf("error while parsing the config: %v", err) + logrus.Fatalf("Error while parsing the config: %v", err) } + logrus.Info("Parsing template theme \"", cfg.ThemeName, "\"...") + err = handler.SetTheme(cfg.ThemeName) + if err != nil { + logrus.Fatalf("Error while parsing templates: %v", err) + } + logrus.Info("Completed template parsing.") + + http.HandleFunc("/", handler.Handler) + portStr := strconv.Itoa(cfg.Port) - logrus.Info("Listening on port " + portStr) + logrus.Info("Listening on port " + portStr + "...") err = http.ListenAndServe(":"+portStr, nil) if err != nil { logrus.Fatalf("error while listen and serving: %v", err) diff --git a/templates/debug/404.tmpl b/templates/debug/404.tmpl new file mode 100644 index 0000000..d2b6db1 --- /dev/null +++ b/templates/debug/404.tmpl @@ -0,0 +1,3 @@ +404 Page. + +Passed in: {{.}} diff --git a/templates/debug/500.tmpl b/templates/debug/500.tmpl new file mode 100644 index 0000000..5c9536d --- /dev/null +++ b/templates/debug/500.tmpl @@ -0,0 +1,3 @@ +500 Page. + +Error: {{.}} diff --git a/templates/debug/index.tmpl b/templates/debug/index.tmpl new file mode 100644 index 0000000..376fb97 --- /dev/null +++ b/templates/debug/index.tmpl @@ -0,0 +1,3 @@ +Index page + +Passed in: {{.}} diff --git a/templates/debug/post.tmpl b/templates/debug/post.tmpl new file mode 100644 index 0000000..402d87e --- /dev/null +++ b/templates/debug/post.tmpl @@ -0,0 +1,3 @@ +Post Page. + +Passed in: {{.}} diff --git a/templates/default/404.tmpl b/templates/default/404.tmpl new file mode 100644 index 0000000..e69de29 diff --git a/templates/default/500.tmpl b/templates/default/500.tmpl new file mode 100644 index 0000000..e69de29 diff --git a/templates/default/index.tmpl b/templates/default/index.tmpl new file mode 100644 index 0000000..e69de29 diff --git a/templates/default/post.tmpl b/templates/default/post.tmpl new file mode 100644 index 0000000..e69de29