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.
master
Luther Wen Xu 2020-11-30 23:29:44 +07:00
parent 2225bc34e4
commit ccc23cead0
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
13 changed files with 95 additions and 6 deletions

1
.gitignore vendored

@ -1 +1,2 @@
config.json
content/

@ -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)
}

@ -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)
}

@ -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
}

@ -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)

@ -0,0 +1,3 @@
404 Page.
Passed in: {{.}}

@ -0,0 +1,3 @@
500 Page.
Error: {{.}}

@ -0,0 +1,3 @@
Index page
Passed in: {{.}}

@ -0,0 +1,3 @@
Post Page.
Passed in: {{.}}