Documentation ¶
Index ¶
- Constants
- func BuildOkResponse() map[string]interface{}
- func BuildOkResponseWithData(dataName string, data interface{}) map[string]interface{}
- func BuildResponse(status string) map[string]interface{}
- func CORSMiddleware() mux.MiddlewareFunc
- func HandleServerError(writer http.ResponseWriter, err error)
- func Send(statusCode int, w http.ResponseWriter, data interface{}) error
- func SendAccepted(w http.ResponseWriter, data map[string]interface{}) error
- func SendBadRequest(w http.ResponseWriter, errorMessage string) error
- func SendCreated(w http.ResponseWriter, data map[string]interface{}) error
- func SendForbidden(w http.ResponseWriter, errorMessage string) error
- func SendInternalServerError(w http.ResponseWriter, errorMessage string) error
- func SendNotFound(w http.ResponseWriter, errorMessage string) error
- func SendOK(w http.ResponseWriter, data map[string]interface{}) error
- func SendUnauthorized(w http.ResponseWriter, errorMessage string) error
- type AuthConfig
- type Config
- type ContextKey
- type Identity
- type Internal
- type JWTPayload
- type OrgID
- type Server
- func (server *Server) Authentication(next http.Handler, noAuthURLs []string) http.Handler
- func (server *Server) GetAuthToken(request *http.Request) (*Identity, error)
- func (server *Server) GetCurrentUserID(request *http.Request) (UserID, error)
- func (server *Server) Start() error
- func (server *Server) Stop(ctx context.Context) error
- type Token
- type UserID
Constants ¶
const ContextKeyUser = ContextKey("user")
ContextKeyUser is a constant for user authentication token in request
Variables ¶
This section is empty.
Functions ¶
func BuildOkResponse ¶
func BuildOkResponse() map[string]interface{}
BuildOkResponse builds simple "ok" response
func BuildOkResponseWithData ¶
BuildOkResponseWithData builds response with status "ok" and data
func BuildResponse ¶
BuildResponse builds response for RestAPI request
func CORSMiddleware ¶
func CORSMiddleware() mux.MiddlewareFunc
CORSMiddleware handles CORS HTTP headers For more info see https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
func HandleServerError ¶
func HandleServerError(writer http.ResponseWriter, err error)
HandleServerError handles separate server errors and sends appropriate responses
func Send ¶
func Send(statusCode int, w http.ResponseWriter, data interface{}) error
Send sends HTTP response with a provided statusCode data can be either string or map[string]interface{} if data is string it will send response like this: {"status": data} which is helpful for explaining error to the client
Returned error value is based on error returned from json.Encoder
func SendAccepted ¶
func SendAccepted(w http.ResponseWriter, data map[string]interface{}) error
SendAccepted returns response with status Accepted 202
func SendBadRequest ¶
func SendBadRequest(w http.ResponseWriter, errorMessage string) error
SendBadRequest returns error response with status Bad Request 400
func SendCreated ¶
func SendCreated(w http.ResponseWriter, data map[string]interface{}) error
SendCreated returns response with status Created 201
func SendForbidden ¶
func SendForbidden(w http.ResponseWriter, errorMessage string) error
SendForbidden returns response with status Forbidden 403
func SendInternalServerError ¶
func SendInternalServerError(w http.ResponseWriter, errorMessage string) error
SendInternalServerError returns response with status Internal Server Error 500
func SendNotFound ¶
func SendNotFound(w http.ResponseWriter, errorMessage string) error
SendNotFound returns response with status Not Found 404
func SendOK ¶
func SendOK(w http.ResponseWriter, data map[string]interface{}) error
SendOK returns JSON response with status OK 200
func SendUnauthorized ¶
func SendUnauthorized(w http.ResponseWriter, errorMessage string) error
SendUnauthorized returns error response for unauthorized access with status Unauthorized 401
Types ¶
type AuthConfig ¶
type AuthConfig struct { Enabled bool `mapstructure:"enabled" toml:"enabled"` Type string `mapstructure:"type" toml:"type"` }
AuthConfig structure represents auth. settings for the server
type Config ¶
type Config struct { Address string `mapstructure:"address" toml:"address"` UseHTTPS bool `mapstructure:"use_https" toml:"use_https"` EnableCORS bool `mapstructure:"enable_cors" toml:"enable_cors"` CertFolder string // added for testing purposes }
Config data structure represents HTTP/HTTPS server configuration.
type ContextKey ¶
type ContextKey string
ContextKey is a type for user authentication token in request
type Identity ¶
type Identity struct { AccountNumber UserID `json:"account_number"` Internal Internal `json:"internal"` }
Identity contains internal user info
type Internal ¶
type Internal struct {
OrgID OrgID `json:"org_id,string"`
}
Internal contains information about organization ID
type JWTPayload ¶
type JWTPayload struct { AccountNumber UserID `json:"account_number"` OrgID OrgID `json:"org_id,string"` }
JWTPayload is structure that contain data from parsed JWT token
type Server ¶
type Server struct { Config Config AuthConfig AuthConfig Router *mux.Router HTTPServer *http.Server }
Server data structure represents instances of HTTP/HTTPS server.
func New ¶
func New(cfg Config, authCfg AuthConfig, router *mux.Router) *Server
New function constructs new server instance.
func (*Server) Authentication ¶
Authentication middleware for checking auth rights
func (*Server) GetAuthToken ¶
GetAuthToken returns current authentication token
func (*Server) GetCurrentUserID ¶
GetCurrentUserID retrieves current user's id from request