webserver

package module
v0.0.0-...-8310fa3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: BSD-3-Clause Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger defaultLogger

Functions

func ApplyErrorHandler

func ApplyErrorHandler[T any, S any](s *Server[S], fn func(req *Request, code Error) T)

Specify the function for Server s to call when an error code is returned. It can return any type T, it will be delivered under the same rules as any given route's returned data type

Types

type Csver

type Csver interface {
	AsCsv() []byte
}

type Error

type Error struct {
	Code  uint
	Error error
}

type EventStreamHandler

type EventStreamHandler func(req *Request) <-chan EventStreamer

type EventStreamer

type EventStreamer interface {
	AsEventStream() string
}

type FormDataParser

type FormDataParser interface {
	ParseFormData(io.Reader) *Error
}

type Htmler

type Htmler interface {
	AsHtml() []byte
}

type InMemorySessionStore

type InMemorySessionStore[T any] struct {
	Sessions map[string]T
	// contains filtered or unexported fields
}

func NewInMemorySessionStore

func NewInMemorySessionStore[T any]() *InMemorySessionStore[T]

func (InMemorySessionStore[T]) Delete

func (store InMemorySessionStore[T]) Delete(token string) error

func (*InMemorySessionStore[T]) Get

func (store *InMemorySessionStore[T]) Get(token string) (interface{}, error)

func (*InMemorySessionStore[T]) ParseToken

func (store *InMemorySessionStore[T]) ParseToken(header http.Header) string

func (*InMemorySessionStore[T]) Save

func (store *InMemorySessionStore[T]) Save(token string, data interface{}) error

type Jsoner

type Jsoner interface {
	AsJson() []byte
}

type Logger

type Logger interface {
	LogRequest(req *Request)
	LogMessage(req *Request, msg any)
	LogPanic(req *Request, p any)
	LogError(req *Request, err error)
}

type Middleware

type Middleware func(req *Request) *Error

type MultipartFormDataParser

type MultipartFormDataParser interface {
	ParseMultipartFormData(io.Reader, string) *Error
}

type PlainTextParser

type PlainTextParser interface {
	ParsePlainText(io.Reader) *Error
}

type Request

type Request struct {
	Session interface{}
	Verb    Verb
	Path    string
	Headers http.Header
	Cookies []*http.Cookie

	Body            interface{} // TODO: generic without breaking Request logic?
	Context         context.Context
	ResponseHeaders http.Header
	ResponseCode    int
	// contains filtered or unexported fields
}

func (*Request) BodySize

func (req *Request) BodySize() uint

func (*Request) ResponseSize

func (req *Request) ResponseSize() uint

func (*Request) SetCookie

func (req *Request) SetCookie(cookie http.Cookie)

func (*Request) Start

func (req *Request) Start() time.Time

type RequestBody

type RequestBody struct {
	url.Values
	Files map[string][]multipart.File
}

func (*RequestBody) ParseFormData

func (body *RequestBody) ParseFormData(rdr io.Reader) *Error

func (*RequestBody) ParseMultipartFormData

func (body *RequestBody) ParseMultipartFormData(rdr io.Reader, boundary string) *Error

type Route

type Route[B any, T any] struct {
	// contains filtered or unexported fields
}

func ApplyRoute

func ApplyRoute[T any, S any, B any](s *Server[S], Path string, body B, handlers map[Verb]func(req *Request) (T, *Error)) *Route[B, T]

You're not able to use generics on a method, so going through a public function which accepts the Server object is the least-bad way to get type safety in the handlers.

func (*Route[B, T]) EventStream

func (r *Route[B, T]) EventStream(handler EventStreamHandler)

func (*Route[B, T]) Middleware

func (r *Route[B, T]) Middleware(mw Middleware)

func (*Route[B, T]) Websocket

func (r *Route[B, T]) Websocket(handler WebsocketHandler)

type Server

type Server[S any] struct {
	Logger       defaultLogger
	SecureConfig *tls.Config
	MaxPostSize  uint
	// contains filtered or unexported fields
}

func New

func New[S any](sessionStore SessionStore) *Server[S]

func (*Server[S]) Middleware

func (s *Server[S]) Middleware(mw Middleware)

func (*Server[S]) PublicRoute

func (s *Server[S]) PublicRoute(dirPath string, pathPrefix string)

func (*Server[S]) RegisterContentTypeInterface

func (s *Server[S]) RegisterContentTypeInterface(contentType string, i interface{})

func (*Server[S]) Start

func (s *Server[S]) Start(addr string) (string, uint, error)

Starts listening on the server Returns host and port used (in case 0 is returned), or error if there is one

type Session

type Session[T any] struct {
	Token string
	Data  *T
	// contains filtered or unexported fields
}

type SessionStore

type SessionStore interface {
	ParseToken(http.Header) string
	Get(token string) (interface{}, error)
	Save(token string, data interface{}) error
	Delete(token string) error
}

Sessions could be via Cookies Stored in DB Stored in memory

type SessionStoreContext

type SessionStoreContext interface {
	GetCtx(token string, ctx context.Context) (interface{}, error)
	SaveCtx(token string, data interface{}, ctx context.Context) error
	DeleteCtx(token string, ctx context.Context) error
}

type Sessionless

type Sessionless struct{}

Sessionless is a placeholder for anyone who wants to run a sessionless-server

func (Sessionless) Delete

func (_ Sessionless) Delete(token string) error

func (Sessionless) Get

func (_ Sessionless) Get(token string) (interface{}, error)

func (Sessionless) ParseToken

func (_ Sessionless) ParseToken(header http.Header) string

func (Sessionless) Save

func (_ Sessionless) Save(token string, data interface{}) error

type Verb

type Verb byte
const (
	GET Verb = iota + 1
	POST
	PUT
	DELETE
	HEAD
	OPTIONS
	CONNECT
	TRACE
	PATCH
)

func ParseVerb

func ParseVerb(in string) (Verb, error)

func (Verb) String

func (v Verb) String() string

type WebsocketHandler

type WebsocketHandler func(req *Request, inFeed <-chan []byte) <-chan []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