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