go webアプリケーション その1
webアプリケーションの開発 入門
localhostでwebサーバーを動かす方法について解説します。
1. まずはインポートパッケージと構造体を定義します。
package main import ( "html/template" "io/ioutil" "log" "net/http" "regexp" ) //Pageの構造体 type Page struct { Title string Body []byte }
2. 次に引数のページからタイトル、Bodyを抜き出し保存します。
//ファイルの保存 タイトル名+.txt (中身はpのBody) func (p *Page) save() error { filename := p.Title + ".txt" return ioutil.WriteFile(filename, p.Body, 0600) }
3. タイトル名からファイルをロードし、Bodyを抜きだす。
//ページのロードを行う ファイルが存在しない場合はBodyはnilを返す func loadPage(title string) (*Page, error) { filename := title + ".txt" body, _ := ioutil.ReadFile(filename) return &Page{Title: title, Body: body}, nil }
4. html/templateパッケージのメソッドを使用する。template.ParseFiles()を使用し、htmlエスケープ処理を行う。
テンプレートファイルのパース処理が成功するのが自明の場合には、template.Must()を使用する。(エラー時はpanicを起こす。)
var templates = template.Must(template.ParseFiles("edit.html", "view.html")) //templateを作ることで読み込みを短縮する func renderTmplate(w http.ResponseWriter, tmpl string, p *Page) { // t, _ := template.ParseFiles(tmpl + ".html") // t.Execute(w, p) err := templates.ExecuteTemplate(w, tmpl+".html", p) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
5. ハンドラー処理。
viewのハンドラーではページをロードし、ファイルが存在すればeditへ、なければ新しいファイルを作成してeditに渡す。
editのハンドラーではページをロードし(必ず存在する)、編集画面を表示する。
saveのハンドラーではページのタイトルと内容を抜き出し、保存。viewパスに返す。
// func viewHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { http.Redirect(w, r, "/edit/"+title, http.StatusFound) return } renderTmplate(w, "view", p) } func saveHandler(w http.ResponseWriter, r *http.Request, title string) { body := r.FormValue("body") p := &Page{Title: title, Body: []byte(body)} err := p.save() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } http.Redirect(w, r, "/view/"+title, http.StatusFound) } func editHandler(w http.ResponseWriter, r *http.Request, title string) { p, err := loadPage(title) if err != nil { p = &Page{Title: title} } renderTmplate(w, "edit", p) }
6. 最後にmakeHundler()について解説します。
makeHundler()を使うことでtitleの抜き取りを一括で行えます。
var validPath = regexp.MustCompile("^/(edit|view|save)/([a-zA-Z0-9]+)$") func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { m := validPath.FindStringSubmatch(r.URL.Path) if m == nil { http.NotFound(w, r) return } fn(w, r, m[2]) } }
最後にメイン関数。
func main() { http.HandleFunc("/view/", makeHandler(viewHandler)) http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/save/", makeHandler(saveHandler)) log.Fatalln(http.ListenAndServe(":8080", nil)) }
view.html
<h1>{{.Title}}</h1> <p>[<a href="/edit/{{.Title}}">Edit</a>]</p> <div>{{printf "%s" .Body}}</div>
edit.html
<h1>Editting {{.Title}}</h1> <form action="/save/{{.Title}}" method="POST"> <div> <textarea name="body" rows="20" cols="80">{{printf "%s" .Body}}</textarea> </div> <div> <input type="submit" value="save" /> </div> </form>