|  | @@ -1,5 +1,5 @@
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  | -The Kurz Web API exposes these routes:
 | 
	
		
			
				|  |  | +The Kurz Web API exposes HTTP routes for JSON clients, route names to access them, and types for the requests and responses.
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    - GET "/<short>" : resolve a short URL
 | 
	
		
			
				|  |  |      - Handler: handleGetShort()
 | 
	
	
		
			
				|  | @@ -21,15 +21,13 @@ 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
 | 
	
		
			
				|  |  | +These routes are exposed by running SetupRoutes(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"
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -43,19 +41,34 @@ type Target struct {
 | 
	
		
			
				|  |  |  	Target string `json:"target"`
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -/*
 | 
	
		
			
				|  |  | -ListenAndServe() is the
 | 
	
		
			
				|  |  | - */
 | 
	
		
			
				|  |  | -func ListenAndServe(addr string) error {
 | 
	
		
			
				|  |  | -	router := mux.NewRouter()
 | 
	
		
			
				|  |  | +// Route names.
 | 
	
		
			
				|  |  | +const (
 | 
	
		
			
				|  |  | +	RouteGetShort   = "kurz.api.get_short"
 | 
	
		
			
				|  |  | +	RoutePostTarget = "kurz.api.post_target"
 | 
	
		
			
				|  |  | +)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// Content types.
 | 
	
		
			
				|  |  | +const (
 | 
	
		
			
				|  |  | +	// JsonType is the MIME JSON type.
 | 
	
		
			
				|  |  | +	JsonType = "application/json"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	JsonTypeHeader = JsonType + "; charset=utf-8"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	// JsonTypeRegex is a regex matching the MIME JSON type anywhere
 | 
	
		
			
				|  |  | +	JsonTypeRegex = JsonType
 | 
	
		
			
				|  |  | +)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// SetupRoutes() configures Web API routes on the passed mux.Router.
 | 
	
		
			
				|  |  | +//
 | 
	
		
			
				|  |  | +func SetupRoutes(router *mux.Router) {
 | 
	
		
			
				|  |  | +	// BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
 | 
	
		
			
				|  |  |  	router.HandleFunc("/{short}", handleGetShort).
 | 
	
		
			
				|  |  | +		HeadersRegexp("Accept", JsonTypeRegex).
 | 
	
		
			
				|  |  |  		Methods("GET", "HEAD").
 | 
	
		
			
				|  |  | -		Name("kurz.get_short")
 | 
	
		
			
				|  |  | +		Name(RouteGetShort)
 | 
	
		
			
				|  |  |  	router.HandleFunc("/", handlePostTarget).
 | 
	
		
			
				|  |  | -		HeadersRegexp("Content-Type", "^application/json$").
 | 
	
		
			
				|  |  | +		HeadersRegexp("Accept", JsonTypeRegex).
 | 
	
		
			
				|  |  | +		Headers("Content-Type", JsonType).
 | 
	
		
			
				|  |  |  		Methods("POST").
 | 
	
		
			
				|  |  | -		Name("kurd.post_target")
 | 
	
		
			
				|  |  | -	http.Handle("/", router)
 | 
	
		
			
				|  |  | -	err := http.ListenAndServe(addr, router)
 | 
	
		
			
				|  |  | -	return err
 | 
	
		
			
				|  |  | +		Name(RoutePostTarget)
 | 
	
		
			
				|  |  |  }
 |