takunomi-blog/websiteTemplate.go

101 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
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
var templates = template.Must(template.ParseFiles("templates/blog.tmpl", "templates/about.tmpl", "templates/past.tmpl", "templates/contact.tmpl", "templates/blog_roll.tmpl", "templates/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)
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("images"))))
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
2017-12-06 19:04:37 +00:00
//posts = PostsCollection[:]
setPostsCollection()
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-12-06 19:04:37 +00:00
func setPostsCollection() {
posts = PostsCollection[:]
}
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-21 09:53:32 +00:00
length := len(posts)
posts = posts[:3]
//posts = getNewestPosts(3)
page := Page{name, posts, "blog"}
2017-11-07 11:16:35 +00:00
renderTemplates(w, page)
2017-11-21 09:53:32 +00:00
posts = posts[:length]
2017-11-07 11:16:35 +00:00
}
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)}
page := Page{name, posts, "blog"}
2017-11-19 14:02:18 +00:00
renderTemplates(w, page)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
page := Page{name, []Post{}, "about"}
2017-11-19 14:02:18 +00:00
renderTemplates(w, page)
}
func pastHandler(w http.ResponseWriter, r *http.Request) {
page := Page{name, 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, []Post{}, "contact"}
2017-11-20 13:37:08 +00:00
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
}