forked from Team-Ortix/blgo
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.
parent
2225bc34e4
commit
ccc23cead0
@ -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
|
||||
}
|
@ -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: {{.}}
|
Loading…
Reference in New Issue