The code below renders the initial page, which has a form in it. On submission of that form, I want to render a results page. The form submits and is handled, but all I see is a blank html document.
I don't want to only display an html page, but render it as some of the content will come from my golang code on form submission.I am trying to do this using templating ( from a template ) where designated lines in the <body></body>
are the values of Golang variables.
I would greatly appreciate it if someone could help me figure out how to render a results page.
package main
import (
//"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"html/template"
"io/ioutil"
)
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
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page){
t, _ := template.ParseFiles("Projects/Go/src/web/site/" + tmpl + ".html")
t.Execute(w, p)
}
func editHandler(w http.ResponseWriter, r *http.Request){
title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil{
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func viewHandler(w http.ResponseWriter, r *http.Request){
title := r.URL.Path[len("/ask"):]
p, _ := loadPage(title)
renderTemplate(w, "ask", p)
}
func response(c web.C, w http.ResponseWriter, r *http.Request){
//name := r.FormValue("name")
//fmt.Fprintf(w, "Hello, %s!", name)
http.HandleFunc("/ask", viewHandler)
http.HandleFunc("/edit/", editHandler)
//http.HandleFunc("/save", saveHandler)
http.ListenAndServe("8000", nil)
}
func serveSingle(filename string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
}
}
func main() {
goji.Get("/", serveSingle("Projects/Go/src/web/site/index.html"))
goji.Handle("/ask", response)
goji.Serve()
}
file structure:
root/Projects/Go/src/web/site/edit.html
root/Projects/Go/src/web/site/index.html
root/Projects/Go/src/web/site/view.html
index.html's body:
<form action="ask" method="get">
<input type="text" name="q" />
</form>
view.html's body:
<form action="ask" method="get">
<input type="text" name="q" />
</form>
<h1 class="abTitle">{{printf "%s" .Body}}</h1>
view.html and edit.html are the exact same.
Theoretically, the script you published should respond on the /
requests and crash on requests to the /ask
. On the request to the /ask
it should execute the response
handler and panic (as the documentation specifies http://golang.org/pkg/net/http/#ServeMux.Handle But what exactly will happen is quite unclear. Anyway, it will be more idiomatic (for such small server) to configure all the handlers in advance and avoid registering handlers in responses.
Another issue is in the viewHandler
function - it will be called with /ask
request only, but then the /ask
will be stripped and the title will be "" - this will lead to loading ".txt" template file, which is probably not intentional.
It will be also helpful if you post the content of your form template and files structure (folders and file names).