Documentation ¶
Index ¶
- Variables
- func ReadJSON(rq *http.Request, val interface{}) error
- func RequireRole(role string, authServerURL string) func(http.Handler) http.Handler
- func Validate(val interface{}) error
- func WriteJSON(status int, body interface{}, w http.ResponseWriter) error
- func WriteJSONP(status int, body interface{}, callback string, w http.ResponseWriter) error
- type HTTPError
- type Handler
- type HealthCheck
- type HealthCheckFunc
- type RpServer
- func (srv *RpServer) AddHandler(method, pattern string, f func(w http.ResponseWriter, r *http.Request) error)
- func (srv *RpServer) AddHealthCheck(h HealthCheck)
- func (srv *RpServer) AddHealthCheckFunc(f func() error)
- func (srv *RpServer) StartServer()
- func (srv *RpServer) WithRouter(f func(router *chi.Mux))
- type StatusError
- type User
- type UserInfoErr
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Authorities = map[string]int{
"ROLE_USER": 1,
"ROLE_ADMINISTRATOR": 2,
}
Authorities represents available ReportPortal roles
Functions ¶
func RequireRole ¶
RequireRole checks whether request auth represented by ReportPortal user with provided or higher role
func WriteJSON ¶ added in v1.0.1
func WriteJSON(status int, body interface{}, w http.ResponseWriter) error
WriteJSON serializes body to provided writer
func WriteJSONP ¶ added in v1.0.1
func WriteJSONP(status int, body interface{}, callback string, w http.ResponseWriter) error
WriteJSONP serializes body as JSONP
Types ¶
type HTTPError ¶
HTTPError represents a handler error. It provides methods for a HTTP status code and embeds the built-in error interface.
type Handler ¶
type Handler struct {
H func(w http.ResponseWriter, r *http.Request) error
}
The Handler struct that takes a configured Env and a function matching our useful signature.
type HealthCheck ¶
type HealthCheck interface {
Check() error
}
HealthCheck specifies interface for health checks
type HealthCheckFunc ¶ added in v1.0.5
type HealthCheckFunc func() error
HealthCheckFunc convenience func to deal with health checks
func (HealthCheckFunc) Check ¶ added in v1.0.5
func (f HealthCheckFunc) Check() error
Check checks healths
type RpServer ¶
type RpServer struct {
// contains filtered or unexported fields
}
RpServer represents ReportPortal micro-service instance
Example ¶
rpConf := conf.EmptyConfig() _ = conf.LoadConfig(rpConf) rp := New(rpConf, commons.GetBuildInfo()) rp.WithRouter(func(router *chi.Mux) { router.Get("/ping", func(w http.ResponseWriter, rq *http.Request) { WriteJSON(http.StatusOK, Person{"av", 20}, w) }) }) rp.StartServer()
Output:
func New ¶
func New(cfg *conf.ServerConfig, buildInfo *commons.BuildInfo) *RpServer
New creates new instance of RpServer struct
func (*RpServer) AddHandler ¶
func (srv *RpServer) AddHandler(method, pattern string, f func(w http.ResponseWriter, r *http.Request) error)
AddHandler is preferred way to add handler to the server. Allows to return error which is representation of HTTP Response
func (*RpServer) AddHealthCheck ¶
func (srv *RpServer) AddHealthCheck(h HealthCheck)
AddHealthCheck adds health check
func (*RpServer) AddHealthCheckFunc ¶ added in v1.0.5
AddHealthCheckFunc adds health check function
func (*RpServer) StartServer ¶
func (srv *RpServer) StartServer()
StartServer starts HTTP server
Example ¶
rpConf := conf.EmptyConfig() authServerURL := "http://localhost:9998/sso/me" _ = conf.LoadConfig(rpConf) srv := New(rpConf, commons.GetBuildInfo()) srv.WithRouter(func(mux *chi.Mux) { mux.Use(func(next http.Handler) http.Handler { return handlers.LoggingHandler(os.Stdout, next) }) secured := chi.NewMux() secured.Use(RequireRole("USER", authServerURL)) me := func(w http.ResponseWriter, rq *http.Request) { WriteJSON(http.StatusOK, rq.Context().Value("user"), w) } secured.HandleFunc("/me", me) mux.Handle("/", secured) }) srv.StartServer()
Output:
func (*RpServer) WithRouter ¶
WithRouter gives access to Chi router to add route and perform other modifications
type StatusError ¶
StatusError represents an error with an associated HTTP status code.
func NewStatusError ¶
func NewStatusError(code int, err string) StatusError
NewStatusError creates new StatusError
func ToStatusError ¶ added in v1.0.2
func ToStatusError(code int, err error) StatusError
ToStatusError creates new StatusError
func (StatusError) Error ¶
func (se StatusError) Error() string
Error allows StatusError to satisfy the error interface.
func (StatusError) StackTrace ¶
func (se StatusError) StackTrace() errors.StackTrace
StackTrace returns stacktrace of child error or nil
type UserInfoErr ¶
type UserInfoErr struct { Error string `json:"error"` ErrorDescription string `json:"error_description"` }
UserInfoErr represents error response from ReportPortal's UAT endpoint