~huyngo/yue

aee8759966b696edd0a82a8e743a2f5ed3ee9437 — Huy Ngo 11 months ago a45975a
Add about page
3 files changed, 37 insertions(+), 3 deletions(-)

M home.go
M main.go
A static.go
M home.go => home.go +3 -3
@@ 8,19 8,19 @@ import (

func HomePage(w http.ResponseWriter, r *http.Request) {
	files := []string{
		"./templates/pages/home.tmpl",
		"./templates/dynamic/home.tmpl",
		"./templates/base.tmpl",
		"./templates/partials/head.tmpl",
	}

	ts, err := template.ParseFiles(files...)
	tmpl, err := template.ParseFiles(files...)
	if err != nil {
		log.Println(err.Error())
		http.Error(w, "Internal Server Error", 500)
		return
	}

	err = ts.Execute(w, nil)
	err = tmpl.Execute(w, nil)
	if err != nil {
		log.Println(err.Error())
		http.Error(w, "Internal Server Error", 500)

M main.go => main.go +1 -0
@@ 12,6 12,7 @@ func main() {

	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
	r.HandleFunc("/", HomePage)
	r.HandleFunc("/about/", AboutPage)

	http.ListenAndServe(":8080", r)
}

A static.go => static.go +33 -0
@@ 0,0 1,33 @@
package main

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
)

func genericStaticPage(w http.ResponseWriter, r *http.Request, page string) {
	files := []string{
		fmt.Sprintf("./templates/static/%s.tmpl", page),
		"./templates/base.tmpl",
		"./templates/partials/head.tmpl",
	}

	tmpl, err := template.ParseFiles(files...)
	if err != nil {
		log.Println(err.Error())
		http.Error(w, "Internal Server Error", 500)
		return
	}

	err = tmpl.Execute(w, nil)
	if err != nil {
		log.Println(err.Error())
		http.Error(w, "Internal Server Error", 500)
	}
}

func AboutPage(w http.ResponseWriter, r *http.Request) {
	genericStaticPage(w, r, "about")
}