Documentation
¶
Index ¶
- Variables
- func AssetsDir() string
- func AssetsHandler(path string) func(res *Response, req *Request)
- func CacheKeyHash(key string) string
- func HandleAssets(router Router) error
- func HandleAssetsOnPath(router Router, path string) error
- func Register(resolver interface{}, singleton bool) error
- func RegisterDB(name string, factory func() *bun.DB) error
- func RegisterNamed(name string, resolver interface{}, singleton bool) error
- func RequestLogger(res *Response, req *Request, next func() error) (err error)
- func Resolve(obj interface{}) error
- func ResolveNamed(name string, obj interface{}) error
- func TemplateDir() string
- func ViewHandler(view string) func(res *Response, req *Request) error
- type Cache
- type Handler
- func (h *Handler) AddMiddleware(middleware interface{}) error
- func (h Handler) CanError() bool
- func (h Handler) HandlerFunc() HandlerFunc
- func (h Handler) HasContext() bool
- func (h Handler) HasRequest() bool
- func (h Handler) HasResponse() bool
- func (h Handler) Serve(req *Request, res *Response) error
- type HandlerFunc
- type LogFields
- type Logger
- type MiddlewareHandler
- type Renderer
- type Request
- type Response
- type RouteGroup
- func (r *RouteGroup) DELETE(path string, handler interface{}) error
- func (r *RouteGroup) GET(path string, handler interface{}) error
- func (r *RouteGroup) HEAD(path string, handler interface{}) error
- func (r *RouteGroup) POST(path string, handler interface{}) error
- func (r *RouteGroup) PUT(path string, handler interface{}) error
- func (r *RouteGroup) WithMiddleware(middleware interface{}) error
- type Routeable
- type Router
- type ViewBag
Constants ¶
This section is empty.
Variables ¶
var ( // ResourcesDir is the base path of where all static resources like templates and assets are stored // This is dynamically set to the current working directory on startup ResourcesDir = "" // TemplatePrefix is a prefix that is appended to the ResourcesDir to get the base path for all templates // This is used by the default renderer implementation to figure out where to look for partials. TemplatePrefix = "/templates/" // AssetsPrefix is a prefix that is appended to the ResourcesDir to get the base path for all assets // This is used by the provided AssetsHandler implementation to serve files from AssetsPrefix = "/assets/" )
var ErrorCacheMiss = errors.New("the given key was not found in the cache")
ErrorCacheMiss is returned when the cache could not find the given key
var ErrorNextHandlerInvalid = errors.New("middleware next parameter type is incorrect. Must be a func() error")
ErrorNextHandlerInvalid is returned when a middleware has an incorrect signature for the next() function
var ( // ErrorTooManyArguments is returned when a handler has too many arguments ErrorTooManyArguments = errors.New("the specified handler has too many argument") )
Functions ¶
func AssetsDir ¶
func AssetsDir() string
AssetsDir will return the absolute path of the configured assets directory
func AssetsHandler ¶
AssetsHandler will return a handler that can serve assets on a given path. AssetsHandler will look for the assets as ResourcesDir + AssetsPrefix on the file system. The path provided must be the same path as registered on the router.
func CacheKeyHash ¶
CacheKeyHash returns the SHA256 hash of a string for caching purposes
func HandleAssets ¶
HandleAssets will register the AssetsHandler on a given router on the default path /assets
func HandleAssetsOnPath ¶
HandleAssetsOnPath will register the AssetsHandler on a given router and a given path with the correct route param suffix
func Register ¶
Register will register a new dependency as default for the return type of the function
func RegisterDB ¶
RegisterDB registers a new database factory for a bun.DB connection
func RegisterNamed ¶
RegisterNamed will register a new dependency under the given name
func RequestLogger ¶
RequestLogger provides a simple middleware implementation that will log every request handled by scalar
func Resolve ¶
func Resolve(obj interface{}) error
Resolve will resolve a dependency based on the target objects type
func ResolveNamed ¶
ResolveNamed will resolve a dependecy based on the given name
func TemplateDir ¶
func TemplateDir() string
TemplateDir will return the absolute path of the configured templates directory
Types ¶
type Cache ¶
type Cache interface {
// Contains returns whether a key is present in the cache
Contains(key string) (bool, error)
// Delete removes a cache entry by its key, will do nothing if the
// key was not present in the cache
Delete(key string) error
// ExpireAfter will mark a cache key for expiration after a certain duration
ExpireAfter(key string, duration time.Duration) error
// Get will attempt to read a stored cache value into the given
// out interface pointer or error if not present
Get(key string, out interface{}) error
// Get will attempt to read a stored cache value into the given
// out interface pointer or return default if not found
GetOrDefault(key string, out interface{}, def interface{}) error
// Set will attempt to store a value in the cache with a given key
Set(key string, val interface{}) error
}
Cache defines the minimum API surface for a valid scalar cache
func DefaultCache ¶
func DefaultCache() (cache Cache)
DefaultCache will return the default cache instance for the scalar.Cache type
func NewMemoryCache ¶
func NewMemoryCache() Cache
NewMemoryCache will create a new instance of the built-in go-scalar memory cache
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler defines a scalar handler
func NewHandler ¶
NewHandler will introspect the given handler and, if valid, return a new handler instance that can serve a route
func (*Handler) AddMiddleware ¶
AddMiddleware adds a middleware into the chain
func (Handler) HandlerFunc ¶
func (h Handler) HandlerFunc() HandlerFunc
HandlerFunc will generate the original handlers function without any chained middlewares
func (Handler) HasContext ¶
HasContext returns true if the position of a context argument has been detected
func (Handler) HasRequest ¶
HasRequest returns true if the position of a scalar request argument has been detected
func (Handler) HasResponse ¶
HasResponse returns true if the position of a scalar response argument has been detected
type HandlerFunc ¶
HandlerFunc describes the method signature of a scalar handler function that can process an incoming requests. A handler func masks all middleware and dependency injection behind a simple, easy to use interface
func HandlerChain ¶
func HandlerChain(f HandlerFunc, middleware MiddlewareHandler) HandlerFunc
HandlerChain will chain a handler func and a middleware together to form a new, single handler func
type LogFields ¶
type LogFields map[string]interface{}
LogFields is a type definition for cleaner code
type Logger ¶
type Logger interface {
// Debug will write a debug log
Debug(msg interface{})
// Debugf will write a debug log sprintf-style
Debugf(msg string, values ...interface{})
// Error will write a error log
Error(msg interface{})
// Errorf will write a error log sprintf-style
Errorf(msg string, values ...interface{})
// Fatal will write a fatal log
Fatal(msg interface{})
// Fatalf will write a fatal log sprintf-style
Fatalf(msg string, values ...interface{})
// Field will add a field to a new logger and return it
Field(name string, val interface{}) Logger
// Fields will add multiple fields to a new logger and return it
Fields(fields LogFields) Logger
// Info will write a info log
Info(msg interface{})
// Infof will write a info log sprintf-style
Infof(msg string, values ...interface{})
// Trace will write a trace log
Trace(msg interface{})
// Tracef will write a trace log sprintf-style
Tracef(msg string, values ...interface{})
// Warn will write a warn log
Warn(msg interface{})
// Warnf will write a warn log sprintf-style
Warnf(msg string, values ...interface{})
}
Logger defines the interface of a scalar compatible logger implementation
func DefaultLogger ¶
func DefaultLogger() (logger Logger)
DefaultLogger will return the default logger instance for the scalar.Logger type
func NewBuiltinLogger ¶
func NewBuiltinLogger() Logger
NewBuiltinLogger will create a new instance of the scalar builtin logger implementation
func NewZerologLogger ¶
func NewZerologLogger() Logger
NewZerologLogger will create a new instance of the scalar zerolog implementation
type MiddlewareHandler ¶
MiddlewareHandler is a special kind of HandlerFunc, that receives the next handler in line as its third parameter.
type Renderer ¶
type Renderer interface {
// Render will load a template file and render the template
// within using the viewbag as a context
Render(view string, bag ViewBag) (string, error)
}
Renderer defines the interface of a scalar compatible renderer
func DefaultRenderer ¶
func DefaultRenderer() (renderer Renderer)
DefaultRenderer will return the default renderer instance for the scalar.Renderer type
func NewHandlebarsRenderer ¶
func NewHandlebarsRenderer() Renderer
NewHandlebarsRenderer will return a new instance of the scalar handlebars renderer implementation
type Request ¶
type Request struct {
*http.Request
Params map[string]string
// contains filtered or unexported fields
}
Request is the scalar implementation of a request which wraps around a regular http.Request object.
func NewRequest ¶
NewRequest will create a new instance of a scalar request for the given http.Request object
func (Request) HasContentType ¶ added in v1.1.1
HasContentType determines whether a request has a given mime type as its content type
func (Request) ParamOrDefault ¶
ParamOrDefault returns the route parameter or a custom default if not found
func (Request) SetMetadata ¶
SetMetadata will set the metadata value for a given key
type Response ¶
type Response struct {
http.ResponseWriter
ViewBag ViewBag
}
Response is the scalar implementation of a response which wraps around a regular http.ResponseWriter object
func NewResponse ¶
func NewResponse(res http.ResponseWriter) *Response
NewResponse will create a new instance of a scalar response for the given http.ResponseWriter object
func (Response) PrettyJSON ¶
PrettyJSON writes any object to the response body as pretty JSON
type RouteGroup ¶
type RouteGroup struct {
Delete map[string]*Handler
Get map[string]*Handler
Head map[string]*Handler
Post map[string]*Handler
Put map[string]*Handler
Middlewares []interface{}
}
RouteGroup is the default implementation of a generic Routeable
func NewRouteGroup ¶ added in v1.1.1
func NewRouteGroup() *RouteGroup
NewRouteGroup will create a new, empty instance of the default Routeable implementation
func (*RouteGroup) DELETE ¶ added in v1.1.1
func (r *RouteGroup) DELETE(path string, handler interface{}) error
DELETE will register a new route using the DELETE method
func (*RouteGroup) GET ¶ added in v1.1.1
func (r *RouteGroup) GET(path string, handler interface{}) error
GET will register a new route using the GET method
func (*RouteGroup) HEAD ¶ added in v1.1.1
func (r *RouteGroup) HEAD(path string, handler interface{}) error
HEAD will register a new route using the HEAD method
func (*RouteGroup) POST ¶ added in v1.1.1
func (r *RouteGroup) POST(path string, handler interface{}) error
POST will register a new route using the POST method
func (*RouteGroup) PUT ¶ added in v1.1.1
func (r *RouteGroup) PUT(path string, handler interface{}) error
PUT will register a new route using the PUT method
func (*RouteGroup) WithMiddleware ¶ added in v1.1.1
func (r *RouteGroup) WithMiddleware(middleware interface{}) error
WithMiddleware will add a middleware to all registered and future routes in this route group
type Routeable ¶
type Routeable interface {
DELETE(path string, handler interface{}) error
GET(path string, handler interface{}) error
HEAD(path string, handler interface{}) error
POST(path string, handler interface{}) error
PUT(path string, handler interface{}) error
WithMiddleware(handler interface{}) error
}
Routeable defines the functions necessary on any object that can register routes and middlewares
type Router ¶
type Router interface {
Routeable
Group(path string, callback func(router Routeable))
ListenAndServe(address string) error
}
Router defines a superset of a Routeable that can create route groups as well as start a webserver
func DefaultRouter ¶
func DefaultRouter() (router Router)
DefaultRouter will return the default router instance for the scalar.Router type
func NewBunRouter ¶
func NewBunRouter() Router
NewBunRouter will create new instance of the scalar bun router implementation