server

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2018 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Authorities = map[string]int{
	"ROLE_USER":          1,
	"ROLE_ADMINISTRATOR": 2,
}

Authorities represents available ReportPortal roles

Functions

func ReadJSON added in v1.0.1

func ReadJSON(rq *http.Request, val interface{}) error

ReadJSON reads

func RequireRole

func RequireRole(role string, authServerURL string) func(http.Handler) http.Handler

RequireRole checks whether request auth represented by ReportPortal user with provided or higher role

func Validate added in v1.0.3

func Validate(val interface{}) error

Validate validates struct

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

type HTTPError interface {
	error
	Status() int
}

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.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP allows our Handler type to satisfy http.Handler.

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 {
	Sd registry.ServiceDiscovery
	// 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.RpConfig, 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

func (srv *RpServer) AddHealthCheckFunc(f func() error)

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

func (srv *RpServer) WithRouter(f func(router *chi.Mux))

WithRouter gives access to Chi router to add route and perform other modifications

type StatusError

type StatusError struct {
	Code int
	Err  error
}

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) Status

func (se StatusError) Status() int

Status returns our HTTP status code.

type User

type User struct {
	User        string
	Authorities []string
}

User represents logged-in user

func GetUser

func GetUser(ctx context.Context) *User

GetUser obtains value from the Context

type UserInfoErr

type UserInfoErr struct {
	Error            string `json:"error"`
	ErrorDescription string `json:"error_description"`
}

UserInfoErr represents error response from ReportPortal's UAT endpoint

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL