From ccc23cead041bb36ce2f4d7b9d27b36be0783368 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Mon, 30 Nov 2020 23:29:44 +0800 Subject: [PATCH] feat: Implement basic handler functionality The `default` handler is currently all empty and should be setup in the future. It's currently present as the default theme is in fact, default. The debug template can be used to debug the content being passed into the template as it just prints them out. --- .gitignore | 1 + handler/handler.go | 17 +++++++++++++++++ handler/post.go | 27 +++++++++++++++++++++++++++ handler/template.go | 21 +++++++++++++++++++++ main.go | 23 +++++++++++++++++------ templates/debug/404.tmpl | 3 +++ templates/debug/500.tmpl | 3 +++ templates/debug/index.tmpl | 3 +++ templates/debug/post.tmpl | 3 +++ templates/default/404.tmpl | 0 templates/default/500.tmpl | 0 templates/default/index.tmpl | 0 templates/default/post.tmpl | 0 13 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 handler/handler.go create mode 100644 handler/post.go create mode 100644 handler/template.go create mode 100644 templates/debug/404.tmpl create mode 100644 templates/debug/500.tmpl create mode 100644 templates/debug/index.tmpl create mode 100644 templates/debug/post.tmpl create mode 100644 templates/default/404.tmpl create mode 100644 templates/default/500.tmpl create mode 100644 templates/default/index.tmpl create mode 100644 templates/default/post.tmpl 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