Skip to content

Instantly share code, notes, and snippets.

@gerep
Last active Aug 29, 2015
Embed
What would you like to do?
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