Last active
December 18, 2015 00:59
-
-
Save nathanjsharpe/5700981 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Editing {{.Title}}</h1> | |
<form action="/save/{{.Title}}" method="POST"> | |
<div> | |
<textarea name="body" cols="80" rows="20">{{printf "%s" .Body}}</textarea> | |
</div> | |
<div> | |
<input type="submit" value="Save"> | |
</div> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a sample Page. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2013/06/03 16:02:47 http: panic serving 127.0.0.1:39740: runtime error: invalid memory address or nil pointer dereference | |
/home/nathan/go/src/pkg/net/http/server.go:589 (0x43fba1) | |
_func_004: buf.Write(debug.Stack()) | |
/home/nathan/go/src/pkg/runtime/proc.c:1443 (0x40fafa) | |
panic: reflect·call(d->fn, d->args, d->siz); | |
/home/nathan/go/src/pkg/runtime/runtime.c:128 (0x4105c6) | |
panicstring: runtime·panic(err); | |
/home/nathan/go/src/pkg/runtime/thread_linux.c:199 (0x413418) | |
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference"); | |
/home/nathan/go/src/pkg/html/template/template.go:51 (0x42213f) | |
(*Template).Execute: t.nameSpace.mu.Lock() | |
/home/nathan/Dropbox/code/go/gowiki/wiki.go:36 (0x400eef) | |
renderTemplate: t.Execute(w, p) | |
/home/nathan/Dropbox/code/go/gowiki/wiki.go:42 (0x400f7a) | |
viewHandler: renderTemplate(w, "view", p) | |
/home/nathan/go/src/pkg/net/http/server.go:703 (0x4339ea) | |
HandlerFunc.ServeHTTP: f(w, r) | |
/home/nathan/go/src/pkg/net/http/server.go:941 (0x43483e) | |
(*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r) | |
/home/nathan/go/src/pkg/net/http/server.go:669 (0x4337fd) | |
(*conn).serve: handler.ServeHTTP(w, w.req) | |
/home/nathan/go/src/pkg/runtime/proc.c:271 (0x40dc00) | |
goexit: runtime·goexit(void) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>{{.Title}}</h1> | |
<p>[<a href="/edit/{{.Title}}">edit</a>]</p> | |
<div>{{printf "%s", .Body}}</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2010 The Go Authors. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
package main | |
import ( | |
"html/template" | |
"io/ioutil" | |
"net/http" | |
) | |
type Page struct { | |
Title string | |
Body []byte | |
} | |
func (p *Page) save() error { | |
filename := p.Title + ".txt" | |
return ioutil.WriteFile(filename, p.Body, 0600) | |
} | |
func loadPage(title string) (*Page, error) { | |
filename := title + ".txt" | |
body, err := ioutil.ReadFile(filename) | |
if err != nil { | |
return nil, err | |
} | |
return &Page{Title: title, Body: body}, nil | |
} | |
const lenPath = len("/view/") | |
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { | |
t, _ := template.ParseFiles(tmpl + ".html") | |
t.Execute(w, p) | |
} | |
func viewHandler(w http.ResponseWriter, r *http.Request) { | |
title := r.URL.Path[lenPath:] | |
p, _ := loadPage(title) | |
renderTemplate(w, "view", p) | |
} | |
func editHandler(w http.ResponseWriter, r *http.Request) { | |
title := r.URL.Path[lenPath:] | |
p, err := loadPage(title) | |
if err != nil { | |
p = &Page{Title: title} | |
} | |
renderTemplate(w, "edit", p) | |
} | |
func main() { | |
http.HandleFunc("/view/", viewHandler) | |
http.HandleFunc("/edit/", editHandler) | |
//http.HandleFunc("/save/", saveHandler) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment