2017-11-08 09:48:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-11-09 23:03:40 +00:00
|
|
|
"fmt"
|
2017-11-08 09:48:07 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-11-09 23:03:40 +00:00
|
|
|
"strings"
|
2017-11-08 09:48:07 +00:00
|
|
|
)
|
|
|
|
|
2017-11-09 23:03:40 +00:00
|
|
|
//Page : Is exported to PostCollection
|
2017-11-08 09:48:07 +00:00
|
|
|
type Page struct {
|
2017-11-09 23:03:40 +00:00
|
|
|
Title string
|
2017-11-08 09:48:07 +00:00
|
|
|
MenuItems []string
|
2017-11-09 23:03:40 +00:00
|
|
|
Posts []Post
|
2017-11-08 09:48:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 23:03:40 +00:00
|
|
|
var name = "takunomi"
|
2017-11-08 09:48:07 +00:00
|
|
|
var posts []Post
|
2017-11-09 23:03:40 +00:00
|
|
|
var sections = []string{"about", "past", "contact"}
|
|
|
|
var templates = template.Must(template.ParseFiles("blog.tmpl", "blog_roll.tmpl", "style.tmpl", "post.tmpl"))
|
2017-11-08 09:48:07 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
http.HandleFunc("/", blogHandler)
|
|
|
|
http.HandleFunc("/post/", postHandler)
|
|
|
|
|
|
|
|
if os.Args[1] == "local" {
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Args[1] == "ext" {
|
|
|
|
http.ListenAndServe(":35291", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorHandler(w http.ResponseWriter, req *http.Request, status int) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
if status == http.StatusNotFound {
|
|
|
|
fmt.Fprint(w, "custom 404")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func blogHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path != "/" {
|
|
|
|
errorHandler(w, req, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2017-11-09 23:03:40 +00:00
|
|
|
posts = getNewestPosts(3)
|
|
|
|
page := Page{name, sections, posts}
|
2017-11-08 09:48:07 +00:00
|
|
|
renderTemplates(w, page)
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:03:40 +00:00
|
|
|
func postHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
title := strings.TrimPrefix(r.URL.Path, "/post/")
|
|
|
|
posts := []Post{getPostByURLTitle(title)}
|
|
|
|
page := Page{name, sections, posts}
|
2017-11-08 09:48:07 +00:00
|
|
|
renderTemplates(w, page)
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:03:40 +00:00
|
|
|
func renderTemplates(w http.ResponseWriter, p Page) {
|
2017-11-08 09:48:07 +00:00
|
|
|
s1 := templates.Lookup("blog.tmpl")
|
2017-11-09 23:03:40 +00:00
|
|
|
s1.ExecuteTemplate(w, "blog", p)
|
|
|
|
s2 := templates.Lookup("blog_roll.tmpl")
|
|
|
|
s2.ExecuteTemplate(w, "blog_roll", nil)
|
|
|
|
s3 := templates.Lookup("post.tmpl")
|
|
|
|
s3.ExecuteTemplate(w, "post", nil)
|
2017-11-08 09:48:07 +00:00
|
|
|
}
|