Skip to content

Instantly share code, notes, and snippets.

@gerep
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerep/b50f74ffb68e8f21859d to your computer and use it in GitHub Desktop.
Save gerep/b50f74ffb68e8f21859d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"log"
)
type ApiFunc func(w http.ResponseWriter, r *http.Request)
func main() {
createRoutes()
log.Println("Listening on :4242")
http.ListenAndServe(":4242", nil)
}
func createRoutes() {
m := map[string]map[string]ApiFunc{
"GET": {
"/ping": ping,
"/pong": pong,
},
}
for method, routes := range m {
for route, function := range routes {
log.Printf("Creating route %s with method %s", route, method)
x := createHandler(method, route, function)
}
}
}
func createHandler(method string, route string, function ApiFunc) http.HandlerFunc {
return http.HandlerFunc(route, function)
}
func ping(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "I'm ping @ %s", r.URL.Path[1:])
}
func pong(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "I'm pong @ %s", r.URL.Path[1:])
// POSTGRESQL
}
package main
import (
"testing"
"fmt"
"net/http"
"net/http/httptest"
)
func TestGetPing(t *testing.T) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://localhost/ping", nil)
if err != nil {
t.Fatal(err)
}
http.DefaultServeMux.ServeHTTP(recorder, req)
fmt.Print(recorder.Body.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment