-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
54 lines (47 loc) · 1.47 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package apid
import (
"github.com/micro/go-micro/metadata"
"golang.org/x/net/context"
"net/http"
"path"
"regexp"
"strings"
)
var (
versionRe = regexp.MustCompilePOSIX("^v[0-9]+$")
)
// Translates /foo/bar/zool into api service go.micro.api.foo method Bar.Zool
// Translates /foo/bar into api service go.micro.api.foo method Foo.Bar
func PathToReceiver(ns, p string) (string, string) {
p = path.Clean(p)
p = strings.TrimPrefix(p, "/")
parts := strings.Split(p, "/")
// If we've got two or less parts
// Use first part as service
// Use all parts as method
if len(parts) <= 2 {
service := ns + strings.Join(parts[:len(parts)-1], ".")
method := strings.Title(strings.Join(parts, "."))
return service, method
}
// Treat /v[0-9]+ as versioning where we have 3 parts
// /v1/foo/bar => service: v1.foo method: Foo.bar
if len(parts) == 3 && versionRe.Match([]byte(parts[0])) {
service := ns + strings.Join(parts[:len(parts)-1], ".")
method := strings.Title(strings.Join(parts[len(parts)-2:], "."))
return service, method
}
// Service is everything minus last two parts
// Method is the last two parts
service := ns + strings.Join(parts[:len(parts)-2], ".")
method := strings.Title(strings.Join(parts[len(parts)-2:], "."))
return service, method
}
func RequestToContext(r *http.Request) context.Context {
ctx := context.Background()
md := make(metadata.Metadata)
for k, v := range r.Header {
md[k] = strings.Join(v, ",")
}
return metadata.NewContext(ctx, md)
}