server

package
v0.0.0-...-5c36ebe Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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

func BuildOkResponseWithData(dataName string, data interface{}) map[string]interface{}

BuildOkResponseWithData builds response with status "ok" and data

func BuildResponse

func BuildResponse(status string) map[string]interface{}

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 OrgID

type OrgID uint32

OrgID represents organization ID

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

func (server *Server) Authentication(next http.Handler, noAuthURLs []string) http.Handler

Authentication middleware for checking auth rights

func (*Server) GetAuthToken

func (server *Server) GetAuthToken(request *http.Request) (*Identity, error)

GetAuthToken returns current authentication token

func (*Server) GetCurrentUserID

func (server *Server) GetCurrentUserID(request *http.Request) (UserID, error)

GetCurrentUserID retrieves current user's id from request

func (*Server) Start

func (server *Server) Start() error

Start method starts HTTP or HTTPS server.

func (*Server) Stop

func (server *Server) Stop(ctx context.Context) error

Stop method stops server's execution.

type Token

type Token struct {
	Identity Identity `json:"identity"`
}

Token is x-rh-identity struct

type UserID

type UserID string

UserID represents type for user id

Jump to

Keyboard shortcuts

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