takunomi-blog/websiteTemplate.go

95 lines
2.3 KiB
Go
Raw Normal View History

2017-11-07 11:16:35 +00:00
package main
import (
2017-11-09 08:56:33 +00:00
"fmt"
2017-11-07 11:16:35 +00:00
"html/template"
"net/http"
2017-11-07 11:47:37 +00:00
"os"
2017-11-09 08:56:33 +00:00
"strings"
2017-11-07 11:16:35 +00:00
)
2017-11-09 08:56:33 +00:00
//Page : Is exported to PostCollection
2017-11-07 11:16:35 +00:00
type Page struct {
2017-11-19 14:02:18 +00:00
Title string
MenuItems []template.HTML
Posts []Post
CurrentPage string
2017-11-07 11:16:35 +00:00
}
2017-11-19 14:02:18 +00:00
var style = ``
2017-11-09 08:56:33 +00:00
var name = "takunomi"
2017-11-07 11:16:35 +00:00
var posts []Post
2017-11-19 14:02:18 +00:00
var sections = []template.HTML{
`<a class="no-link menuElement" style="text-decoration: none; color: inherit" href=/about>about</a>`,
`<a class="no-link menuElement" style="text-decoration: none; color: inherit" href=/past>past</a>`,
`<a class="no-link menuElement" style="text-decoration: none; color: inherit" href=/contact>contact</a>`,
}
2017-11-20 13:37:08 +00:00
var templates = template.Must(template.ParseFiles("blog.tmpl", "about.tmpl", "past.tmpl", "contact.tmpl", "blog_roll.tmpl", "style.tmpl", "post.tmpl"))
2017-11-07 11:16:35 +00:00
func main() {
2017-11-07 11:47:37 +00:00
2017-11-18 18:48:47 +00:00
NewPostsCollection()
2017-11-17 14:12:04 +00:00
2017-11-19 14:02:18 +00:00
http.HandleFunc("/about/", aboutHandler)
http.HandleFunc("/past/", pastHandler)
2017-11-20 13:37:08 +00:00
http.HandleFunc("/contact/", contactHandler)
2017-11-07 11:16:35 +00:00
http.HandleFunc("/post/", postHandler)
2017-11-19 14:02:18 +00:00
http.HandleFunc("/", blogHandler)
2017-11-07 11:47:37 +00:00
if os.Args[1] == "local" {
http.ListenAndServe(":8080", nil)
}
if os.Args[1] == "ext" {
http.ListenAndServe(":35291", nil)
}
2017-11-07 11:16:35 +00:00
}
2017-11-07 11:52:02 +00:00
func errorHandler(w http.ResponseWriter, req *http.Request, status int) {
w.WriteHeader(status)
if status == http.StatusNotFound {
fmt.Fprint(w, "custom 404")
}
}
2017-11-07 11:16:35 +00:00
func blogHandler(w http.ResponseWriter, req *http.Request) {
2017-11-07 11:52:02 +00:00
if req.URL.Path != "/" {
errorHandler(w, req, http.StatusNotFound)
return
}
2017-11-09 08:56:33 +00:00
posts = getNewestPosts(3)
2017-11-19 14:02:18 +00:00
page := Page{name, sections, posts, "blog"}
2017-11-07 11:16:35 +00:00
renderTemplates(w, page)
}
2017-11-09 08:56:33 +00:00
func postHandler(w http.ResponseWriter, r *http.Request) {
title := strings.TrimPrefix(r.URL.Path, "/post/")
posts := []Post{getPostByURLTitle(title)}
2017-11-19 14:02:18 +00:00
page := Page{name, sections, posts, "blog"}
renderTemplates(w, page)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
page := Page{name, sections, []Post{}, "about"}
renderTemplates(w, page)
}
func pastHandler(w http.ResponseWriter, r *http.Request) {
page := Page{name, sections, posts, "past"}
2017-11-07 11:16:35 +00:00
renderTemplates(w, page)
}
2017-11-20 13:37:08 +00:00
func contactHandler(w http.ResponseWriter, r *http.Request) {
page := Page{name, sections, []Post{}, "contact"}
renderTemplates(w, page)
}
2017-11-09 08:56:33 +00:00
func renderTemplates(w http.ResponseWriter, p Page) {
2017-11-07 11:16:35 +00:00
s1 := templates.Lookup("blog.tmpl")
2017-11-19 14:02:18 +00:00
err := s1.ExecuteTemplate(w, "blog", p)
if err != nil {
fmt.Println("error is ", err)
}
2017-11-07 11:16:35 +00:00
}