stk

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: MIT Imports: 10 Imported by: 3

README

STK

Server toolkit - minimal and simple framework for developing server in golang

what is included

  • go's native http server wrapper
    • uses net/http package
    • Get, Post, Put, Delete, Patch methods
  • httprouter for routing
  • middleware support for all routes
  • logger by logrus package
  • utils
    • db connection helpers
    • password hashing using argon2
    • loading env variables

get started

package main

import (
	"net/http"

	"github.com/adharshmk96/stk"
)

func main() {
	config := stk.ServerConfig{
		Port:           "0.0.0.0:8080",
		RequestLogging: 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()
}

middleware

you can add any middleware by simply creating a function like this and adding it to server.Use()

middleware := func(next stk.HandlerFunc) stk.HandlerFunc {
	return func(c stk.Context) {
		if ctx.Request.URL.Path == "/blocked" {
  			ctx.Status(http.StatusForbidden).JSONResponse("blocked")
			return
  		}
		next(c)
	}
}

server.Use(middleware)

publish new version

make patch / minor / major 
make publish

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 interface {
	// http objects
	GetRequest() *http.Request
	GetWriter() http.ResponseWriter

	// get data from request
	GetParam(key string) string
	GetQueryParam(key string) string
	GetAllowedOrigins() []string
	DecodeJSONBody(v interface{}) error

	// set data for response
	Status(status int) Context
	SetHeader(string, string)
	RawResponse(raw []byte)
	JSONResponse(data interface{})

	// logger
	Logger() *logrus.Logger
}

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 Server

type Server struct {
	Router      *httprouter.Router
	Middlewares []Middleware
	Config      *ServerConfig
	Logger      *logrus.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
	AllowedOrigins []string
	Logger         *logrus.Logger
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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