stk

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 8 Imported by: 3

README

stk

Server tool kit - framework for developing server in golang

what is included

  • go's native http server wrapper
    • use net/http package
    • Get, Post, Put, Delete, Patch methods
  • Middleware support
    • support middleware for all routes
  • logger
    • zap logger by uber go zap package
  • utils
    • password hashing using argon2
    • loading env variables

usage

package main

import (
	"net/http"

	"github.com/adharshmk96/stk"
)

func main() {
	config := stk.ServerConfig{
		Port:           "0.0.0.0:8080",
		RequestLogging: true,
		CORS:           true,
	}
	// create new server
	server := stk.NewServer(&config)

	// add routes
	server.Get("/", func(c *stk.Context) {
		c.Status(http.StatusOK).JSONResponse(stk.Map{"message": "Hello World"})
	})

	// start server
	server.Start()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyTooLarge = errors.New("request body too large")
View Source
var ErrInternalServer = errors.New("internal server error")
View Source
var ErrInvalidJSON = errors.New("invalid json")

Functions

func NormalizePort

func NormalizePort(val string) string

Types

type Context

type Context struct {
	Request *http.Request
	Writer  http.ResponseWriter

	Params httprouter.Params

	Logger *zap.Logger

	ResponseStatus int
}

func (*Context) DecodeJSONBody

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

func (*Context) GetParam

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

func (*Context) GetQueryParam

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

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

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

Status sets the status code of the response

type HandlerFunc

type HandlerFunc func(*Context)

func CORS

func CORS(next HandlerFunc) HandlerFunc

func SecurityHeaders

func SecurityHeaders(next HandlerFunc) HandlerFunc

type Map

type Map map[string]interface{}

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

This implementation of middleware will enable middleware chaining

type Server

type Server struct {
	Router      *httprouter.Router
	Middlewares []Middleware
	Config      *ServerConfig
	Logger      *zap.Logger
}

func NewServer

func NewServer(config *ServerConfig) *Server

NewServer creates a new server instance

func (*Server) Delete

func (s *Server) Delete(path string, handler HandlerFunc)

func (*Server) Get

func (s *Server) Get(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) Start

func (s *Server) Start()

Start starts the server on the configured port

func (*Server) Use

func (s *Server) Use(middleware Middleware)

Use adds a middleware to the server usage example: server.Use(stk.RequestLogger())

type ServerConfig

type ServerConfig struct {
	Port           string
	RequestLogging bool
	CORS           bool
	Logger         *zap.Logger
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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