Created
January 25, 2015 18:17
-
-
Save kishorevaishnav/90079db8730a6e0b5544 to your computer and use it in GitHub Desktop.
Simple HTTP & Static rendering in Go
This file contains 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
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