web

package module
v0.0.0-...-85f0486 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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
}

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