10000 Added the final concepts into play such as error_handling, making gen… · anarchymonkey/learn_golang@615ce70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 615ce70

Browse files
committed
Added the final concepts into play such as error_handling, making generic templates for consumption & closures
1 parent 384a716 commit 615ce70

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
This is my own file, what are you gonna do man
1+
This is my own file, what are you gonna do man, Nothing
2+
23

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"html/template"
67
"log"
78
"net/http"
9+
"regexp"
810

911
"batsy.com/wiki"
1012
)
1113

14+
const PUBLIC_DIR = "./public"
15+
16+
var templates = template.Must(template.ParseFiles(PUBLIC_DIR+"/template.html", PUBLIC_DIR+"/view.html"))
17+
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-z0-9]+)$")
18+
1219
func handlerFunc(w http.ResponseWriter, req *http.Request) {
1320
w.Header().Add("Content-Type", "text/html")
1421
fmt.Fprintf(w, "This is so awesome %s", req.URL.Path[1:])
@@ -17,56 +24,74 @@ func handlerFunc(w http.ResponseWriter, req *http.Request) {
1724
// remove duplicacy
1825

1926
func renderTemplate(htmlFileLocation string, writer http.ResponseWriter, page *wiki.Page) {
20-
respTemplate, err := template.ParseFiles(htmlFileLocation)
27+
err := templates.ExecuteTemplate(writer, htmlFileLocation, page)
2128

2229
if err != nil {
23-
log.Fatal("Something went wrong", err)
30+
http.Error(writer, err.Error(), http.StatusInternalServerError)
2431
}
32+
}
2533

26-
respTemplate.Execute(writer, page)
34+
func getTitle(writer http.ResponseWriter, req *http.Request) (string, error) {
35+
matched := validPath.FindStringSubmatch(req.URL.Path)
36+
37+
if matched == nil {
38+
http.NotFound(writer, req)
39+
return "", errors.New("invalid page Title")
40+
}
41+
return matched[2], nil
2742
}
2843

29-
func viewHandler(w http.ResponseWriter, req *http.Request) {
30-
title := req.URL.Path[len("/view/"):]
44+
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
45+
return func(w http.ResponseWriter, req *http.Request) {
46+
matched := validPath.FindStringSubmatch(req.URL.Path)
3147

48+
if matched == nil {
49+
http.NotFound(w, req)
50+
return
51+
}
52+
fn(w, req, matched[2])
53+
}
54+
}
55+
56+
func viewHandler(w http.ResponseWriter, req *http.Request, title string) {
3257
p, err := wiki.LoadPage(title)
3358

3459
if err != nil {
3560
http.Redirect(w, req, "/edit/"+title, http.StatusFound)
3661
return
3762
}
3863

39-
renderTemplate("./public/view.html", w, p)
64+
renderTemplate("view.html", w, p)
4065
}
4166

42-
func editHandler(w http.ResponseWriter, req *http.Request) {
43-
title := req.URL.Path[len("/edit/"):]
44-
67+
func editHandler(w http.ResponseWriter, req *http.Request, title string) {
4568
page, err := wiki.LoadPage(title)
4669

4770
if err != nil {
4871
page = &wiki.Page{Title: "Tempfile"}
4972
}
5073

51-
renderTemplate("./public/template.html", w, page)
74+
renderTemplate("template.html", w, page)
5275
}
5376

54-
func saveHandler(w http.ResponseWriter, req *http.Request) {
55-
title := req.URL.Path[len("/save/"):]
77+
func saveHandler(w http.ResponseWriter, req *http.Request, title string) {
5678
body := req.FormValue("body")
5779

5880
page := &wiki.Page{Title: title, Body: []byte(body)}
59-
page.Save()
81+
err := page.Save()
6082

83+
if err != nil {
84+
http.Error(w, err.Error(), http.StatusInternalServerError)
85+
}
6186
http.Redirect(w, req, "/view/"+title, http.StatusFound)
6287
}
6388

6489
func main() {
6590

6691
http.HandleFunc("/", handlerFunc)
67-
http.HandleFunc("/view/", viewHandler)
68-
http.HandleFunc("/edit/", editHandler)
69-
http.HandleFunc("/save/", saveHandler)
92+
http.HandleFunc("/view/", makeHandler(viewHandler))
93+
http.HandleFunc("/edit/", makeHandler(editHandler))
94+
http.HandleFunc("/save/", makeHandler(saveHandler))
7095

7196
log.Fatal(http.ListenAndServe(":8080", nil))
7297
}

0 commit comments

Comments
 (0)
0