Documentation
¶
Overview ¶
Package servex provides a basic HTTP(S) server based on a net/http and gorilla/mux.
Index ¶
- Constants
- Variables
- func GetFromContext[T any](r *http.Request, key any) (empty T)
- func GetTLSConfig(cert *tls.Certificate) *tls.Config
- func MakeRawRequest(path, host string, headers map[string]string, body ...string) []byte
- func MakeRawResponse(code int, headers map[string]string, body ...string) []byte
- func Read(r *http.Request) ([]byte, error)
- func ReadAndValidate[T interface{ ... }](r *http.Request) (T, error)
- func ReadCertificate(cert, key []byte) (tls.Certificate, error)
- func ReadCertificateFromFile(certFile, keyFile string) (tls.Certificate, error)
- func ReadFile(r *http.Request, fileKey string) ([]byte, *multipart.FileHeader, error)
- func ReadJSON[T any](r *http.Request) (T, error)
- func RegisterLoggingMiddleware(router MiddlewareRouter, logger RequestLogger, metrics Metrics, ...)
- func RegisterRateLimitMiddleware(router MiddlewareRouter, cfg RateLimitConfig)
- func RegisterRecoverMiddleware(router MiddlewareRouter, logger ErrorLogger)
- func RegisterSimpleAuthMiddleware(router MiddlewareRouter, authToken string)
- func Start(cfg BaseConfig, handlerSetter func(*mux.Router), opts ...Option) (shutdown func(context.Context) error, err error)
- func StartWithShutdown(ctx context.Context, cfg BaseConfig, handlerSetter func(*mux.Router), ...) error
- type AuthConfig
- type AuthDatabase
- type AuthManager
- func (h *AuthManager) CreateUser(ctx context.Context, username, password string, roles ...UserRole) error
- func (h *AuthManager) GetAllUsersHandler(w http.ResponseWriter, r *http.Request)
- func (h *AuthManager) GetCurrentUserHandler(w http.ResponseWriter, r *http.Request)
- func (h *AuthManager) LoginHandler(w http.ResponseWriter, r *http.Request)
- func (h *AuthManager) LogoutHandler(w http.ResponseWriter, r *http.Request)
- func (h *AuthManager) RefreshHandler(w http.ResponseWriter, r *http.Request)
- func (h *AuthManager) RegisterHandler(w http.ResponseWriter, r *http.Request)
- func (h *AuthManager) RegisterRoutes(r *mux.Router)
- func (h *AuthManager) UpdateUserRoleHandler(w http.ResponseWriter, r *http.Request)
- func (m *AuthManager) WithAuth(next http.HandlerFunc, roles ...UserRole) http.HandlerFunc
- type BaseConfig
- type BaseRequestLogger
- type Context
- func (ctx *Context) APIVersion() string
- func (ctx *Context) BadGateway(err error, msg string, args ...any)
- func (ctx *Context) BadRequest(err error, msg string, args ...any)
- func (ctx *Context) Conflict(err error, msg string, args ...any)
- func (ctx *Context) Cookie(key string) (*http.Cookie, error)
- func (ctx *Context) Error(err error, code int, msg string, args ...any)
- func (ctx *Context) Forbidden(err error, msg string, args ...any)
- func (ctx *Context) FormValue(key string) string
- func (ctx *Context) Header(key string) string
- func (ctx *Context) InternalServerError(err error, msg string, args ...any)
- func (ctx *Context) LogFields(fieldsToInclude ...string) []any
- func (ctx *Context) NoLog()
- func (ctx *Context) NotAcceptable(err error, msg string, args ...any)
- func (ctx *Context) NotFound(err error, msg string, args ...any)
- func (ctx *Context) NotImplemented(err error, msg string, args ...any)
- func (ctx *Context) ParseUnixFromQuery(key string) (time.Time, error)
- func (ctx *Context) Path(key string) string
- func (ctx *Context) Query(key string) string
- func (ctx *Context) Read() ([]byte, error)
- func (ctx *Context) ReadAndValidate(body interface{ ... }) error
- func (ctx *Context) ReadFile(fileKey string) ([]byte, *multipart.FileHeader, error)
- func (ctx *Context) ReadJSON(body any) error
- func (ctx *Context) RequestID() string
- func (ctx *Context) Response(code int, bodyRaw ...any)
- func (ctx *Context) ResponseFile(filename string, mimeType string, body []byte)
- func (ctx *Context) ServiceUnavailable(err error, msg string, args ...any)
- func (ctx *Context) SetContentType(mimeType string, charset ...string)
- func (ctx *Context) SetCookie(name, value string, maxAge int, secure, httpOnly bool)
- func (ctx *Context) SetHeader(key string, value ...string)
- func (ctx *Context) SetRawCookie(c *http.Cookie)
- func (ctx *Context) SetSendErrorToClient(sendErrorToClient bool)
- func (ctx *Context) TooManyRequests(err error, msg string, args ...any)
- func (ctx *Context) Unauthorized(err error, msg string, args ...any)
- func (ctx *Context) UnprocessableEntity(err error, msg string, args ...any)
- type ErrorLogger
- type ErrorResponse
- type InitialUser
- type Logger
- type MemoryAuthDatabase
- func (db *MemoryAuthDatabase) FindAll(ctx context.Context) ([]User, error)
- func (db *MemoryAuthDatabase) FindByID(ctx context.Context, id string) (User, bool, error)
- func (db *MemoryAuthDatabase) FindByUsername(ctx context.Context, username string) (User, bool, error)
- func (db *MemoryAuthDatabase) NewUser(ctx context.Context, username string, passwordHash string, roles ...UserRole) (string, error)
- func (db *MemoryAuthDatabase) UpdateUser(ctx context.Context, id string, diff *UserDiff) error
- type Metrics
- type MiddlewareRouter
- type Option
- func WithAuth(db AuthDatabase) Option
- func WithAuthBasePath(path string) Option
- func WithAuthConfig(auth AuthConfig) Option
- func WithAuthInitialRoles(roles ...UserRole) Option
- func WithAuthInitialUsers(users ...InitialUser) Option
- func WithAuthIssuer(issuer string) Option
- func WithAuthKey(accessKey, refreshKey string) Option
- func WithAuthMemoryDatabase() Option
- func WithAuthNotRegisterRoutes(notRegisterRoutes bool) Option
- func WithAuthRefreshTokenCookieName(name string) Option
- func WithAuthToken(t string) Option
- func WithAuthTokensDuration(accessDuration, refreshDuration time.Duration) Option
- func WithBurstSize(burstSize int) Option
- func WithCertificate(cert tls.Certificate) Option
- func WithIdleTimeout(tm time.Duration) Option
- func WithLogger(l Logger) Option
- func WithMetrics(m Metrics) Option
- func WithNoLogClientErrors() Option
- func WithNoRateInAuthRoutes() Option
- func WithNoRequestLog() Option
- func WithRPM(rpm int) Option
- func WithRPS(rps int) Option
- func WithRateLimitConfig(rateLimit RateLimitConfig) Option
- func WithRateLimitExcludePaths(paths ...string) Option
- func WithRateLimitIncludePaths(paths ...string) Option
- func WithRateLimitKeyFunc(keyFunc func(r *http.Request) string) Option
- func WithRateLimitMessage(message string) Option
- func WithRateLimitStatusCode(statusCode int) Option
- func WithReadHeaderTimeout(tm time.Duration) Option
- func WithReadTimeout(tm time.Duration) Option
- func WithRequestLogger(r RequestLogger) Option
- func WithRequestsPerInterval(requestsPerInterval int, interval time.Duration) Option
- func WithSendErrorToClient(sendErrorToClient bool) Option
- type Options
- type RateLimitConfig
- type RequestLogBundle
- type RequestLogger
- type RoleContextKey
- type Server
- func (s *Server) AddMiddleware(middleware ...func(http.Handler) http.Handler)
- func (s *Server) AuthManager() *AuthManager
- func (s *Server) H(path string, h http.Handler, methods ...string) *mux.Route
- func (s *Server) HA(path string, f http.HandlerFunc, roles ...UserRole) *mux.Route
- func (s *Server) HF(path string, f http.HandlerFunc, methods ...string) *mux.Route
- func (s *Server) HFA(path string, f http.HandlerFunc, roles ...UserRole) *mux.Route
- func (s *Server) HTTPAddress() string
- func (s *Server) HTTPSAddress() string
- func (s *Server) Handle(path string, h http.Handler, methods ...string) *mux.Route
- func (s *Server) HandleFunc(path string, f http.HandlerFunc, methods ...string) *mux.Route
- func (s *Server) HandleFuncWithAuth(path string, f http.HandlerFunc, roles ...UserRole) *mux.Route
- func (s *Server) HandleWithAuth(path string, h http.Handler, roles ...UserRole) *mux.Route
- func (s *Server) IsAuthEnabled() bool
- func (s *Server) IsHTTP() bool
- func (s *Server) IsTLS() bool
- func (s *Server) R(path ...string) *mux.Router
- func (s *Server) Router(path ...string) *mux.Router
- func (s *Server) Shutdown(ctx context.Context) error
- func (s *Server) Start(httpAddr, httpsAddr string) error
- func (s *Server) StartHTTP(address string) error
- func (s *Server) StartHTTPS(address string) error
- func (s *Server) StartWithShutdown(ctx context.Context, httpAddr, httpsAddr string) error
- func (s *Server) StartWithShutdownHTTP(ctx context.Context, address string) error
- func (s *Server) StartWithShutdownHTTPS(ctx context.Context, address string) error
- func (s *Server) WithAuth(next http.HandlerFunc, roles ...UserRole) http.HandlerFunc
- func (s *Server) WithBasePath(path string) *Server
- type User
- type UserContextKey
- type UserDiff
- type UserLoginRequest
- type UserLoginResponse
- type UserRole
- type UserUpdateRequest
Constants ¶
const ( IDDBField = "id" IDDBBsonField = "_id" UsernameDBField = "username" RolesDBField = "roles" PasswordHashDBField = "password_hash" RefreshTokenHashDBField = "refresh_token_hash" RefreshTokenExpiresAtDBField = "refresh_token_expires_at" )
const ( // RequestIDLogField adds request ID to logs. RequestIDLogField = "request_id" // IPLogField adds remote IP address to logs. IPLogField = "ip" // UserAgentLogField adds client's User-Agent to logs. UserAgentLogField = "user_agent" // URLLogField adds request URL to logs. URLLogField = "url" // MethodLogField adds request method to logs like GET or POST. MethodLogField = "method" // ProtoLogField adds request protocol to logs like HTTP/1.1 or HTTP/2. ProtoLogField = "proto" )
const ( // MIMETypeAAC defines the MIME type for AAC audio. MIMETypeAAC = "audio/aac" // MIMETypeABW defines the MIME type for AbiWord documents. MIMETypeABW = "application/x-abiword" // MIMETypeAPNG defines the MIME type for Animated Portable Network Graphics (APNG). MIMETypeAPNG = "image/apng" // MIMETypeARC defines the MIME type for Archive documents (multiple files embedded). MIMETypeARC = "application/x-freearc" // MIMETypeAVIF defines the MIME type for AVIF images. MIMETypeAVIF = "image/avif" // MIMETypeAVI defines the MIME type for AVI (Audio Video Interleave). MIMETypeAVI = "video/x-msvideo" // MIMETypeAZW defines the MIME type for Amazon Kindle eBook format. MIMETypeAZW = "application/vnd.amazon.ebook" // MIMETypeBIN defines the MIME type for any kind of binary data. MIMETypeBIN = "application/octet-stream" // MIMETypeBMP defines the MIME type for Windows OS/2 Bitmap Graphics. MIMETypeBMP = "image/bmp" // MIMETypeBZ defines the MIME type for BZip archives. MIMETypeBZ = "application/x-bzip" // MIMETypeBZ2 defines the MIME type for BZip2 archives. MIMETypeBZ2 = "application/x-bzip2" // MIMETypeCDA defines the MIME type for CD audio. MIMETypeCDA = "application/x-cdf" // MIMETypeCSH defines the MIME type for C-Shell scripts. MIMETypeCSH = "application/x-csh" // MIMETypeCSS defines the MIME type for Cascading Style Sheets (CSS). MIMETypeCSS = "text/css" // MIMETypeCSV defines the MIME type for Comma-separated values (CSV). MIMETypeCSV = "text/csv" // MIMETypeDOC defines the MIME type for Microsoft Word. MIMETypeDOC = "application/msword" // MIMETypeDOCX defines the MIME type for Microsoft Word (OpenXML). MIMETypeDOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" // MIMETypeEOT defines the MIME type for MS Embedded OpenType fonts. MIMETypeEOT = "application/vnd.ms-fontobject" // MIMETypeEPUB defines the MIME type for Electronic publications (EPUB). MIMETypeEPUB = "application/epub+zip" // MIMETypeGZ defines the MIME type for GZip Compressed Archives. MIMETypeGZ = "application/gzip" // MIMETypeGIF defines the MIME type for Graphics Interchange Format (GIF). MIMETypeGIF = "image/gif" // MIMETypeHTML defines the MIME type for HyperText Markup Language (HTML). MIMETypeHTML = "text/html" // MIMETypeICO defines the MIME type for Icon format. MIMETypeICO = "image/vnd.microsoft.icon" // MIMETypeICS defines the MIME type for iCalendar format. MIMETypeICS = "text/calendar" // MIMETypeJAR defines the MIME type for Java Archives (JAR). MIMETypeJAR = "application/java-archive" // MIMETypeJPEG defines the MIME type for JPEG images. MIMETypeJPEG = "image/jpeg" // MIMETypeJS defines the MIME type for JavaScript. MIMETypeJS = "text/javascript" // MIMETypeJSON defines the MIME type for JSON format. MIMETypeJSON = "application/json" // MIMETypeJSONLD defines the MIME type for JSON-LD format. MIMETypeJSONLD = "application/ld+json" // MIMETypeMIDI defines the MIME type for Musical Instrument Digital Interface (MIDI). MIMETypeMIDI = "audio/midi" // MIMETypeMJS defines the MIME type for JavaScript modules. MIMETypeMJS = "text/javascript" // MIMETypeMP3 defines the MIME type for MP3 audio. MIMETypeMP3 = "audio/mpeg" // MIMETypeMP4 defines the MIME type for MP4 video. MIMETypeMP4 = "video/mp4" // MIMETypeMPEG defines the MIME type for MPEG Video. MIMETypeMPEG = "video/mpeg" // MIMETypeMPKG defines the MIME type for Apple Installer Packages. MIMETypeMPKG = "application/vnd.apple.installer+xml" // MIMETypeODP defines the MIME type for OpenDocument presentation documents. MIMETypeODP = "application/vnd.oasis.opendocument.presentation" // MIMETypeODS defines the MIME type for OpenDocument spreadsheet documents. MIMETypeODS = "application/vnd.oasis.opendocument.spreadsheet" // MIMETypeODT defines the MIME type for OpenDocument text documents. MIMETypeODT = "application/vnd.oasis.opendocument.text" // MIMETypeOGA defines the MIME type for Ogg audio. MIMETypeOGA = "audio/ogg" // MIMETypeOGV defines the MIME type for Ogg video. MIMETypeOGV = "video/ogg" // MIMETypeOGX defines the MIME type for Ogg. MIMETypeOGX = "application/ogg" // MIMETypeOPUS defines the MIME type for Opus audio in Ogg container. MIMETypeOPUS = "audio/ogg" // MIMETypeOTF defines the MIME type for OpenType fonts. MIMETypeOTF = "font/otf" // MIMETypePNG defines the MIME type for Portable Network Graphics. MIMETypePNG = "image/png" // MIMETypePDF defines the MIME type for Adobe Portable Document Format (PDF). MIMETypePDF = "application/pdf" // MIMETypePHP defines the MIME type for Hypertext Preprocessor (Personal Home Page). MIMETypePHP = "application/x-httpd-php" // MIMETypePPT defines the MIME type for Microsoft PowerPoint. MIMETypePPT = "application/vnd.ms-powerpoint" // MIMETypePPTX defines the MIME type for Microsoft PowerPoint (OpenXML). MIMETypePPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation" // MIMETypeRAR defines the MIME type for RAR archives. MIMETypeRAR = "application/vnd.rar" // MIMETypeRTF defines the MIME type for Rich Text Format (RTF). MIMETypeRTF = "application/rtf" // MIMETypeSH defines the MIME type for Bourne shell scripts. MIMETypeSH = "application/x-sh" // MIMETypeSVG defines the MIME type for Scalable Vector Graphics (SVG). MIMETypeSVG = "image/svg+xml" // MIMETypeTAR defines the MIME type for Tape Archives (TAR). MIMETypeTAR = "application/x-tar" // MIMETypeTIFF defines the MIME type for Tagged Image File Format (TIFF). MIMETypeTIFF = "image/tiff" // MIMETypeTS defines the MIME type for MPEG transport stream. MIMETypeTS = "video/mp2t" // MIMETypeTTF defines the MIME type for TrueType Fonts. MIMETypeTTF = "font/ttf" // MIMETypeTXT defines the MIME type for Plain Text. MIMETypeTXT = "text/plain" // MIMETypeText is an alias for MIMETypeTXT. MIMETypeText = MIMETypeTXT // MIMETypePlain is an alias for MIMETypeTXT. MIMETypePlain = MIMETypeTXT // MIMETypeVSD defines the MIME type for Microsoft Visio. MIMETypeVSD = "application/vnd.visio" // MIMETypeWAV defines the MIME type for Waveform Audio Format. MIMETypeWAV = "audio/wav" // MIMETypeWEBA defines the MIME type for WEBM audio. MIMETypeWEBA = "audio/webm" // MIMETypeWEBM defines the MIME type for WEBM video. MIMETypeWEBM = "video/webm" // MIMETypeWEBP defines the MIME type for WEBP images. MIMETypeWEBP = "image/webp" // MIMETypeWOFF defines the MIME type for Web Open Font Format (WOFF). MIMETypeWOFF = "font/woff" // MIMETypeWOFF2 defines the MIME type for Web Open Font Format (WOFF2). MIMETypeWOFF2 = "font/woff2" // MIMETypeXHTML defines the MIME type for XHTML. MIMETypeXHTML = "application/xhtml+xml" // MIMETypeXLS defines the MIME type for Microsoft Excel. MIMETypeXLS = "application/vnd.ms-excel" // MIMETypeXLSX defines the MIME type for Microsoft Excel (OpenXML). MIMETypeXLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // MIMETypeXML defines the MIME type for XML. MIMETypeXML = "application/xml" // MIMETypeXUL defines the MIME type for XUL. MIMETypeXUL = "application/vnd.mozilla.xul+xml" // MIMETypeZIP defines the MIME type for ZIP archives. MIMETypeZIP = "application/zip" // MIMEType3GP defines the MIME type for 3GPP audio/video containers. MIMEType3GP = "video/3gpp" // MIMEType3G2 defines the MIME type for 3GPP2 audio/video containers. MIMEType3G2 = "video/3gpp2" // MIMEType7Z defines the MIME type for 7-zip archives. MIMEType7Z = "application/x-7z-compressed" )
const ( // GET is the HTTP GET method. GET = http.MethodGet // HEAD is the HTTP HEAD method. HEAD = http.MethodHead // POST is the HTTP POST method. POST = http.MethodPost // PUT is the HTTP PUT method. PUT = http.MethodPut // PATCH is the HTTP PATCH method. PATCH = http.MethodPatch // DELETE is the HTTP DELETE method. DELETE = http.MethodDelete // CONNECT is the HTTP CONNECT method. CONNECT = http.MethodConnect // OPTIONS is the HTTP OPTIONS method. OPTIONS = http.MethodOptions // TRACE is the HTTP TRACE method. TRACE = http.MethodTrace )
HTTP methods shortcuts
Variables ¶
var ( // ListenAddressRegexp is used to match "ip:port" or ":port" strings or kuber domains with port. ListenAddressRegexp = regexp.MustCompile(`^[\w\-\/:@\.]*:[0-9]{1,5}$`) )
Functions ¶
func GetFromContext ¶ added in v1.3.0
func GetTLSConfig ¶ added in v1.1.0
func GetTLSConfig(cert *tls.Certificate) *tls.Config
GetTLSConfig creates a *tls.Config suitable for an HTTPS server using the provided certificate. It returns nil if the certificate is nil. The config enables HTTP/2, prefers server cipher suites, sets minimum TLS version to 1.2, and includes a list of secure cipher suites and curve preferences.
func MakeRawRequest ¶
MakeRawRequest makes a raw HTTP request in a form of []byte.
func MakeRawResponse ¶
MakeRawResponse makes a raw HTTP response in a form of []byte.
func ReadAndValidate ¶
ReadAndValidate reads a JSON from the request body to a variable of the provided type and validates it.
func ReadCertificate ¶
func ReadCertificate(cert, key []byte) (tls.Certificate, error)
ReadCertificate is a function that reads a TLS certificate from the given cert and key bytes and returns a tls.Certificate instance.
func ReadCertificateFromFile ¶
func ReadCertificateFromFile(certFile, keyFile string) (tls.Certificate, error)
ReadCertificateFromFile is a function that reads a TLS certificate from the given cert and key files and returns a tls.Certificate instance.
func ReadFile ¶ added in v1.2.0
ReadFile reads a file from the request body. fileKey is the key of the file in the request. It returns the file bytes, the file header and an error.
func RegisterLoggingMiddleware ¶ added in v1.1.0
func RegisterLoggingMiddleware(router MiddlewareRouter, logger RequestLogger, metrics Metrics, noLogClientErrors ...bool)
RegisterLoggingMiddleware registers a middleware that logs incoming requests. It logs details such as request method, path, status code, duration, and any errors encountered during processing. It also integrates with a Metrics handler if provided. If the logger is nil, it defaults to a BaseRequestLogger using slog.Default(). Requests can be excluded from logging by calling ctx.NoLog() within the handler.
func RegisterRateLimitMiddleware ¶ added in v1.1.0
func RegisterRateLimitMiddleware(router MiddlewareRouter, cfg RateLimitConfig)
RegisterRateLimitMiddleware adds rate limiting middleware to the router. If the config is not enabled, no middleware will be registered.
func RegisterRecoverMiddleware ¶ added in v1.1.0
func RegisterRecoverMiddleware(router MiddlewareRouter, logger ErrorLogger)
RegisterRecoverMiddleware registers a middleware that recovers from panics in HTTP handlers. If a panic occurs, it logs the error and stack trace using the provided logger (defaulting to slog.Default() if nil) and sends a 500 Internal Server Error response only if no response headers have been written yet.
func RegisterSimpleAuthMiddleware ¶ added in v1.1.0
func RegisterSimpleAuthMiddleware(router MiddlewareRouter, authToken string)
RegisterSimpleAuthMiddleware registers a middleware for simple token-based authentication. It checks the "Authorization" header for a token matching the provided authToken. It supports both "Bearer <token>" and "<token>" formats. If the authToken is empty, no middleware is registered. If the header is missing or the token is invalid, it responds with 401 Unauthorized.
func Start ¶
func Start(cfg BaseConfig, handlerSetter func(*mux.Router), opts ...Option) (shutdown func(context.Context) error, err error)
Start starts the server with the provided BaseConfig and [Option]s. It returns an error if there was an error starting either of the servers. You should provide a function that sets the handlers for the server to the router. It returns shutdown function so you should shutdown the server manually.
func StartWithShutdown ¶
func StartWithShutdown(ctx context.Context, cfg BaseConfig, handlerSetter func(*mux.Router), opts ...Option) error
Start starts the server with the provided BaseConfig and [Option]s. It returns an error if there was an error starting either of the servers. You should provide a function that sets the handlers for the server to the router. It shutdowns the server when the context is closed (it starts a goroutine to check [Context.Done]).
Types ¶
type AuthConfig ¶ added in v1.1.0
type AuthConfig struct { // Database is the interface for user data persistence. Database AuthDatabase // JWTAccessSecret is the secret key used for signing access tokens (hex encoded). // If empty, a random key will be generated. JWTAccessSecret string // JWTRefreshSecret is the secret key used for signing refresh tokens (hex encoded). // If empty, a random key will be generated. JWTRefreshSecret string // AccessTokenDuration specifies the validity duration for access tokens. // Defaults to 5 minutes if not set. AccessTokenDuration time.Duration // RefreshTokenDuration specifies the validity duration for refresh tokens. // Defaults to 7 days if not set. RefreshTokenDuration time.Duration // IssuerNameInJWT is the issuer name included in the JWT claims. // Defaults to "testing" if not set. IssuerNameInJWT string // RefreshTokenCookieName is the name of the cookie used to store the refresh token. // Defaults to "_servexrt" if not set. RefreshTokenCookieName string // AuthBasePath is the base path for the authentication API endpoints. // Defaults to "/api/v1/auth" if not set. AuthBasePath string // RolesOnRegister are the roles assigned to a newly registered user. RolesOnRegister []UserRole // InitialUsers is a list of initial users to be created. InitialUsers []InitialUser // NotRegisterRoutes, if true, prevents the automatic registration of default auth routes. NotRegisterRoutes bool // contains filtered or unexported fields }
AuthConfig holds the configuration specific to authentication.
type AuthDatabase ¶ added in v1.1.0
type AuthDatabase interface { // NewUser creates a new user in the database. NewUser(ctx context.Context, username string, passwordHash string, roles ...UserRole) (string, error) // FindByID finds a user by their ID. FindByID(ctx context.Context, id string) (user User, exists bool, err error) // FindByUsername finds a user by their username. FindByUsername(ctx context.Context, username string) (user User, exists bool, err error) // FindAll retrieves all users from the database. FindAll(ctx context.Context) ([]User, error) // UpdateUser updates a user's information in the database. // Fields are updated only if the corresponding pointers are not nil. UpdateUser(ctx context.Context, id string, diff *UserDiff) error }
AuthDatabase defines the interface for interacting with the user database.
type AuthManager ¶ added in v1.1.0
type AuthManager struct {
// contains filtered or unexported fields
}
AuthManager handles authentication and authorization logic.
func NewAuthManager ¶ added in v1.1.0
func NewAuthManager(cfg AuthConfig) *AuthManager
NewAuthManager creates a new AuthManager with the provided configuration. It initializes default values for configuration fields if they are not provided and generates random secrets if JWT secrets are empty.
func (*AuthManager) CreateUser ¶ added in v1.1.0
func (h *AuthManager) CreateUser(ctx context.Context, username, password string, roles ...UserRole) error
CreateUser provides a programmatic way to create or update a user. If the user already exists (based on username), it updates their password and roles. If the user does not exist, it creates a new user with the provided details.
func (*AuthManager) GetAllUsersHandler ¶ added in v1.1.0
func (h *AuthManager) GetAllUsersHandler(w http.ResponseWriter, r *http.Request)
GetAllUsersHandler handles the HTTP request to retrieve all users. Note: This handler is intended for administrative purposes and might require specific roles. The corresponding route registration is commented out by default.
func (*AuthManager) GetCurrentUserHandler ¶ added in v1.1.0
func (h *AuthManager) GetCurrentUserHandler(w http.ResponseWriter, r *http.Request)
GetCurrentUserHandler handles the HTTP request to retrieve the details of the currently authenticated user.
func (*AuthManager) LoginHandler ¶ added in v1.1.0
func (h *AuthManager) LoginHandler(w http.ResponseWriter, r *http.Request)
LoginHandler handles the HTTP request for user login.
func (*AuthManager) LogoutHandler ¶ added in v1.1.0
func (h *AuthManager) LogoutHandler(w http.ResponseWriter, r *http.Request)
LogoutHandler handles the HTTP request for user logout. It invalidates the refresh token associated with the current session.
func (*AuthManager) RefreshHandler ¶ added in v1.1.0
func (h *AuthManager) RefreshHandler(w http.ResponseWriter, r *http.Request)
RefreshHandler handles the HTTP request for refreshing access tokens using a refresh token cookie.
func (*AuthManager) RegisterHandler ¶ added in v1.1.0
func (h *AuthManager) RegisterHandler(w http.ResponseWriter, r *http.Request)
RegisterHandler handles the HTTP request for user registration.
func (*AuthManager) RegisterRoutes ¶ added in v1.1.0
func (h *AuthManager) RegisterRoutes(r *mux.Router)
RegisterRoutes registers the authentication-related HTTP routes on the provided router.
func (*AuthManager) UpdateUserRoleHandler ¶ added in v1.1.0
func (h *AuthManager) UpdateUserRoleHandler(w http.ResponseWriter, r *http.Request)
UpdateUserRoleHandler handles the HTTP request to update a user's roles. Note: This handler is intended for administrative purposes and might require specific roles. The corresponding route registration is commented out by default.
func (*AuthManager) WithAuth ¶ added in v1.1.0
func (m *AuthManager) WithAuth(next http.HandlerFunc, roles ...UserRole) http.HandlerFunc
WithAuth is an HTTP middleware that enforces authentication and authorization. It checks for a valid JWT access token in the Authorization header. If roles are provided, it verifies that the authenticated user has at least one of the required roles. The user ID and roles are added to the request context.
type BaseConfig ¶
type BaseConfig struct { // HTTP is an address to start HTTP listener on. HTTP string `yaml:"http" json:"http" env:"SERVER_HTTP"` // HTTPS is an address to start HTTPS listener on. HTTPS string `yaml:"https" json:"https" env:"SERVER_HTTPS"` // CertFile is a path to the TLS certificate file in case of HTTPS. CertFile string `yaml:"cert_file" json:"cert_file" env:"SERVER_CERT_FILE"` // KeyFile is a path to the TLS key file in case of HTTPS. KeyFile string `yaml:"key_file" json:"key_file" env:"SERVER_KEY_FILE"` // AuthToken is a token for authorization in Authorization header. AuthToken string `yaml:"auth_token" json:"auth_token" env:"SERVER_AUTH_TOKEN"` }
BaseConfig represents the base configuration for a server without additional options. You can use it as a base for your own configuration.
func (*BaseConfig) Validate ¶
func (c *BaseConfig) Validate() error
Validate checks if the BaseConfig is valid. It ensures that at least one of HTTP or HTTPS address is provided and that addresses match the required format.
type BaseRequestLogger ¶
type BaseRequestLogger struct {
Logger
}
func (*BaseRequestLogger) Log ¶
func (l *BaseRequestLogger) Log(r RequestLogBundle)
type Context ¶
Context holds data and methods for handling HTTP request.
func C ¶
func C(w http.ResponseWriter, r *http.Request) *Context
C returns a new context for the provided request. It is a shortcut for NewContext.
func NewContext ¶
func NewContext(w http.ResponseWriter, r *http.Request) *Context
NewContext returns a new context for the provided request.
func (*Context) APIVersion ¶
APIVersion returns the API version of the handler from the path. It returns an empty string if not found.
func (*Context) BadGateway ¶
BadGateway handles an error by returning an HTTP error response with status code 502. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) BadRequest ¶
BadRequest handles an error by returning an HTTP error response with status code 400. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) Conflict ¶
Conflict handles an error by returning an HTTP error response with status code 409. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) Error ¶
Error handles an error by returning an HTTP error response. You should use this method during error handling in HTTP handlers. Method sets Content-Type and Content-Length headers. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method.
func (*Context) Forbidden ¶
Forbidden handles an error by returning an HTTP error response with status code 403. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) Header ¶
Header returns the value of the request header with the given name. If multiple values are present, they are joined with a comma and space ", ".
func (*Context) InternalServerError ¶
InternalServerError handles an error by returning an HTTP error response with status code 500. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) LogFields ¶
LogFields returns a slice of fields to set to logger using With method. You can add fieldsToInclude to set exact fields that you need. By default it returns all fields.
func (*Context) NoLog ¶
func (ctx *Context) NoLog()
NoLog marks to not log the request after returning from the handler.
func (*Context) NotAcceptable ¶
NotAcceptable handles an error by returning an HTTP error response with status code 406. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) NotFound ¶
NotFound handles an error by returning an HTTP error response with status code 404. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) NotImplemented ¶
NotImplemented handles an error by returning an HTTP error response with status code 501. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) ParseUnixFromQuery ¶
ParseUnixFromQuery parses unix timestamp from query params to time.Time.
func (*Context) Path ¶
Path returns the value of the path parameter for the given key. Path parameters are the variables from the URL like "/{key}".
func (*Context) Query ¶
Query returns the value of the query parameter for the given key. Query is a parameter from the URL, e.g. "abc/?key=value".
func (*Context) ReadAndValidate ¶
ReadAndValidate reads a JSON from the request body and variable and validate it. You should provide a pointer to the variable.
func (*Context) ReadFile ¶ added in v1.2.0
ReadFile reads a file from the request body. fileKey is the key of the file in the request. It returns the file bytes, the file header and an error.
func (*Context) ReadJSON ¶
ReadJSON reads a JSON from the request body. You should provide a pointer to the variable.
func (*Context) Response ¶
Response writes provided status code and body to the http.ResponseWriter. Body may be []byte, string or an object, that can be marshalled to JSON. It will write nothing in case of body==nil sending response headers with status code only. Method sets Content-Type and Content-Length headers. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method.
func (*Context) ResponseFile ¶ added in v1.1.2
func (*Context) ServiceUnavailable ¶
ServiceUnavailable handles an error by returning an HTTP error response with status code 503. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) SetContentType ¶
SetContentType sets the Content-Type header.
func (*Context) SetCookie ¶
SetCookie sets the cookie with the given name, value, maxAge, secure and httpOnly. maxAge is the time in seconds until the cookie expires. If maxAge < 0, the cookie is deleted. secure specifies if the cookie should only be transmitted over HTTPS. httpOnly prevents the cookie from being accessed through JavaScript, enhancing security against XSS attacks.
func (*Context) SetHeader ¶
SetHeader sets the value of the response header with the given name. If multiple values are provided, they are added to the header.
func (*Context) SetRawCookie ¶
SetRawCookie sets the cookie with the given http.Cookie.
func (*Context) SetSendErrorToClient ¶ added in v1.3.0
SetSendErrorToClient add golang error to response body in case of error.
func (*Context) TooManyRequests ¶
TooManyRequests handles an error by returning an HTTP error response with status code 429. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) Unauthorized ¶
Unauthorized handles an error by returning an HTTP error response with status code 401. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
func (*Context) UnprocessableEntity ¶
StatusUnprocessableEntity handles an error by returning an HTTP error response with status code 422. You should not modify the http.ResponseWriter after calling this method. You will probably want to return from your handler after calling this method. It is a shortcut for Context.Error.
type ErrorLogger ¶ added in v1.1.0
type ErrorResponse ¶
type ErrorResponse struct {
Message string `json:"message"`
}
ErrorResponse represents a JSON for an error response.
type InitialUser ¶ added in v1.1.0
type InitialUser struct { // Username is the username of the user. Username string // Password is the password of the user. Password string // Roles are the roles assigned to the user. Roles []UserRole }
InitialUser represents a user to be created during server startup.
type Logger ¶
type Logger interface { // Debug is using to log successful requests. Debug(msg string, fields ...any) // Info is using to log 'http(s) server started' Info(msg string, fields ...any) // Error is using to log request errors, panics, serve errors and shutodwn in StartContext errors Error(msg string, fields ...any) }
Logger is an interface for logger to log messages.
type MemoryAuthDatabase ¶ added in v1.1.0
type MemoryAuthDatabase struct {
// contains filtered or unexported fields
}
MockAuthDatabase provides a mock implementation of the AuthDatabase interface for testing.
func NewMemoryAuthDatabase ¶ added in v1.1.0
func NewMemoryAuthDatabase() *MemoryAuthDatabase
func (*MemoryAuthDatabase) FindAll ¶ added in v1.1.0
func (db *MemoryAuthDatabase) FindAll(ctx context.Context) ([]User, error)
func (*MemoryAuthDatabase) FindByUsername ¶ added in v1.1.0
func (*MemoryAuthDatabase) UpdateUser ¶ added in v1.1.0
type Metrics ¶
type Metrics interface { // HandleRequest is called on each request to collect metrics. HandleRequest(r *http.Request) }
Metrics is an interface for collecting metrics on each request. [Metrics.HandleRequest] is called on each request.
type MiddlewareRouter ¶ added in v1.1.0
type MiddlewareRouter interface { // Use adds one or more middleware functions to the router. Use(middleware ...mux.MiddlewareFunc) }
MiddlewareRouter is an interface representing a router that supports adding middleware. This is typically implemented by router packages like gorilla/mux.
type Option ¶
type Option func(*Options)
func WithAuth ¶ added in v1.1.0
func WithAuth(db AuthDatabase) Option
WithAuth sets the [Options.Auth.Database] of the Options to the given AuthDatabase and enables auth.
func WithAuthBasePath ¶ added in v1.1.0
WithAuthBasePath sets the [Options.Auth.AuthBasePath] of the Options to the given base path.
func WithAuthConfig ¶ added in v1.1.0
func WithAuthConfig(auth AuthConfig) Option
WithAuthConfig sets the [Options.Auth] of the Options to the given AuthConfig. It panics if the provided AuthConfig.Database is nil.
func WithAuthInitialRoles ¶ added in v1.1.0
WithAuthInitialRoles sets the [Options.Auth.InitialRoles] of the Options to the given roles.
func WithAuthInitialUsers ¶ added in v1.1.0
func WithAuthInitialUsers(users ...InitialUser) Option
WithAuthInitialUsers sets the [Options.Auth.InitialUsers] of the Options to the given users.
func WithAuthIssuer ¶ added in v1.1.0
WithAuthIssuer sets the [Options.Auth.IssuerNameInJWT] of the Options to the given issuer name.
func WithAuthKey ¶ added in v1.1.0
WithAuthKey sets the [Options.Auth.JWTAccessSecret] and [Options.Auth.JWTRefreshSecret] of the Options to the given keys.
func WithAuthMemoryDatabase ¶ added in v1.1.0
func WithAuthMemoryDatabase() Option
WithAuthMemoryDatabase sets the [Options.Auth.Database] of the Options to the in-memory AuthDatabase and enables auth. NOT RECOMMENDED FOR PRODUCTION USE. It will forget all users on applications shutdown.
func WithAuthNotRegisterRoutes ¶ added in v1.1.0
WithAuthNotRegisterRoutes sets the [Options.Auth.NotRegisterRoutes] of the Options to the given value.
func WithAuthRefreshTokenCookieName ¶ added in v1.1.0
WithAuthRefreshTokenCookieName sets the [Options.Auth.RefreshTokenCookieName] of the Options to the given name.
func WithAuthToken ¶
WithAuthToken sets the [Options.AuthToken] of the Options to the given string. AuthToken is the token that will be checked in the Authorization header.
func WithAuthTokensDuration ¶ added in v1.1.0
WithAuthTokensDuration sets the [Options.Auth.AccessTokenDuration] and [Options.Auth.RefreshTokenDuration] of the Options to the given duration.
func WithBurstSize ¶ added in v1.1.0
WithBurstSize sets the [Options.RateLimit.BurstSize] of the Options to the given value.
func WithCertificate ¶
func WithCertificate(cert tls.Certificate) Option
WithCertificate sets the TLS [Options.Certificate] to the Options. TLS certificate is required to start HTTPS server.
func WithIdleTimeout ¶
WithIdleTimeout sets the [Options.IdleTimeout] of the Options to the given duration. IdleTimeout is the maximum duration an idle Keep-Alive connection will be kept open. A zero or negative value sets default value of 180 seconds.
func WithLogger ¶
WithLogger sets the Logger to the Options. If not set, there will be slog.New with slog.NewJSONHandler with logging to stderr.
func WithNoLogClientErrors ¶ added in v1.1.0
func WithNoLogClientErrors() Option
WithNoLogClientErrors disables logging of client errors with code 400-499.
func WithNoRateInAuthRoutes ¶ added in v1.1.0
func WithNoRateInAuthRoutes() Option
WithNoRateInAuthRoutes sets the [Options.RateLimit.NoRateInAuthRoutes] of the Options to true. If true, will not set rate limit for requests to auth routes automatically.
func WithNoRequestLog ¶ added in v1.1.0
func WithNoRequestLog() Option
WithNoRequestLog disables request logging.
func WithRPM ¶ added in v1.1.0
WithRPM sets the [Options.RateLimit.RequestsPerInterval] of the Options to the given value. Interval is set to 1 minute.
func WithRPS ¶ added in v1.1.0
WithRPS sets the [Options.RateLimit.RequestsPerInterval] of the Options to the given value. Interval is set to 1 second.
func WithRateLimitConfig ¶ added in v1.1.0
func WithRateLimitConfig(rateLimit RateLimitConfig) Option
WithRateLimitConfig sets the [Options.RateLimit] of the Options to the given RateLimitConfig.
func WithRateLimitExcludePaths ¶ added in v1.1.0
WithRateLimitExcludePaths sets the [Options.RateLimit.ExcludePaths] of the Options to the given paths.
func WithRateLimitIncludePaths ¶ added in v1.1.0
WithRateLimitIncludePaths sets the [Options.RateLimit.IncludePaths] of the Options to the given paths. If empty, all paths are included except those in ExcludePaths if rate limiting is enabled.
func WithRateLimitKeyFunc ¶ added in v1.1.0
WithRateLimitKeyFunc sets the [Options.RateLimit.KeyFunc] of the Options to the given function.
func WithRateLimitMessage ¶ added in v1.1.0
WithRateLimitMessage sets the [Options.RateLimit.Message] of the Options to the given value.
func WithRateLimitStatusCode ¶ added in v1.1.0
WithRateLimitStatusCode sets the [Options.RateLimit.StatusCode] of the Options to the given value.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout sets the [Options.ReadHeaderTimeout] of the Options to the given duration. ReadHeaderTimeout is the maximum duration for reading the request headers (without body). A zero or negative value means there will be no timeout (using ReadTimeout for all request).
func WithReadTimeout ¶
WithReadTimeout sets the [Options.ReadTimeout] of the Options to the given duration. ReadTimeout is the maximum duration for reading the entire request, including the body. A zero or negative value sets default value of 60 seconds.
func WithRequestLogger ¶
func WithRequestLogger(r RequestLogger) Option
WithRequestLogger sets the RequestLogger to the Options. If not set it will use [Options.Logger] or [Options.SLogger] in debug level.
func WithRequestsPerInterval ¶ added in v1.1.0
WithRequestsPerInterval sets the [Options.RateLimit.RequestsPerInterval] of the Options to the given value.
func WithSendErrorToClient ¶ added in v1.3.0
WithSendErrorToClient sets the [Options.SendErrorToClient] of the Options to the given value.
type Options ¶
type Options struct { // Certificate is the TLS certificate for the server. // If not set, the server will not start HTTPS server. Certificate *tls.Certificate // ReadTimeout is the maximum duration for reading the entire request. ReadTimeout time.Duration // ReadHeaderTimeout is the maximum duration for reading the request headers. ReadHeaderTimeout time.Duration // IdleTimeout is the maximum duration the server should keep connections alive. IdleTimeout time.Duration // AuthToken is the token that will be checked in the Authorization header. AuthToken string // Metrics is the metrics handler for the server. Metrics Metrics // Logger is the logger for the server. // If not set, there will be [slog.New] with [slog.NewJSONHandler] with logging to stderr. // Debug level is used to log successful requests if RequestLogger is not set. // Info level is used to log 'http(s) server started' // Error level is used to log request errors, panics, serve errors and shutodwn in StartContext errors Logger Logger // RequestLogger is the logger for the requests. // If not set it will use [Options.Logger]. RequestLogger RequestLogger // NoLogClientErrors, if true, will not log client errors with code 400-499 NoLogClientErrors bool // SendErrorToClient, if true, will send golang error to client in response body. SendErrorToClient bool // Auth is the auth configuration for the server. Auth AuthConfig // RateLimit is the rate limit configuration for the server. // If not set, rate limiting will be disabled. // If RateLimit.RequestsPerInterval is not set, it will be disabled. // If RateLimit.Interval is not set, it will be disabled. RateLimit RateLimitConfig }
Options represents the configuration for a server.
type RateLimitConfig ¶ added in v1.1.0
type RateLimitConfig struct { // RequestsPerInterval is the number of operations allowed per interval. // If not set, rate limiting will be disabled. RequestsPerInterval int // Interval is the time after which the token bucket is refilled. // If not set, it will be 1 minute. Interval time.Duration // BurstSize is the maximum burst size (can exceed RequestsPerInterval temporarily). // If not set, it will be equal to RequestsPerInterval. BurstSize int // StatusCode is the HTTP status code returned when rate limit is exceeded. // Defaults to 429 (Too Many Requests) if not set. StatusCode int // Message is the response message when rate limit is exceeded. // Defaults to "rate limit exceeded, try again later." if not set. Message string // KeyFunc is a function that extracts the rate limit key from the request. // Defaults to usernameKeyFunc() if not set. KeyFunc func(r *http.Request) string // ExcludePaths are paths that should be excluded from rate limiting. ExcludePaths []string // IncludePaths are paths that should be included in rate limiting. // If empty, all paths are included except those in ExcludePaths if rate limiting is enabled. IncludePaths []string // NoRateInAuthRoutes, if true, will not rate limit requests to auth routes. NoRateInAuthRoutes bool }
RateLimitConfig holds configuration for the rate limiter middleware.
type RequestLogBundle ¶
type RequestLogBundle struct { Request *http.Request RequestID string Error error ErrorMessage string StatusCode int StartTime time.Time NoLogClientErrors bool }
RequestLogBundle represents a bundle of information for logging a request.
type RequestLogger ¶
type RequestLogger interface {
Log(RequestLogBundle)
}
RequestLogger is an interface for logging requests. [RequestLogger.Log] is called at the end of each request after returning from handler.
type RoleContextKey ¶ added in v1.1.0
type RoleContextKey struct{}
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents an HTTP server.
func New ¶
New creates a new instance of the Server. You can provide a list of options using With* methods. Server without Certificate can serve only plain HTTP.
func NewWithOptions ¶
NewWithOptions creates a new instance of the Server with the provided Options.
func (*Server) AddMiddleware ¶
AddMiddleware adds one or more mux.MiddlewareFunc to the router.
func (*Server) AuthManager ¶ added in v1.1.0
func (s *Server) AuthManager() *AuthManager
AuthManager returns AuthManager, it may be useful if you want to work with auth manually. It returns nil if auth is not enabled (database is not set).
func (*Server) H ¶ added in v1.1.0
H is a shortcut for Server.Handle.
func (*Server) HA ¶ added in v1.1.0
HA is a shortcut for Server.HandleWithAuth.
func (*Server) HF ¶ added in v1.1.0
HF is a shortcut for Server.HandleFunc.
func (*Server) HFA ¶ added in v1.1.0
HFA is a shortcut for Server.HandleFuncWithAuth.
func (*Server) HTTPAddress ¶
HTTPAddress returns the address the HTTP server is listening on. Returns an empty string if the HTTP server is not running or not configured.
func (*Server) HTTPSAddress ¶
HTTPSAddress returns the address the HTTPS server is listening on. Returns an empty string if the HTTPS server is not running or not configured.
func (*Server) Handle ¶
Handle registers a new route with the provided path, http.Handler and methods. It returns a pointer to the created mux.Route to set additional settings to the route.
func (*Server) HandleFunc ¶
HandleFunc registers a new route with the provided path, http.HandlerFunc and methods. It returns a pointer to the created mux.Route to set additional settings to the route.
func (*Server) HandleFuncWithAuth ¶ added in v1.1.0
HandleFuncWithAuth registers a new route with the provided path, http.HandlerFunc and methods. It adds auth middleware to the route with the provided roles. It returns a pointer to the created mux.Route to set additional settings to the route.
func (*Server) HandleWithAuth ¶ added in v1.1.0
HandleWithAuth registers a new route with the provided path, http.Handler and methods. It adds auth middleware to the route with the provided roles. It returns a pointer to the created mux.Route to set additional settings to the route.
func (*Server) IsAuthEnabled ¶ added in v1.1.0
IsAuthEnabled returns true if auth is enabled.
func (*Server) R ¶ added in v1.1.0
R is a shortcut for Server.Router.
func (*Server) Router ¶
Router returns mux.Router, it may be useful if you want to work with router manually. It accepts a path to set as a base path for the router.
func (*Server) Start ¶
Start starts the server. It takes two parameters: httpAddr and httpsAddr - addresses to listen for HTTP and HTTPS. It returns an error if there was an error starting either of the servers.
func (*Server) StartHTTP ¶
StartHTTP starts an HTTP server on the provided address. It returns an error if the server cannot be started or address is invalid.
func (*Server) StartHTTPS ¶
StartHTTPS starts an HTTPS server on the provided address. It returns an error if the server cannot be started, address is invalid or no certificate is provided in config.
func (*Server) StartWithShutdown ¶
StartWithShutdown starts HTTP and HTTPS servers and shutdowns its when the context is closed.
func (*Server) StartWithShutdownHTTP ¶
StartWithShutdownHTTP starts the HTTP server and shutdowns it when the context is closed.
func (*Server) StartWithShutdownHTTPS ¶
StartWithShutdownHTTPS starts the HTTPS server and shutdowns it when the context is closed.
func (*Server) WithAuth ¶ added in v1.1.0
func (s *Server) WithAuth(next http.HandlerFunc, roles ...UserRole) http.HandlerFunc
WithAuth adds auth middleware to the router with the provided roles. It returns a pointer to the created mux.Route to set additional settings to the route.
func (*Server) WithBasePath ¶ added in v1.1.0
WithBasePath sets the base path for the server's router. It returns the server itself to allow method chaining.
type User ¶ added in v1.1.0
type User struct { ID string `json:"id" bson:"_id" db:"id"` Username string `json:"username" bson:"username" db:"username"` Roles []UserRole `json:"roles" bson:"roles" db:"roles"` PasswordHash string `json:"password_hash" bson:"password_hash" db:"password_hash"` RefreshTokenHash string `json:"refresh_token_hash" bson:"refresh_token_hash" db:"refresh_token_hash"` RefreshTokenExpiresAt time.Time `json:"refresh_token_expires_at" bson:"refresh_token_expires_at" db:"refresh_token_expires_at"` }
User represents a user entity in the system.
type UserContextKey ¶ added in v1.1.0
type UserContextKey struct{}
type UserDiff ¶ added in v1.1.0
type UserDiff struct { Username *string `json:"username,omitempty" bson:"username,omitempty" db:"username,omitempty"` Roles *[]UserRole `json:"roles,omitempty" bson:"roles,omitempty" db:"roles,omitempty"` PasswordHash *string `json:"password_hash,omitempty" bson:"password_hash,omitempty" db:"password_hash,omitempty"` RefreshTokenHash *string `json:"refresh_token_hash,omitempty" bson:"refresh_token_hash,omitempty" db:"refresh_token_hash,omitempty"` RefreshTokenExpiresAt *time.Time `json:"refresh_token_expires_at,omitempty" bson:"refresh_token_expires_at,omitempty" db:"refresh_token_expires_at,omitempty"` }
type UserLoginRequest ¶ added in v1.1.0
type UserLoginRequest struct { Username string `json:"username"` Password string `json:"password"` }
UserLoginRequest represents the request body for user login and registration.
func (UserLoginRequest) Validate ¶ added in v1.1.0
func (req UserLoginRequest) Validate() error
Validate checks if the UserLoginRequest is valid.
type UserLoginResponse ¶ added in v1.1.0
type UserLoginResponse struct { ID string `json:"id"` Username string `json:"username"` Roles []UserRole `json:"roles"` AccessToken string `json:"accessToken,omitempty"` }
UserLoginResponse represents the response body for successful user login or registration.
type UserRole ¶ added in v1.1.0
type UserRole string
UserRole represents a role assigned to a user. It's defined as a string type for easy JSON marshaling/unmarshaling.
type UserUpdateRequest ¶ added in v1.1.0
type UserUpdateRequest struct { ID string `json:"id"` Username *string `json:"username,omitempty"` Roles *[]UserRole `json:"roles,omitempty"` Password *string `json:"password,omitempty"` }
UserUpdateRequest represents the request body for updating user information.
func (UserUpdateRequest) Validate ¶ added in v1.1.0
func (req UserUpdateRequest) Validate() error
Validate checks if the UserUpdateRequest is valid.