si

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 11 Imported by: 0

README

Si — simple Go HTTP-server

Si is a simple http-server and wrapper for the chi-router which provides a more convenient way to handle requests.

It has no dependencies other than chi and is designed to be as lightweight as possible.

Features

  • ✨ Easy to install, easy to use, almost dependency-free
  • 🗂 Ability to use libraries (e.g. middlewares) built around chi-router
  • 📦 Ability to get http.Request and http.ResponseWriter for advanced needs from the si.Context
  • 🪶 Write less boilerplate code!

Installation

go get -u github.com/revenkroz/si@latest

Usage

package main

import (
	"github.com/go-chi/chi/v5/middleware"
	"github.com/revenkroz/si"
)

func main() {
	server := si.CreateServer(
		"localhost:8080",
		// List of middlewares
		[]si.Middleware{
			middleware.Logger,
		},
	)

	// Adds a new GET-route under /
	server.Get("/", func(ctx si.Context) {
		// Sends a string response with status code 200
		ctx.SendString("Hello, world!", 200)
	})

	// Adds a new GET-route under /json
	server.Get("/json", func(ctx si.Context) {
		// Sends a json response with status code 200
		ctx.SendJSON(map[string]string{
			"message": "Hello, world!",
		}, 200)
	})

	// Adds a new GET-route under /j
	server.Get("/j", func(ctx si.Context) {
		// Does the same as the previous route, but with a shortcut method
		ctx.SJ(map[string]string{
			"message": "Hello, world!",
		})
	})

	// Creates a new subrouter which will be mounted under /hello (e.g. /hello/{name})
	subrouter := si.NewRouter()
	subrouter.Get("/{name}", func(ctx si.Context) {
		// Gets the value of the name parameter as a string
		name := ctx.ParamString("name")

		ctx.SendString("Hello, "+name+"!", 200)
	})

	// Adds new group of routes under /hello
	server.AddRoute("/hello", subrouter)

	// Starts the server
	server.Start()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request  *http.Request
	Response http.ResponseWriter
}

func Si

func Si(
	request *http.Request,
	response http.ResponseWriter,
) *Context

Si creates a new context

func (*Context) Accepts

func (ctx *Context) Accepts(offers ...string) bool

Accepts checks if the client accepts a certain type of content

func (*Context) CookieString

func (ctx *Context) CookieString(key string) string

CookieString gets a cookie value as a string

func (*Context) FullUrl

func (ctx *Context) FullUrl() string

FullUrl gets the full URL

func (*Context) GetAttribute

func (ctx *Context) GetAttribute(key ContextKey) interface{}

GetAttribute gets a value from the context

func (*Context) GetFormData

func (ctx *Context) GetFormData() (map[string][]string, error)

GetFormData gets the form data

func (*Context) GetRawContent

func (ctx *Context) GetRawContent() ([]byte, error)

GetRawContent gets the raw content

func (*Context) HeaderString

func (ctx *Context) HeaderString(key string) string

HeaderString gets a header value as a string

func (*Context) Host added in v1.1.0

func (ctx *Context) Host() string

Host gets the host

func (*Context) IP

func (ctx *Context) IP() string

IP gets the IP address of the client

func (*Context) Method added in v1.1.0

func (ctx *Context) Method() string

Method gets the HTTP method

func (*Context) NotFound

func (ctx *Context) NotFound()

NotFound sends a 404 response

func (*Context) ParamBool

func (ctx *Context) ParamBool(key string) bool

ParamBool gets a URL parameter as a bool

func (*Context) ParamInt

func (ctx *Context) ParamInt(key string) int

ParamInt gets a URL parameter as an int

func (*Context) ParamString

func (ctx *Context) ParamString(key string) string

ParamString gets a URL parameter as a string

func (*Context) Path

func (ctx *Context) Path() string

Path gets the path

func (*Context) PathAndQuery

func (ctx *Context) PathAndQuery() string

PathAndQuery gets the path and query

func (*Context) Query

func (ctx *Context) Query() map[string]any

Query gets the query parameters

func (*Context) QueryBool

func (ctx *Context) QueryBool(key string) bool

QueryBool gets a query parameter as a bool

func (*Context) QueryInt

func (ctx *Context) QueryInt(key string) int

QueryInt gets a query parameter as an int

func (*Context) QueryString

func (ctx *Context) QueryString(key string) string

QueryString gets a query parameter as a string

func (*Context) Redirect

func (ctx *Context) Redirect(url string, statusCode int)

Redirect redirects to a URL

func (*Context) SB

func (ctx *Context) SB(data []byte)

SB is a shortcut for SendBytes

func (*Context) SJ

func (ctx *Context) SJ(data any)

SJ is a shortcut for SendJSON

func (*Context) SS

func (ctx *Context) SS(data string)

SS is a shortcut for SendString

func (*Context) SendBytes

