takunomi-blog/main.go

123 lines
2.8 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"
2021-11-30 19:23:35 +00:00
"path/filepath"
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
)
type Page struct {
2017-11-19 14:02:18 +00:00
Title string
2022-06-09 18:26:01 +00:00
Years [][]Post
//Years map[int][]Post
2017-11-19 14:02:18 +00:00
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
2022-06-09 18:26:01 +00:00
var years [][]Post
//var years map[int][]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/title.tmpl",
"templates/navbar.tmpl",
"templates/main.tmpl",
"templates/post.tmpl"))
2017-11-07 11:16:35 +00:00
func main() {
2017-11-07 11:47:37 +00:00
2021-11-30 19:23:35 +00:00
buildHandlers()
retrievePosts()
2017-11-07 11:47:37 +00:00
2022-06-09 18:26:01 +00:00
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
}
2021-11-30 19:23:35 +00:00
func retrievePosts() {
posts = nil
2022-06-09 18:26:01 +00:00
years = nil
//years = make(map[int][]Post)
2021-11-30 19:23:35 +00:00
filepath.Walk("./posts", newPost)
2022-06-09 18:26:01 +00:00
years = reverseYears()
for i := 0; i<len(years); i++ {
years[i] = reversePosts(years[i])
}
//posts = reversePosts()
2021-11-30 19:23:35 +00:00
}
func buildHandlers() {
http.HandleFunc("/about/", aboutHandler)
http.HandleFunc("/past/", pastHandler)
2021-12-10 18:37:07 +00:00
http.HandleFunc("/the_girl_who_kicked_a_rabbit/", keruHandler)
2021-11-30 19:23:35 +00:00
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"))))
2017-12-06 19:04:37 +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-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)}
2022-06-09 18:26:01 +00:00
page := Page{name, years, posts, "blog"}
2017-11-19 14:02:18 +00:00
renderTemplates(w, page)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
2022-06-09 18:26:01 +00:00
page := Page{name, nil, []Post{}, "about"}
2017-11-19 14:02:18 +00:00
renderTemplates(w, page)
}
func pastHandler(w http.ResponseWriter, r *http.Request) {
2022-06-09 18:26:01 +00:00
page := Page{name, years, posts, "past"}
2017-11-07 11:16:35 +00:00
renderTemplates(w, page)
}
2021-12-10 18:37:07 +00:00
func keruHandler(w http.ResponseWriter, r *http.Request) {
posts := []Post{getPostByURLTitle("The-Girl-Who-Kicked-a-Rabbit")}
2022-06-09 18:26:01 +00:00
page := Page{name, years, posts, "blog"}
2021-12-10 18:37:07 +00:00
renderTemplates(w, page)
}
2017-11-20 13:37:08 +00:00
func contactHandler(w http.ResponseWriter, r *http.Request) {
2022-06-09 18:26:01 +00:00
page := Page{name, nil, []Post{}, "contact"}
2017-11-20 13:37:08 +00:00
renderTemplates(w, page)
}
2021-11-30 19:23:35 +00:00
func updateHandler(w http.ResponseWriter, r *http.Request) {
retrievePosts()
}
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
}