ss

package module
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 17 Imported by: 0

README

ss

Golang Simple Server library to easily write backend and connect it to the frontend.

TODO

Documentation?

Documentation

Index

Constants

View Source
const (
	// Using the UTF-8 by default.
	ContentTypeUnknown = "application/octet-stream"
	ContentTypeBinary
	ContentTypePlain       = "text/plain"
	ContentTypeCSS         = "text/css"
	ContentTypeHTML        = "text/html"
	ContentTypeJSON        = "application/json"
	ContentTypeProblemJSON = "application/problem+json"
	ContentTypeEncodedURL  = "application/x-www-form-urlencoded"
)
View Source
const StdRandomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Variables

View Source
var (
	DupDefErr             = errors.New("duplicate route define")
	UnknownContentTypeErr = errors.New("unknown content type")
	EmptyScanErr          = errors.New("empty body to scan")
)

Functions

func RandomBase64Bytes added in v0.15.0

func RandomBase64Bytes(n int) (string, error)

Generates random sequence of bytes with length n encoded to the Base64.

func RandomString added in v0.15.0

func RandomString(n int, charset string) (string, error)

Generates random string with length n and consisting of characters present in charset.

func StdRandomString added in v0.15.0

func StdRandomString(n int) (string, error)

RandomString shortcut to use the StdRandomString as the second argument.

Types

type AbsPathRouter added in v0.8.0

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

The type implements absolute paths routing for requests.

func AbsPath added in v0.8.0

func AbsPath() *AbsPathRouter

Returns new empty AbsPathRouter with def as the default fallback.

func (*AbsPathRouter) Case added in v0.8.0

func (router *AbsPathRouter) Case(pth string, handler Handler) *AbsPathRouter

Define new handler for the specified absolute path. Will panic on empty path or if path does not start with slash..

func (*AbsPathRouter) Default added in v0.8.0

func (router *AbsPathRouter) Default(handler Handler) *AbsPathRouter

func (*AbsPathRouter) Handle added in v0.8.0

func (router *AbsPathRouter) Handle(c *Context)

type Chain added in v0.13.0

type Chain []Chainer

type ChainFunc added in v0.13.0

type ChainFunc func(Handler) Handler

func (ChainFunc) Chain added in v0.13.0

func (fn ChainFunc) Chain(h Handler) Handler

type Chainer added in v0.13.0

type Chainer interface {
	Chain(h Handler) Handler
}

type CharSet

type CharSet string
const (
	CharSetUTF8 CharSet = "utf-8"
)

func (CharSet) KeyValue

func (cs CharSet) KeyValue() [2]string

type ContentOption

type ContentOption interface {
	KeyValue() [2]string
}

type Context

type Context struct {
	// Implementing the context shit.
	context.Context
	// The http.Request, expand.
	R *Request

	// Relative URL.
	RelUrl *url.URL

	W ResponseWriter

	// Custom data to expand the Context type..
	Env any
	App any
	// contains filtered or unexported fields
}

The type contains all information regarding HTTP request and interaction with it.

func (*Context) AbsPath

func (c *Context) AbsPath() string

Get absolute path of the request.

func (*Context) BadRequest added in v0.7.0

func (c *Context) BadRequest(errs ...error)

Send 400 bad request back to the client with the specified error.

func (*Context) Close

func (c *Context) Close()

Closes the requests body after finishes scaning.

func (*Context) ContentType

func (c *Context) ContentType() string

Get the request content type.

func (*Context) Cookie added in v0.15.0

func (c *Context) Cookie(name string) (*Cookie, error)

func (*Context) Cookies added in v0.15.0

func (c *Context) Cookies() []*Cookie

func (*Context) CookiesNamed added in v0.15.0

func (c *Context) CookiesNamed(name string) []*Cookie

func (*Context) ErrorJSON added in v0.10.0

func (c *Context) ErrorJSON(err error, detail ...string) error

Send a JSON error with the specified error.

func (*Context) Header

func (c *Context) Header(name string) string

Get the request header by name, returns the first one matching.

func (*Context) Headers added in v0.15.1

func (c *Context) Headers(name string) ([]string, bool)

Get request headers by name, returns all including duplicates.

func (*Context) InternalServerError

func (c *Context) InternalServerError(errs ...error)

Send internal server error to the client.

func (*Context) Method

func (c *Context) Method() string

Get the method of the request (GET, POST etc)

func (*Context) NotFound

func (c *Context) NotFound()

Send Not Found to the client.

func (*Context) Path

func (c *Context) Path() string

Get the local part of the absolute path.

func (*Context) PathAsInt added in v0.7.1

func (c *Context) PathAsInt() (int64, bool, error)

Parses the output of Path() into int64.

func (*Context) PathPrefix

func (c *Context) PathPrefix() string

Get prefix of the path.

func (*Context) Printf

func (c *Context) Printf(format string, v ...any) (int, error)

Writes a formatted string back to the client.

func (*Context) Query

func (c *Context) Query() url.Values

Get the query from the request.

func (*Context) Redirect

func (c *Context) Redirect(u string, status int)

Sends the redirect code back to the client.

func (*Context) RedirectFound added in v0.15.1

func (c *Context) RedirectFound(u string)

func (*Context) Referer added in v0.15.1

func (c *Context) Referer() string

Returns the Request referer .

func (*Context) Render added in v0.12.0

func (c *Context) Render(tmpl, view string, val any)

Render the specified template with the specified view using the value.

func (*Context) Scan

func (c *Context) Scan(v any) error

Scan the incoming value from body depending on the content type of the request into a structure or map[string] any. If you want to be sure about what kind of encoding is going to be applied use more specific methods (eg ScanJSON) or implement it yourself.

func (*Context) ScanFormURL added in v0.13.1

func (c *Context) ScanFormURL(v any) error

Scan the X-WWW-FORM-URLENCODED value into the v.

func (*Context) ScanJSON added in v0.7.0

func (c *Context) ScanJSON(v any) error

Reads from the request body as if it was JSON and stores in reciever (v).

func (*Context) ScanURL added in v0.14.0

func (c *Context) ScanURL(v any) error

Scan the X-WWW-FORM-URLENCODED value into the v taking the query from the Request URL. Respects types and works like json.Unmarshal.

Know that the method is quite slow, since inside it creates maps so if you need optimization use the *Context.Query() to your liking.

func (*Context) SendErrorJSON added in v0.10.0

func (c *Context) SendErrorJSON(e ErrorJSON) error

Send the structured error json with some default values if not specified.

func (*Context) SendJSON added in v0.7.0

func (c *Context) SendJSON(v any) error

Sends a JSON marshalized value back to the client setting the corresponding content type.

func (*Context) SendStatusCode added in v0.7.0

func (c *Context) SendStatusCode(code int)

Sends the specified status code back to the client.

func (*Context) ServeFile added in v0.11.0

func (c *Context) ServeFile(name string)

Shortcut for the http.ServeFile

func (*Context) SetContentType

func (c *Context) SetContentType(typ string, opts ...ContentOption)

Set the reply content type.

func (*Context) SetCookies added in v0.15.0

func (c *Context) SetCookies(cookie *Cookie, rest ...*Cookie)

Send the specified Cookies. Must be called before any *Context.SetHeader calls, because once the headers are sent you cannot add more cookies.

func (*Context) SetHeader

func (c *Context) SetHeader(k, v string)

Set reply headers by key/value combination.

func (*Context) SetStatus

func (c *Context) SetStatus(status int)

Set the reply status code.

func (*Context) Unauthorized added in v0.15.1

func (c *Context) Unauthorized()
type Cookie = http.Cookie

type DebugHandler added in v0.10.3

type DebugHandler struct{}

The type implements simple debuging handler.

func Debug added in v0.10.3

func Debug() *DebugHandler

Return new DebugHandler.

func (*DebugHandler) Handle added in v0.10.3

func (handler *DebugHandler) Handle(c *Context)

Implementing Handler.

type Decoder

type Decoder interface {
	Decode(any) error
}

type ErrorJSON added in v0.10.0

type ErrorJSON struct {
	Type     string `json:"type"`
	Title    string `json:"title"`
	Status   int    `json:"status"`
	Detail   string `json:"detail"`
	Instance string `json:"instance"`
}
{
  "type": "https://example.com",
  "title": "You do not have enough credit.",
  "status": 403,
  "detail": "Your current balance is 30, but that costs 50.",
  "instance": "/account/12345/msgs/abc"
}

type File added in v0.11.0

type File struct {
	http.File
	Options FsOptions
}

dotFileHidingFile is the http.File use in dotFileHidingFileSystem. It is used to wrap the Readdir method of http.File so that we can remove files and directories that start with a period from its output.

func (File) Readdir added in v0.11.0

func (f File) Readdir(n int) (fis []fs.FileInfo, err error)

Readdir is a wrapper around the Readdir method of the embedded File that filters out all files that start with a period in their name.

type FileSystem added in v0.11.0

type FileSystem struct {
	http.FileSystem
	// The options will be passed to every file.
	Options FsOptions
	// contains filtered or unexported fields
}

HTTP file systems that allows easier customization than the one from the standard library

func StaticDir

func StaticDir(pth string) *FileSystem

func (*FileSystem) Handle added in v0.11.0

func (fsys *FileSystem) Handle(c *Context)

func (FileSystem) Open added in v0.11.0

func (fsys FileSystem) Open(name string) (http.File, error)

Open is a wrapper around the Open method of the embedded FileSystem that serves a 403 permission error when name has a file or directory with whose name starts with a period in its path.

func (*FileSystem) ShowDot added in v0.11.0

func (fsys *FileSystem) ShowDot(show bool) *FileSystem

type FsOptions added in v0.11.0

type FsOptions struct {
	ShowDot bool
}

The type provides options on static files access and listing.

type Func

type Func func(*Context)

The type is used for custom-one-place implementation of handling.

func (Func) Handle

func (fn Func) Handle(c *Context)

type Handler

type Handler interface {
	Handle(c *Context)
}

func Chained added in v0.13.0

func Chained(c Chain, h Handler) Handler

Chain Chainers into one Handler and return it.

func Redirect

func Redirect(u string, status int) Handler

Returns the handler that redirects to the specified URL (u)

func StaticFile

func StaticFile(pth string) Handler

type HttpHandler

type HttpHandler = http.Handler

type HttpHandlerFunc

type HttpHandlerFunc = http.HandlerFunc

type MethodRouter

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

The type implements method routing.

func Method

func Method() *MethodRouter

Returns new empty MethodRouter.

func (*MethodRouter) Case

func (mr *MethodRouter) Case(
	method string,
	handler Handler,
) *MethodRouter

Define new handler for the specified method.

func (*MethodRouter) Default added in v0.8.0

func (mr *MethodRouter) Default(
	handler Handler,
) *MethodRouter

func (*MethodRouter) Handle

func (mr *MethodRouter) Handle(c *Context)

Implementing the Handler.

type PathRouter

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

The type implements path routing for requests.

func Path

func Path() *PathRouter

Returns new empty PathRouter with the def specified as the default fallback.

func (*PathRouter) Case

func (router *PathRouter) Case(pth string, handler Handler) *PathRouter

Define new handler for the specified path. The defined path must not contain slashes and will panic otherwise.

func (*PathRouter) Default added in v0.8.0

func (router *PathRouter) Default(handler Handler) *PathRouter

func (*PathRouter) Handle

func (router *PathRouter) Handle(c *Context)

Implementing the Handler.

type Request

type Request = http.Request

type ResponseWriter

type ResponseWriter = http.ResponseWriter

type RootRouter

type RootRouter struct {

	// For global data.
	App any
	// contains filtered or unexported fields
}

The type implements the entry point for all the request for the server.

func Root

func Root(app any, handler Handler) *RootRouter

Return the new RootRouter with the specified handler. If you want to get most of the high-level you will NEED this part of the library. Use it anywhere where you create the new service, eg in every server because it does much of initialization and sanitazation in the ServeHTTP.

func (*RootRouter) Handle

func (router *RootRouter) Handle(c *Context)

Implementing the Handler.

func (*RootRouter) ServeHTTP

func (router *RootRouter) ServeHTTP(w ResponseWriter, r *Request)

Implementing the http.Handler .

func (*RootRouter) Templates added in v0.12.0

func (router *RootRouter) Templates(tmpls tplx.Templates) *RootRouter

type Server

type Server = http.Server

type Wrap

type Wrap struct {
	UseRelUrl bool
	HttpHandler
}

The wrapper for the standard http.Handler(s).

func (Wrap) Handle

func (w Wrap) Handle(c *Context)

Directories

Path Synopsis
cmd
test command

Jump to

Keyboard shortcuts

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