12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /*
- The Kurz Web API exposes these routes:
- - GET "/<short>" : resolve a short URL
- - Handler: handleGetShort()
- - Success: 307 to matching target URL
- - Client request incorrect: 400
- - Short not yet defined: 404 no matching target
- - Target blocked for permission reasons: 403
- - Target legally censored: 451
- - Server failure: 50*
- - POST "/" with <target>: create a short URL from a target URL
- - Handler: handlePostTarget()
- - Success: 201 with <new short>
- - Already existing short: 409 with <existing short>
- - Target blocked for permission reasons: 403
- - Target legally censored: 451
- - Server failure: 50*
- Code 451 MAY be replaced by 403, for example when legal censorship includes a
- gag order, super-injunction (UK), National security letter (US) or similar
- mechanisms.
- These routes are exposed by running ListenAndServe(address), which is enough to
- configure the Kurz domain API. Be sure to also configure the domain SPI to have
- a complete application.
- */
- package api
- import (
- "net/http"
- "github.com/gorilla/mux"
- )
- // Short is the type of responses provided by the Web API from a target.
- type Short struct {
- Short string `json:"short"`
- }
- // Target is the type of requests accepted by the Web API for target submissions.
- type Target struct {
- Target string `json:"target"`
- }
- /*
- ListenAndServe() is the
- */
- 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
- }
|