func Router() *router {
	r := &router{
		redirectHandlers: make(map[string]http.Handler),
	}
	r.Router = mux.NewRouter()
	r.HandleFunc(routePath, func(rw http.ResponseWriter, req *http.Request) {
		// Figure out target address
		var err error
		vars := mux.Vars(req)
		address := fmt.Sprintf("%s://%s", vars["scheme"], vars["host"])
		addressWithPath := fmt.Sprintf("%s%s", address, vars["path"])
		req.URL, err = url.Parse(addressWithPath)
		if err != nil {
			http.Error(rw, fmt.Sprintf("could not parse URL from %s", addressWithPath), http.StatusBadRequest)
			return
		}
		if _, ok := req.Header["User-Agent"]; !ok {
			// explicitly disable User-Agent so it's not set to default value
			req.Header.Set("User-Agent", "")
		}
		// ensure that the handler for a redirect does not change while processing a request
		r.redirectLock.RLock()
		defer r.redirectLock.RUnlock()
		// grab the redirect handler
		handler, ok := r.redirectHandlers[address]
		if !ok {
			http.Error(rw, fmt.Sprintf("redirect address %s has not been registered", address), http.StatusBadRequest)
			return
		}
		// pass request and response to redirect for processing
		handler.ServeHTTP(rw, req)
	})
	return r
}
// Router returns a router that can add or remove redirects
Router returns a router that can add or remove redirects
// Router returns a router that can add or remove redirects
func Router() *router {
	r := &router{
		redirectHandlers: make(map[string]http.Handler),
	}
	r.Router = mux.NewRouter()
	r.HandleFunc(routePath, func(rw http.ResponseWriter, req *http.Request) {
		var err error
		vars := mux.Vars(req)
		address := fmt.Sprintf("%s://%s", vars["scheme"], vars["host"])
		addressWithPath := fmt.Sprintf("%s%s", address, vars["path"])
		req.URL, err = url.Parse(addressWithPath)
		if err != nil {
			http.Error(rw, fmt.Sprintf("could not parse URL from %s", addressWithPath), http.StatusBadRequest)
			return
		}
		if _, ok := req.Header["User-Agent"]; !ok {
			req.Header.Set("User-Agent", "")
		}
		r.redirectLock.RLock()
		defer r.redirectLock.RUnlock()
		handler, ok := r.redirectHandlers[address]
		if !ok {
			http.Error(rw, fmt.Sprintf("redirect address %s has not been registered", address), http.StatusBadRequest)
			return
		}
		handler.ServeHTTP(rw, req)
	})
	return r
}
func Router ( ) * router { r := & router { redirectHandlers : make ( map [ string ] http . Handler ) , } r . Router = mux . NewRouter ( ) r . HandleFunc ( routePath , func ( rw http . ResponseWriter , req * http . Request ) { var err error vars := mux . Vars ( req ) address := fmt . Sprintf ( " " , vars [ " " ] , vars [ " " ] ) addressWithPath := fmt . Sprintf ( " " , address , vars [ " " ] ) req . URL , err = url . Parse ( addressWithPath ) if err != nil { http . Error ( rw , fmt . Sprintf ( " " , addressWithPath ) , http . StatusBadRequest ) return } if _ , ok := req . Header [ " " ] ; ! ok { req . Header . Set ( " " , " " ) } r . redirectLock . RLock ( ) defer r . redirectLock . RUnlock ( ) handler , ok := r . redirectHandlers [ address ] if ! ok { http . Error ( rw , fmt . Sprintf ( " " , address ) , http . StatusBadRequest ) return } handler . ServeHTTP ( rw , req ) } ) return r }
Router returns a router that can add or remove redirects
Router returns a router that can add or remove redirects
// Figure out target address // explicitly disable User-Agent so it's not set to default value // ensure that the handler for a redirect does not change while processing a request // grab the redirect handler // pass request and response to redirect for processing