forked from ohayo-jacob/takunomi-blog
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"path/filepath"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
//Page : Is exported to PostCollection
|
|
type Page struct {
|
|
Title string
|
|
Posts []Post
|
|
CurrentPage string
|
|
}
|
|
|
|
var style = ``
|
|
var name = "takunomi"
|
|
var posts []Post
|
|
var templates = template.Must(template.ParseFiles("templates/blog.tmpl", "templates/about.tmpl","templates/keru_shoujo.tmpl", "templates/past.tmpl", "templates/contact.tmpl", "templates/blog_roll.tmpl", "templates/post.tmpl"))
|
|
|
|
func main() {
|
|
|
|
buildHandlers()
|
|
retrievePosts()
|
|
|
|
if os.Args[1] == "local" {
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|
|
|
|
if os.Args[1] == "ext" {
|
|
http.ListenAndServe(":35291", nil)
|
|
}
|
|
|
|
}
|
|
|
|
func retrievePosts() {
|
|
posts = nil
|
|
filepath.Walk("./posts", newPost)
|
|
posts = reversePosts()
|
|
}
|
|
|
|
func buildHandlers() {
|
|
http.HandleFunc("/about/", aboutHandler)
|
|
http.HandleFunc("/past/", pastHandler)
|
|
http.HandleFunc("/the_girl_who_kicked_a_rabbit/", keruHandler)
|
|
http.HandleFunc("/contact/", contactHandler)
|
|
http.HandleFunc("/post/", postHandler)
|
|
http.HandleFunc("/update", updateHandler)
|
|
http.HandleFunc("/", pastHandler)
|
|
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images"))))
|
|
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
|
|
}
|
|
|
|
func errorHandler(w http.ResponseWriter, req *http.Request, status int) {
|
|
w.WriteHeader(status)
|
|
if status == http.StatusNotFound {
|
|
fmt.Fprint(w, "custom 404")
|
|
}
|
|
}
|
|
|
|
func postHandler(w http.ResponseWriter, r *http.Request) {
|
|
title := strings.TrimPrefix(r.URL.Path, "/post/")
|
|
posts := []Post{getPostByURLTitle(title)}
|
|
page := Page{name, posts, "blog"}
|
|
renderTemplates(w, page)
|
|
}
|
|
|
|
func aboutHandler(w http.ResponseWriter, r *http.Request) {
|
|
page := Page{name, []Post{}, "about"}
|
|
renderTemplates(w, page)
|
|
}
|
|
|
|
func pastHandler(w http.ResponseWriter, r *http.Request) {
|
|
page := Page{name, posts, "past"}
|
|
renderTemplates(w, page)
|
|
}
|
|
|
|
func keruHandler(w http.ResponseWriter, r *http.Request) {
|
|
posts := []Post{getPostByURLTitle("The-Girl-Who-Kicked-a-Rabbit")}
|
|
page := Page{name, posts, "blog"}
|
|
renderTemplates(w, page)
|
|
}
|
|
func contactHandler(w http.ResponseWriter, r *http.Request) {
|
|
page := Page{name, []Post{}, "contact"}
|
|
renderTemplates(w, page)
|
|
}
|
|
|
|
func updateHandler(w http.ResponseWriter, r *http.Request) {
|
|
retrievePosts()
|
|
}
|
|
|
|
func renderTemplates(w http.ResponseWriter, p Page) {
|
|
s1 := templates.Lookup("blog.tmpl")
|
|
err := s1.ExecuteTemplate(w, "blog", p)
|
|
if err != nil {
|
|
fmt.Println("error is ", err)
|
|
}
|
|
}
|