micro/api/resolver/resolver.go

45 lines
862 B
Go
Raw Normal View History

2019-06-03 20:44:43 +03:00
// Package resolver resolves a http request to an endpoint
package resolver
import (
2020-04-03 11:18:30 +03:00
"errors"
2019-06-03 20:44:43 +03:00
"net/http"
)
2020-04-03 11:18:30 +03:00
var (
ErrNotFound = errors.New("not found")
ErrInvalidPath = errors.New("invalid path")
)
2019-06-03 20:44:43 +03:00
// Resolver resolves requests to endpoints
type Resolver interface {
Resolve(r *http.Request) (*Endpoint, error)
String() string
}
// Endpoint is the endpoint for a http request
type Endpoint struct {
// e.g greeter
Name string
// HTTP Host e.g example.com
Host string
// HTTP Methods e.g GET, POST
Method string
// HTTP Path e.g /greeter.
Path string
}
type Options struct {
Handler string
2020-04-09 13:03:33 +03:00
Namespace func(*http.Request) string
2019-06-03 20:44:43 +03:00
}
type Option func(o *Options)
2020-04-09 13:03:33 +03:00
// StaticNamespace returns the same namespace for each request
func StaticNamespace(ns string) func(*http.Request) string {
return func(*http.Request) string {
return ns
}
}