duanjiao3754 2017-03-05 09:16
浏览 423

如何在招摇中创建前缀路由

Usually the examples provided by swagger setup simple servers, where all the api calls for json functions are on the document root. e.g. the famous "hello world" sits at "/hello" .

I want to find a way to define a given prefix, so that can put the a specific api under this prefix. if the prefix would be "/api/2.0", the example api would become

/hello   --> /api/2.0/hello   # The prefix would be "/api/2.0"
/goodbye --> /api/2.0/goodbye # 

in the end i want to keep this dynamic and really provide a commandline switch with some

server -prefix '/api/2.0'

and the server would use this for the api. Note that i can then also specify no prefix, and the system should fall back to "/hello" and "/goodbye".

Is this possible with the go-lang implementation of swagger (https://github.com/go-swagger/go-swagger)

Can somebody point me to an example / or give a short example?

Addendum: I want to do this basically with a swagger 2.0 interface. I learned now that it needs something like this:

http.Handle("/", middlewareOne(middlewareTwo(finalHandler)))

Basically the task is to leave the swagger generated "finalHandler" untouched (so it thinks still handles "only" /hello, or /goodbye). But the middleware would catch all traffic before the finalHandler. The "/api/2.0" would be removed/transformed (this what i am searching, how???) and the middleware would pass this to the finalhandler, which still believes that it serves /hello. I am not sure if the information that it runs "/api/2.0" is ever neeeded, but mabybe it should be added as context.

See https://github.com/go-swagger/go-swagger/blob/master/docs/use/middleware.md

Quirky solution, which seems to work.

func setupGlobalMiddleware(handler http.Handler) http.Handler {
  prefix:="/api/2.0"
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if strings.HasPrefix(r.URL.Path, prefix) {
       r.URL.Path = r.URL.Path[len(prefix):]
    }
    handler.ServeHTTP(w, r)
  })
}

I would rather prefer if would have access to the "handlers" map in the generated code, but it is not accessible. This way i could just "re-register" everything with a prefix. Maybe ther is a better solution?

  • 写回答

1条回答 默认 最新

  • dongshuo1257 2017-03-06 04:51
    关注

    Simple example code. Enjoy.

    package main
    
    import (
        "github.com/julienschmidt/httprouter"
        "net/http"
        "log"
    )
    type (
        API interface {
            Hello(w http.ResponseWriter, r *http.Request)
            Goodbye(w http.ResponseWriter, r *http.Request)
        }
        API2 struct {}
        API3 struct {}
    )
    
    func main() {
        mux := httprouter.New()
        mux.GET("/api/:version/:command",API_HANDLER)
        log.Fatal(http.ListenAndServe(":8091", mux))
    }
    //---------------------------------------------------------------------------
    //  HANDLERS
    //---------------------------------------------------------------------------
    func API_HANDLER(w http.ResponseWriter, r *http.Request, p httprouter.Params){
        var hand API
        switch p.ByName("version") {
        case "2.0": hand = API2{}
        case "3.0": hand = API3{}
        default: hand = nil
        }
        switch p.ByName("command") {
            case "hello": hand.Hello(w,r)
            case "goodbye": hand.Goodbye(w,r)
        }
    }
    //---------------------------------------------------------------------------
    //  API2 version
    //---------------------------------------------------------------------------
    func (a API2) Hello(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("[API2] Hello"))
    }
    func (a API2) Goodbye (w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("[API2] Goodbye"))
    }
    
    //---------------------------------------------------------------------------
    //  API3 version
    //---------------------------------------------------------------------------
    func (a API3) Hello(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("[API3] Hello"))
    }
    func (a API3) Goodbye (w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("[API3] Goodbye"))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题