kid

package module
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 15 Imported by: 0

README

Kid

Simple web framework written in Go

Go Reference

Installation

go get -u github.com/Tarocch1/kid

Quickstart

package main

import (
    "github.com/Tarocch1/kid"
    "github.com/Tarocch1/kid/middlewares/recovery"
)

func main() {
    k := kid.New()
    k.Use(recovery.New())

    k.Get("/", func(c *kid.Ctx) error {
        return c.String("Hello, World 👋!")
    })

    k.Listen(":3000")
}

Documentation

Index

Constants

View Source
const (
	HeaderRequestId                     = "X-Request-ID"
	HeaderContentDisposition            = "Content-Disposition"
	HeaderContentType                   = "Content-Type"
	HeaderLocation                      = "Location"
	HeaderAuthorization                 = "Authorization"
	HeaderWWWAuthenticate               = "WWW-Authenticate"
	HeaderOrigin                        = "Origin"
	HeaderVary                          = "Vary"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
)

Http header

View Source
const (
	CtxRequestId         = "Ctx-Request-ID"
	CtxBasicAuthUsername = "Ctx-Basic-Auth-Username"
	CtxBasicAuthPassword = "Ctx-Basic-Auth-Password"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ErrorHandler is executed when an error is returned from kid.HandlerFunc.
	//
	// Default: DefaultErrorHandler
	ErrorHandler ErrorHandlerFunc
}

type Ctx

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

func (*Ctx) AddHeader

func (c *Ctx) AddHeader(key string, value string) *Ctx

AddHeader adds a header value.

func (*Ctx) Body

func (c *Ctx) Body() []byte

Body gets request's raw body.

func (*Ctx) BodyParser

func (c *Ctx) BodyParser(out interface{}) error

BodyParser parsers body to any struct.

func (*Ctx) ClearCookie

func (c *Ctx) ClearCookie(name string) *Ctx

ClearCookie clears a cookie.

func (*Ctx) Cookies

func (c *Ctx) Cookies() []*http.Cookie

Cookies gets request's cookies.

func (*Ctx) FormFile

func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error)

FormFile gets a form file by key.

func (*Ctx) FormValue

func (c *Ctx) FormValue(key string, defaultValue ...string) string

FormValue gets a form value by key.

func (*Ctx) Get

func (c *Ctx) Get(key string) interface{}

Get gets some value from ctx.

func (*Ctx) GetCookie

func (c *Ctx) GetCookie(name string) *http.Cookie

GetCookie gets a cookie by name.

func (*Ctx) GetHeader

func (c *Ctx) GetHeader(key string, defaultValue ...string) string

GetHeader gets a header's first value by key.

func (*Ctx) GetHeaderValues

func (c *Ctx) GetHeaderValues(key string, defaultValue ...[]string) []string

GetHeaderValues gets a header by key.

func (*Ctx) GetParam

func (c *Ctx) GetParam(key string, defaultValue ...string) string

GetParam gets a router path param value by key.

func (*Ctx) GetQuery

func (c *Ctx) GetQuery(key string, defaultValue ...string) string

GetQuery gets a query value by key.

func (*Ctx) Header

func (c *Ctx) Header() http.Header

Header gets request's Header.

func (*Ctx) Html

func (c *Ctx) Html(html string) error

Html sends html.

func (*Ctx) Json

func (c *Ctx) Json(data interface{}) error

Json sends json.

func (*Ctx) Method

func (c *Ctx) Method() string

Method returns request's method.

func (*Ctx) Next

func (c *Ctx) Next() error

Next starts the next middleware.

func (*Ctx) Params

func (c *Ctx) Params() map[string]string

Params gets all router path params.

func (*Ctx) Query

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

Query gets request's Query.

func (*Ctx) Redirect added in v0.2.0

func (c *Ctx) Redirect(target string, status ...int) error

Redirect redirects request to target with status.

func (*Ctx) SendFile added in v0.2.0

func (c *Ctx) SendFile(path string, download bool, fs ...http.FileSystem) error

SendFile reads file from fs at path and sends it.

func (*Ctx) SendStatus

func (c *Ctx) SendStatus(status int) error

SendStatus sets response's status and send.

func (*Ctx) Set

func (c *Ctx) Set(key string, value interface{})

Set sets some value to ctx.

func (*Ctx) SetCookie

func (c *Ctx) SetCookie(cookie *http.Cookie) *Ctx

SetCookie sets a cookie.

func (*Ctx) SetHeader

func (c *Ctx) SetHeader(key string, value string) *Ctx

SetHeader sets a header.

func (*Ctx) Status

func (c *Ctx) Status(status int) *Ctx

Status sets response's status.

func (*Ctx) Stream

func (c *Ctx) Stream(data []byte) error

Stream sends binary stream.

func (*Ctx) String

func (c *Ctx) String(format string, values ...interface{}) error

String sends string.

func (*Ctx) Url

func (c *Ctx) Url() *url.URL

Url returns request's URL.

type Error

type Error struct {
	// Http status
	Status int

	// Message to show
	Message string

	// Extra data
	Data interface{}
}

func NewError

func NewError(status int, message string, data interface{}) *Error

NewError creates *kid.Error.

func (*Error) Error

func (e *Error) Error() string

type ErrorHandlerFunc

type ErrorHandlerFunc func(*Ctx, error) error

ErrorHandlerFunc defines a function to process return errors or panic errors from handlers.

var DefaultErrorHandler ErrorHandlerFunc = func(c *Ctx, err error) error {
	message := fmt.Sprintf("%s %s", c.Method(), c.Url().RequestURI())
	if e, ok := err.(*Error); ok {
		errorLogger.Error(c, message, map[string]interface{}{
			"data": e.Data,
		}, e)
		return c.Status(e.Status).String(e.Message)
	} else {
		errorLogger.Error(c, message, nil, err)
		return c.Status(http.StatusInternalServerError).String(err.Error())
	}
}

DefaultErrorHandler that process return errors or panic errors from handlers.

type FileSystem added in v0.6.0

type FileSystem struct {
	// Index page filename, default: index.html
	Index string

	// Root dir, default: os.Getwd()
	Root string

	// Enable open file out of root path, default: false
	EnableOuter bool

	// FS that implement http.FileSystem, default: os
	FS http.FileSystem
}

Filesystem implement http.FileSystem

func (*FileSystem) Open added in v0.6.0

func (fs *FileSystem) Open(path string) (http.File, error)

Open opens file at given path

type HandlerFunc

type HandlerFunc func(*Ctx) error

HandlerFunc defines a function to serve HTTP requests.

type Kid

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

Kid application.

func New

func New(config ...Config) *Kid

New creates a kid app.

func (Kid) Delete

func (g Kid) Delete(pattern string, handler HandlerFunc)

Delete adds a delete router.

func (Kid) Get

func (g Kid) Get(pattern string, handler HandlerFunc)

Get adds a get router.

func (Kid) Group

func (g Kid) Group(prefix string) *group

Group creates a router group.

func (Kid) Head

func (g Kid) Head(pattern string, handler HandlerFunc)

Head adds a head router.

func (*Kid) Listen

func (k *Kid) Listen(addr string) (err error)

Listen starts server at addr.

func (*Kid) ListenTLS

func (k *Kid) ListenTLS(addr string, certFile string, keyFile string) (err error)

ListenTLS starts server at addr with https.

func (Kid) Patch

func (g Kid) Patch(pattern string, handler HandlerFunc)

Patch adds a patch router.

func (Kid) Post

func (g Kid) Post(pattern string, handler HandlerFunc)

Post adds a post router.

func (Kid) Put

func (g Kid) Put(pattern string, handler HandlerFunc)

Put adds a put router.

func (Kid) Use

func (g Kid) Use(middlewares ...HandlerFunc)

Use adds a middleware.

type Logger added in v0.6.0

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

func NewLogger added in v0.6.0

func NewLogger(module string) *Logger

func (*Logger) Error added in v0.6.0

func (l *Logger) Error(c *Ctx, message string, extra map[string]interface{}, err error)

func (*Logger) FormatMessage added in v0.6.0

func (l *Logger) FormatMessage(
	c *Ctx,
	level LoggerLevel,
	message string,
	extra map[string]interface{},
	err error,
) string

func (*Logger) Info added in v0.6.0

func (l *Logger) Info(c *Ctx, message string, extra map[string]interface{})

type LoggerLevel added in v0.6.0

type LoggerLevel string
const (
	LoggerLevelInfo  LoggerLevel = "info"
	LoggerLevelError LoggerLevel = "error"
)

Directories

Path Synopsis
middlewares

Jump to

Keyboard shortcuts

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