resgateio / resgate

A Realtime API Gateway used with NATS to build REST, real time, and RPC APIs, where all your clients are synchronized seamlessly.
https://resgate.io
MIT License
689 stars 67 forks source link

how to add versioning in api #134

Closed quayilab closed 5 years ago

quayilab commented 5 years ago

Hello, I'm new to Resgate and quite a while with Golang. Is it possible to define api with version? For example: /api/v1.0/profile => will be handled by service "profile_v1", and /api/v2.0/profile => will be handled by service "profile_v2"?

If it is, may be an example in Golang? Thanks

jirenius commented 5 years ago

Hi!

Great question! I have no example for it, but it is rather simple.

Are you using the go-res package for Go? If so, then you can just add the v1 (or v2) as a prefix to the service name:

package main

import res "github.com/jirenius/go-res"

func main() {
    s := res.NewService("v1.profile")
    s.Handle("model",
        res.Access(res.AccessGranted),
        res.GetModel(func(r res.ModelRequest) {
            r.Model(map[string]string{
                "message": "Hello, World!",
            })
        }),
    )
    s.ListenAndServe("nats://localhost:4222")
}

And access it with HTTP:

GET /api/v1/profile/model

Though, because dot (.) is used as separator in resource IDs, you will currently not be able to do /v1.0/ (it would become /v1/0/). Instead you can do /v1/ or perhaps /v1_0/.

PS. Currently I am trying to redirect help questions to forum.resgate.io , so if you have more questions, please post them there :). Then I'll use GitHub more for issues and bug reports.

quayilab commented 5 years ago

Thank you for fast response. Yes! that's what I need /v1_0/ is enough for me.