Last active
August 29, 2015 14:08
-
-
Save gerep/b50f74ffb68e8f21859d to your computer and use it in GitHub Desktop.
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" | |
"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 | |
} |
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 ( | |
"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