Skip to content

Instantly share code, notes, and snippets.

@andreinechaev
Created February 20, 2016 07:03
Show Gist options
  • Save andreinechaev/0d3271a5593a8c80ca54 to your computer and use it in GitHub Desktop.
Save andreinechaev/0d3271a5593a8c80ca54 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"text/template"
"log"
"fmt"
)
type Page struct {
Title string
}
func render(w http.ResponseWriter, tmpl string) {
w.Header().Set("Content-Type", "text/html")
tmpl = fmt.Sprintf("templates/%s", tmpl)
t, err := template.ParseFiles(tmpl)
if err != nil {
log.Print("template parsing error: ", err)
}
if err = t.Execute(w, nil); err != nil {
log.Print("template executing error: ", err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
render(w, "index.html")
}
func reduplicator(w http.ResponseWriter, r *http.Request) {
render(w, "lexical_reduplicator.html")
}
func main() {
http.HandleFunc("/images/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
http.HandleFunc("/", index)
http.HandleFunc("/lex", reduplicator)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment