gsk

package
v1.2.1-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 13 Imported by: 23

Documentation

Index

Constants

View Source
const (
	DEFAULT_PORT        = "8080"
	DEFAULT_STATIC_PATH = "/static"
	DEFAULT_STATIC_DIR  = "public/assets"
)

Variables

View Source
var (
	ErrInvalidJSON    = errors.New("invalid_json")
	ErrInternalServer = errors.New("internal_server_error")
	ErrBodyTooLarge   = errors.New("request_body_too_large")
)
View Source
var DEFAULT_TEMPLATE_VARIABLES = map[string]interface{}{
	"Static": DEFAULT_STATIC_PATH,
}

Functions

func NormalizePort

func NormalizePort(val string) string

Types

type Context

type Context struct {
	// server
	Request *http.Request
	Writer  http.ResponseWriter
	// contains filtered or unexported fields
}

func (*Context) DecodeJSONBody

func (c *Context) DecodeJSONBody(v interface{}) error

func (*Context) GetCookie

func (c *Context) GetCookie(name string) (*http.Cookie, error)

Get cookie using cookie name

func (*Context) GetStatusCode added in v0.5.3

func (c *Context) GetStatusCode() int

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) Logger

func (c *Context) Logger() *slog.Logger

get the logger

func (*Context) Origin added in v0.6.0

func (c *Context) Origin() string

func (*Context) Param added in v0.6.0

func (c *Context) Param(key string) string

Param gets the params within the path mentioned as a wildcard

func (*Context) Path added in v0.6.0

func (c *Context) Path() string

func (*Context) QueryParam added in v0.6.0

func (c *Context) QueryParam(key string) string

QueryParam gets the query parameters passed eg: /?name=value

func (*Context) RawResponse

func (c *Context) RawResponse(raw []byte)

sets response body as byte array

func (*Context) Redirect added in v0.6.1

func (c *Context) Redirect(url string)

Redirect redirects the request to the provided URL

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

Set cookie using http.Cookie

func (*Context) SetHeader

func (c *Context) SetHeader(key string, value string)

sets response header key value

func (*Context) Status

func (c *Context) Status(status int) *Context

Status sets the status code of the response

func (*Context) StringResponse added in v0.5.5

func (c *Context) StringResponse(data string)

StringResponse writes the provided string to the response writer

func (*Context) TemplateResponse added in v1.0.1

func (c *Context) TemplateResponse(template *Tpl)

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 Map

type Map map[string]interface{}

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

This implementation of middleware will enable middleware chaining

type Params added in v0.5.6

type Params interface {
	ByName(string) string
}

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

func (s *Server) Shutdown() error

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) Start

func (s *Server) Start()

Start starts the server on the configured port

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

type ServerConfig

type ServerConfig struct {
	Port   string
	Logger *slog.Logger
	// Input
	BodySizeLimit int64

	// Static
	StaticPath string
	StaticDir  string
}

type TestParams added in v0.5.4

type TestParams struct {
	Cookies []*http.Cookie
	Headers map[string]string
}

type Tpl added in v1.0.1

type Tpl struct {
	TemplatePath string
	Variables    interface{}
}

func (*Tpl) Render added in v1.0.1

func (t *Tpl) Render(configVars map[string]interface{}) ([]byte, error)

Jump to

Keyboard shortcuts

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