func (ctx *Context) SendBytes(data []byte, statusCode int)

SendBytes sends a byte array

func (*Context) SendErrorJSON

func (ctx *Context) SendErrorJSON(message string, statusCode int)

SendErrorJSON sends an error JSON response

func (*Context) SendJSON

func (ctx *Context) SendJSON(data any, statusCode int)

SendJSON sends a JSON response

func (*Context) SendStream

func (ctx *Context) SendStream(stream io.ReadCloser, statusCode int)

SendStream sends a stream

func (*Context) SendString

func (ctx *Context) SendString(data string, statusCode int)

SendString sends a string

func (*Context) SetAttribute

func (ctx *Context) SetAttribute(key ContextKey, value interface{})

SetAttribute sets a key-value pair in the context

func (*Context) SetCookie added in v1.1.0

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

SetCookie writes a cookie Can be used to clear a cookie by setting MaxAge to -1

func (*Context) UnmarshalJSONBody

func (ctx *Context) UnmarshalJSONBody(v interface{}) error

UnmarshalJSONBody unmarshals the JSON body

func (*Context) WriteEarlyHintScript

func (ctx *Context) WriteEarlyHintScript(path string)

WriteEarlyHintScript writes a preload hint for a script

func (*Context) WriteEarlyHintStyle

func (ctx *Context) WriteEarlyHintStyle(path string)

WriteEarlyHintStyle writes a preload hint for a style

func (*Context) WriteHeader

func (ctx *Context) WriteHeader(key string, value string)

WriteHeader writes a header

func (*Context) WriteHeaders

func (ctx *Context) WriteHeaders(headers map[string]string)

WriteHeaders writes multiple headers

func (*Context) WriteStatus

func (ctx *Context) WriteStatus(statusCode int)

WriteStatus writes a status code should be called after all headers are written

type ContextKey

type ContextKey string

func (ContextKey) String

func (c ContextKey) String() string

type Handler

type Handler func(ctx *Context)

type HandlerFunc

type HandlerFunc Handler

type Map

type Map map[string]interface{}

type Middleware

type Middleware func(http.Handler) http.Handler

func MW

func MW(f Handler) Middleware

MW creates a new middleware

type Router

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) Connect

func (r *Router) Connect(pattern string, handler HandlerFunc)

func (*Router) Delete

func (r *Router) Delete(pattern string, handler HandlerFunc)

func (*Router) Get

func (r *Router) Get(pattern string, handler HandlerFunc)

func (*Router) Handle

func (r *Router) Handle(pattern string, handler http.Handler)

func (*Router) Head

func (r *Router) Head(pattern string, handler HandlerFunc)

func (*Router) Mount

func (r *Router) Mount(pattern string, router *Router)

func (*Router) NotFound

func (r *Router) NotFound(handler HandlerFunc)

func (*Router) Options

func (r *Router) Options(pattern string, handler HandlerFunc)

func (*Router) Patch

func (r *Router) Patch(pattern string, handler HandlerFunc)

func (*Router) Post

func (r *Router) Post(pattern string, handler HandlerFunc)

func (*Router) PrintRoutes

func (r *Router) PrintRoutes() error

func (*Router) Put

func (r *Router) Put(pattern string, handler HandlerFunc)

func (*Router) Trace

func (r *Router) Trace(pattern string, handler HandlerFunc)

func (*Router) Use

func (r *Router) Use(middleware Middleware)

type Server

type Server struct {
	Router *Router
	// contains filtered or unexported fields
}

Server is a wrapper around http.Server

func CreateServer

func CreateServer(
	listenAddress string,
	middlewares []Middleware,
) *Server

CreateServer creates a new server

func (*Server) AddRoute

func (s *Server) AddRoute(pattern string, subrouter *Router)

AddRoute adds a subrouter to the server

func (*Server) Connect added in v1.1.0

func (s *Server) Connect(pattern string, handler HandlerFunc)

func (*Server) Delete added in v1.1.0

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

func (*Server) Get added in v1.1.0

func (s *Server) Get(pattern string, handler HandlerFunc)

func (*Server) Head added in v1.1.0

func (s *Server) Head(pattern string, handler HandlerFunc)

func (*Server) Options added in v1.1.0

func (s *Server) Options(pattern string, handler HandlerFunc)

func (*Server) Patch added in v1.1.0

func (s *Server) Patch(pattern string, handler HandlerFunc)

func (*Server) Post added in v1.1.0

func (s *Server) Post(pattern string, handler HandlerFunc)

func (*Server) Put added in v1.1.0

func (s *Server) Put(pattern string, handler HandlerFunc)

func (*Server) Start

func (s *Server) Start() error

Start starts the server

func (*Server) Stop

func (s *Server) Stop() error

Stop stops the server

func (*Server) Trace added in v1.1.0

func (s *Server) Trace(pattern string, handler HandlerFunc)

Jump to

Keyboard shortcuts

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