web

package module
v0.0.0-...-1d82323 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: MIT Imports: 13 Imported by: 0

README

Web

A fast HTTP/1.1 web server that can sit behind a reverse proxy like caddy or nginx for HTTP 1/2/3 support.

Features

  • High performance
  • Low latency
  • Scales incredibly well with the number of routes

Installation

go get git.asharkk.net/Go/Web

Usage

s := web.NewServer()

// Static route
s.Get("/", func(ctx web.Context) error {
	return ctx.String("Hello")
})

// Parameter route
s.Get("/blog/:post", func(ctx web.Context) error {
	return ctx.String(ctx.Request().Param("post"))
})

// Wildcard route
s.Get("/images/*file", func(ctx web.Context) error {
	return ctx.String(ctx.Request().Param("file"))
})

// Middleware
s.Use(func(ctx web.Context) error {
	start := time.Now()

	defer func() {
		fmt.Println(ctx.Request().Path(), time.Since(start))
	}()

	return ctx.Next()
})

s.Run(":8080")

Documentation

Index

Constants

View Source
const (
	HeaderContentType      = "Content-Type"
	HeaderContentLength    = "Content-Length"
	HeaderHost             = "Host"
	HeaderAccept           = "Accept"
	HeaderUserAgent        = "User-Agent"
	HeaderAcceptEncoding   = "Accept-Encoding"
	HeaderAcceptLanguage   = "Accept-Language"
	HeaderConnection       = "Connection"
	HeaderCookie           = "Cookie"
	HeaderSetCookie        = "Set-Cookie"
	HeaderLocation         = "Location"
	HeaderAuthorization    = "Authorization"
	HeaderCacheControl     = "Cache-Control"
	HeaderOrigin           = "Origin"
	HeaderReferer          = "Referer"
	HeaderTransferEncoding = "Transfer-Encoding"
)

Common HTTP header keys

Variables

View Source
var (
	// Content type headers
	HeaderContentTypeJSON      = Header{Key: HeaderContentType, Value: "application/json"}
	HeaderContentTypeHTML      = Header{Key: HeaderContentType, Value: "text/html"}
	HeaderContentTypePlain     = Header{Key: HeaderContentType, Value: "text/plain"}
	HeaderContentTypeXML       = Header{Key: HeaderContentType, Value: "application/xml"}
	HeaderContentTypeForm      = Header{Key: HeaderContentType, Value: "application/x-www-form-urlencoded"}
	HeaderContentTypeMultipart = Header{Key: HeaderContentType, Value: "multipart/form-data"}

	// Connection headers
	HeaderConnectionClose     = Header{Key: HeaderConnection, Value: "close"}
	HeaderConnectionKeepAlive = Header{Key: HeaderConnection, Value: "keep-alive"}
)

Pre-allocated common headers

Functions

func FindHeader

func FindHeader(headers []Header, key string) (string, bool)

FindHeader looks for a header by key in a slice of headers

func SetHeader

func SetHeader(headers *[]Header, key string, value string)

SetHeader sets a header value in a slice of headers

Types

type Context

type Context interface {
	Bytes([]byte) error
	Error(...any) error
	Next() error
	Redirect(int, string) error
	Request() Request
	Response() Response
	Status(int) Context
	String(string) error
}

Interface for a request and its response.

type Handler

type Handler func(Context) error
type Header struct {
	Key   string
	Value string
}

Header represents an HTTP header with key and value

type Request

type Request interface {
	Header(string) string
	Host() string
	Method() string
	Path() string
	Scheme() string
	Param(string) string
	Body() []byte
}

Interface for HTTP requests.

type Response

type Response interface {
	io.Writer
	io.StringWriter
	Body() []byte
	Header(string) string
	SetHeader(key string, value string)
	SetBody([]byte)
	SetStatus(int)
	Status() int
}

Interface for an HTTP response.

type Server

type Server interface {
	Get(path string, handler Handler)
	Post(path string, handler Handler)
	Put(path string, handler Handler)
	Delete(path string, handler Handler)
	Patch(path string, handler Handler)
	Request(method string, path string, headers []Header, body io.Reader) Response
	Router() *router.Router[Handler]
	Run(address string) error
	Use(handlers ...Handler)
}

Interface for an HTTP server.

func NewServer

func NewServer() Server

Creates a new HTTP server.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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