Skip to content

Instantly share code, notes, and snippets.

@kishorevaishnav
Created January 25, 2015 18:17
Show Gist options
  • Save kishorevaishnav/90079db8730a6e0b5544 to your computer and use it in GitHub Desktop.
Save kishorevaishnav/90079db8730a6e0b5544 to your computer and use it in GitHub Desktop.
Simple HTTP & Static rendering in Go
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
var INDEX_HTML []byte
func main() {
fmt.Println("starting server on http://localhost:8888/\nvalue is %s", "asdf")
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/post", PostHandler)
serveSingle("/js/getData.js", "./js/getData.js")
// http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/getData.js"))))
http.ListenAndServe(":8888", nil)
}
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
log.Println("GET /")
w.Write(INDEX_HTML)
}
func PostHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
log.Println("in posthandler", r.Form)
var value = r.FormValue("textfield")
w.Write([]byte(value))
}
func init() {
INDEX_HTML, _ = ioutil.ReadFile("./html/index.html")
}
// http://stackoverflow.com/questions/28138952/ajax-request-not-sending-to-go-web-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment