Documentation ¶
Index ¶
- Constants
- Variables
- func NormalizePort(val string) string
- type Context
- func (c *Context) DecodeJSONBody(v interface{}) error
- func (c *Context) GetCookie(name string) (*http.Cookie, error)
- func (c *Context) GetStatusCode() int
- func (c *Context) JSONResponse(data interface{})
- func (c *Context) Logger() *slog.Logger
- func (c *Context) Origin() string
- func (c *Context) Param(key string) string
- func (c *Context) Path() string
- func (c *Context) QueryParam(key string) string
- func (c *Context) RawResponse(raw []byte)
- func (c *Context) Redirect(url string)
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetHeader(key string, value string)
- func (c *Context) Status(status int) *Context
- func (c *Context) StringResponse(data string)
- func (c *Context) TemplateResponse(template *Tpl)
- type HandlerFunc
- type Map
- type Middleware
- type Params
- type RouteGroup
- func (rg *RouteGroup) Delete(path string, handler HandlerFunc)
- func (rg *RouteGroup) Get(path string, handler HandlerFunc)
- func (rg *RouteGroup) Handle(method string, path string, handler HandlerFunc)
- func (rg *RouteGroup) Patch(path string, handler HandlerFunc)
- func (rg *RouteGroup) Post(path string, handler HandlerFunc)
- func (rg *RouteGroup) Put(path string, handler HandlerFunc)
- func (rg *RouteGroup) RouteGroup(path string) *RouteGroup
- func (rg *RouteGroup) Use(middleware Middleware)
- type Router
- type Server
- func (s *Server) Delete(path string, handler HandlerFunc)
- func (s *Server) Get(path string, handler HandlerFunc)
- func (s *Server) Handle(method string, path string, handler HandlerFunc)
- func (s *Server) Patch(path string, handler HandlerFunc)
- func (s *Server) Post(path string, handler HandlerFunc)
- func (s *Server) Put(path string, handler HandlerFunc)
- func (s *Server) RouteGroup(path string) *RouteGroup
- func (s *Server) Shutdown() error
- func (s *Server) Start()
- func (s *Server) Test(method string, route string, body io.Reader, testParams ...TestParams) (httptest.ResponseRecorder, error)
- func (s *Server) Use(mw Middleware)
- type ServerConfig
- type TestParams
- type Tpl
Constants ¶
const ( DEFAULT_PORT = "8080" DEFAULT_STATIC_PATH = "/static" DEFAULT_STATIC_DIR = "public/assets" )
Variables ¶
var ( ErrInvalidJSON = errors.New("invalid_json") ErrInternalServer = errors.New("internal_server_error") ErrBodyTooLarge = errors.New("request_body_too_large") )
var DEFAULT_TEMPLATE_VARIABLES = map[string]interface{}{ "Static": DEFAULT_STATIC_PATH, }
Functions ¶
func NormalizePort ¶
Types ¶
type Context ¶
type Context struct { // server Request *http.Request Writer http.ResponseWriter // contains filtered or unexported fields }
func (*Context) DecodeJSONBody ¶
func (*Context) GetStatusCode ¶ added in v0.5.3
Get the status code set for the response
func (*Context) JSONResponse ¶
func (c *Context) JSONResponse(data interface{})
JSONResponse marshals the provided interface into JSON and writes it to the response writer If there is an error in marshalling the JSON, an internal server error is returned
func (*Context) Param ¶ added in v0.6.0
Param gets the params within the path mentioned as a wildcard
func (*Context) QueryParam ¶ added in v0.6.0
QueryParam gets the query parameters passed eg: /?name=value
func (*Context) RawResponse ¶
sets response body as byte array
func (*Context) StringResponse ¶ added in v0.5.5
StringResponse writes the provided string to the response writer
func (*Context) TemplateResponse ¶ added in v1.0.1
TemplateResponse renders the provided template with the provided data and writes it to the response writer with content type text/html
type HandlerFunc ¶
type HandlerFunc func(*Context)
type Middleware ¶
type Middleware func(HandlerFunc) HandlerFunc
This implementation of middleware will enable middleware chaining
type RouteGroup ¶ added in v0.5.5
type RouteGroup struct {
// contains filtered or unexported fields
}
func (*RouteGroup) Delete ¶ added in v0.5.5
func (rg *RouteGroup) Delete(path string, handler HandlerFunc)
func (*RouteGroup) Get ¶ added in v0.5.5
func (rg *RouteGroup) Get(path string, handler HandlerFunc)
func (*RouteGroup) Handle ¶ added in v0.5.5
func (rg *RouteGroup) Handle(method string, path string, handler HandlerFunc)
func (*RouteGroup) Patch ¶ added in v0.5.5
func (rg *RouteGroup) Patch(path string, handler HandlerFunc)
func (*RouteGroup) Post ¶ added in v0.5.5
func (rg *RouteGroup) Post(path string, handler HandlerFunc)
func (*RouteGroup) Put ¶ added in v0.5.5
func (rg *RouteGroup) Put(path string, handler HandlerFunc)
func (*RouteGroup) RouteGroup ¶ added in v0.5.5
func (rg *RouteGroup) RouteGroup(path string) *RouteGroup
func (*RouteGroup) Use ¶ added in v0.5.5
func (rg *RouteGroup) Use(middleware Middleware)
type Router ¶ added in v0.5.6
type Router interface { ServeFiles(string, http.FileSystem) ServeHTTP(http.ResponseWriter, *http.Request) HandlerFunc(method string, path string, handler http.HandlerFunc) ParamsFromContext(context.Context) Params Router() *httprouter.Router }
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func New ¶ added in v0.5.3
func New(userconfig ...*ServerConfig) *Server
New creates a new server instance Configurations can be passed as a parameter and It's optional If no configurations are passed, default values are used Returning server struct pointer because its more performant interface{} is slower than concrete types, because it slows down garbage collector
func (*Server) Delete ¶
func (s *Server) Delete(path string, handler HandlerFunc)
func (*Server) Get ¶
func (s *Server) Get(path string, handler HandlerFunc)
Register handlers for the HTTP methods usage example: server.Get("/test", func(c stk.Context) { gc.Status(http.StatusOK).JSONResponse("OK") })
func (*Server) Handle ¶ added in v0.5.4
func (s *Server) Handle(method string, path string, handler HandlerFunc)
func (*Server) Patch ¶
func (s *Server) Patch(path string, handler HandlerFunc)
func (*Server) Post ¶
func (s *Server) Post(path string, handler HandlerFunc)
func (*Server) Put ¶
func (s *Server) Put(path string, handler HandlerFunc)
func (*Server) RouteGroup ¶ added in v0.5.5
func (s *Server) RouteGroup(path string) *RouteGroup
RouteGroup returns a new RouteGroup instance RouteGroup is used to register routes with the same path prefix It will also ensure that the middlewares are applied to the routes exclusively usage example: rg := server.RouteGroup("/api") rg.Get("/users", func(c stk.Context) { gc.Status(http.StatusOK).JSONResponse("OK") })
func (*Server) Shutdown ¶ added in v0.5.0
Shuts down the server, use for graceful shutdown Eg Usage:
// indicate that the server is shutting down done := make(chan bool)
// A go routine that listens for os signals // it will block until it receives a signal // once it receives a signal, it will shutdown close the done channel
go func() { sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt, syscall.SIGTERM) <-sigint if err := server.Shutdown(); err != nil { logger.Error(err) } close(done) }()
return server, done
func (*Server) Test ¶ added in v0.5.4
func (s *Server) Test(method string, route string, body io.Reader, testParams ...TestParams) (httptest.ResponseRecorder, error)
Helper function to test the server Usage example: w, err := server.Test("GET", "/test", nil)
func (*Server) Use ¶
func (s *Server) Use(mw Middleware)
Use adds a middleware to the server usage example: server.Use(stk.RequestLogger()) NOTE: Middlewares will be applied when the route is registered SO Make sure to register the routes after adding the middlewares