Skip to content

Instantly share code, notes, and snippets.

@JKobyP
Created July 31, 2017 22:04
Show Gist options
  • Save JKobyP/7cd29579033b4f35749b087a6410f35b to your computer and use it in GitHub Desktop.
Save JKobyP/7cd29579033b4f35749b087a6410f35b to your computer and use it in GitHub Desktop.
import (
"net/http"
"log"
)
func main() {
http.Handle("/", reactServer())
http.Handle("/dist/", assetHandler())
http.HandleFunc("/api/", apiHandler)
api.Init()
log.Fatal(http.ListenAndServe(":8000", nil))
}
func assetHandler() http.Handler {
return http.FileServer(http.Dir("."))
}
func reactServer() http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
log.Printf("Serving index.html")
}
return http.HandlerFunc(fn)
}
func apiHandler(w http.ResponseWriter, req *http.Request) {
cmd := req.URL.Path[len("/api/"):]
if cmd[0:2] == "pr" {
PRHandler(w, req)
} else {
fmt.Fprintf(w, "Support for %v not implemented!\n", cmd)
log.Printf("Support for %v not implemented!\n", cmd)
}
}
func PRHandler(w http.ResponseWriter, req *http.Request) {
components := strings.Split(req.URL.Path[len("/api/pr/"):], "/")
if len(components) != 2 {
log.Printf("invalid url %s", req.URL.Path)
return
}
// etc. etc.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment