|
@@ -2,7 +2,7 @@
|
|
The Kurz Web API exposes these routes:
|
|
The Kurz Web API exposes these routes:
|
|
|
|
|
|
- GET "/<short>" : resolve a short URL
|
|
- GET "/<short>" : resolve a short URL
|
|
- - Handler: HandleGetShort()
|
|
|
|
|
|
+ - Handler: handleGetShort()
|
|
- Success: 307 to matching target URL
|
|
- Success: 307 to matching target URL
|
|
- Client request incorrect: 400
|
|
- Client request incorrect: 400
|
|
- Short not yet defined: 404 no matching target
|
|
- Short not yet defined: 404 no matching target
|
|
@@ -10,7 +10,7 @@ The Kurz Web API exposes these routes:
|
|
- Target legally censored: 451
|
|
- Target legally censored: 451
|
|
- Server failure: 50*
|
|
- Server failure: 50*
|
|
- POST "/" with <target>: create a short URL from a target URL
|
|
- POST "/" with <target>: create a short URL from a target URL
|
|
- - Handler: HandlePostTarget()
|
|
|
|
|
|
+ - Handler: handlePostTarget()
|
|
- Success: 201 with <new short>
|
|
- Success: 201 with <new short>
|
|
- Already existing short: 409 with <existing short>
|
|
- Already existing short: 409 with <existing short>
|
|
- Target blocked for permission reasons: 403
|
|
- Target blocked for permission reasons: 403
|
|
@@ -23,6 +23,12 @@ mechanisms.
|
|
*/
|
|
*/
|
|
package api
|
|
package api
|
|
|
|
|
|
|
|
+import (
|
|
|
|
+ "net/http"
|
|
|
|
+
|
|
|
|
+ "github.com/gorilla/mux"
|
|
|
|
+)
|
|
|
|
+
|
|
// Short is the type of responses provided by the Web API from a target.
|
|
// Short is the type of responses provided by the Web API from a target.
|
|
type Short struct {
|
|
type Short struct {
|
|
Short string `json:"short"`
|
|
Short string `json:"short"`
|
|
@@ -32,3 +38,17 @@ type Short struct {
|
|
type Target struct {
|
|
type Target struct {
|
|
Target string `json:"target"`
|
|
Target string `json:"target"`
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func ListenAndServe(addr string) error {
|
|
|
|
+ router := mux.NewRouter()
|
|
|
|
+ router.HandleFunc("/{short}", handleGetShort).
|
|
|
|
+ Methods("GET", "HEAD").
|
|
|
|
+ Name("kurz.get_short")
|
|
|
|
+ router.HandleFunc("/", handlePostTarget).
|
|
|
|
+ HeadersRegexp("Content-Type", "^application/json$").
|
|
|
|
+ Methods("POST").
|
|
|
|
+ Name("kurd.post_target")
|
|
|
|
+ http.Handle("/", router)
|
|
|
|
+ err := http.ListenAndServe(addr, router)
|
|
|
|
+ return err
|
|
|
|
+}
|