hime

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2020 License: MIT Imports: 39 Imported by: 6

README

Hime

codecov Go Report Card GoDoc

Hime is a Go Web Framework.

See Wiki for guide more information.

Why Framework

I ❤️ net/http but... there are many duplicated code when working on multiple projects, plus no standard. Framework creates a standard for developers.

Why Another Framework

There're many Go frameworks out there. But I want a framework that works with any net/http compatible libraries seamlessly.

For example, you can choose any router, any middlewares, or handlers that work with standard library.

That why hime won't ship with any handler include router 🙈

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAppNotFound = errors.New("hime: app not found")
)

Errors

Functions

func Compatible added in v0.10.0

func Compatible() *tls.Config

Compatible is the tls config for compatible mode

func Global added in v0.13.0

func Global(ctx context.Context, key interface{}) interface{}

Global returns global value from context

func Modern added in v0.10.0

func Modern() *tls.Config

Modern is the tls config for modern mode

func Restricted added in v0.10.0

func Restricted() *tls.Config

Restricted is the tls config for restricted mode

func Route added in v0.13.0

func Route(ctx context.Context, name string, params ...interface{}) string

Route returns route value from context

func SafeRedirectPath added in v0.0.17

func SafeRedirectPath(p string) string

SafeRedirectPath filters domain out from path

func StartHTTPSRedirectServer added in v0.10.0

func StartHTTPSRedirectServer(addr string) error

StartHTTPSRedirectServer starts http to https redirect server

Types

type App

type App struct {
	ETag bool
	// contains filtered or unexported fields
}

App is the hime app

func New

func New() *App

New creates new app

func (*App) Address added in v0.5.0

func (app *App) Address(addr string) *App

Address sets server address

func (*App) Clone added in v0.10.0

func (app *App) Clone() *App

Clone clones app

func (*App) Config added in v0.4.0

func (app *App) Config(config AppConfig) *App

Config merges config into app's config

Example:

globals:

data1: test

routes:

index: /
about: /about

templates:

  • dir: view root: layout delims: ["{{", "}}"] minify: true preload:
  • comp/comp1.tmpl
  • comp/comp2.tmpl list: main.tmpl:
  • main.tmpl
  • _layout.tmpl about.tmpl: [about.tmpl, _layout.tmpl]

server:

readTimeout: 10s
readHeaderTimeout: 5s
writeTimeout: 5s
idleTimeout: 30s
gracefulShutdown:
  timeout: 1m
  wait: 5s

func (*App) Global added in v0.0.11

func (app *App) Global(key interface{}) interface{}

Global gets value from global storage

func (*App) Globals added in v0.0.11

func (app *App) Globals(globals Globals) *App

Globals registers global constants

func (*App) GracefulShutdown

func (app *App) GracefulShutdown() *GracefulShutdown

GracefulShutdown changes server to graceful shutdown mode

func (*App) Handler

func (app *App) Handler(h http.Handler) *App

Handler sets the handler

func (*App) ListenAndServe

func (app *App) ListenAndServe() error

ListenAndServe starts web server

func (*App) ParseConfig added in v0.4.0

func (app *App) ParseConfig(data []byte) *App

ParseConfig parses config data

func (*App) ParseConfigFile added in v0.4.0

func (app *App) ParseConfigFile(filename string) *App

ParseConfigFile parses config from file

func (*App) ReusePort added in v0.10.0

func (app *App) ReusePort(enable bool) *App

ReusePort uses SO_REUSEPORT when create listener using app.ListenAndServe

func (*App) Route

func (app *App) Route(name string, params ...interface{}) string

Route gets route path from given name

func (*App) Routes added in v0.0.4

func (app *App) Routes(routes Routes) *App

Routes registers route name and path

func (*App) SelfSign added in v0.10.0

func (app *App) SelfSign(s SelfSign) *App

SelfSign generates self sign cert

func (*App) Serve added in v0.10.0

func (app *App) Serve(l net.Listener) error

Serve serves listener

func (*App) ServeHTTP added in v0.2.0

func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*App) Server added in v0.0.21

func (app *App) Server() *http.Server

Server returns server inside app

func (*App) Shutdown added in v0.10.0

func (app *App) Shutdown(ctx context.Context) error

Shutdown shutdowns server

func (*App) TCPKeepAlive added in v0.10.0

func (app *App) TCPKeepAlive(d time.Duration) *App

TCPKeepAlive sets tcp keep-alive interval when using app.ListenAndServe

func (*App) TLS added in v0.10.0

func (app *App) TLS(certFile, keyFile string) *App

TLS sets cert and key file

func (*App) Template

func (app *App) Template() *Template

Template creates new template loader

func (*App) TemplateFunc added in v0.4.2

func (app *App) TemplateFunc(name string, f interface{}) *App

TemplateFunc registers an app's level template func

func (*App) TemplateFuncs

func (app *App) TemplateFuncs(funcs ...template.FuncMap) *App

TemplateFuncs registers app's level template funcs

type AppConfig added in v0.4.2

type AppConfig struct {
	Globals   Globals          `yaml:"globals" json:"globals"`
	Routes    Routes           `yaml:"routes" json:"routes"`
	Templates []TemplateConfig `yaml:"templates" json:"templates"`
	Server    struct {
		Addr              string            `yaml:"addr" json:"addr"`
		ReadTimeout       string            `yaml:"readTimeout" json:"readTimeout"`
		ReadHeaderTimeout string            `yaml:"readHeaderTimeout" json:"readHeaderTimeout"`
		WriteTimeout      string            `yaml:"writeTimeout" json:"writeTimeout"`
		IdleTimeout       string            `yaml:"idleTimeout" json:"idleTimeout"`
		ReusePort         *bool             `yaml:"reusePort" json:"reusePort"`
		TCPKeepAlive      string            `yaml:"tcpKeepAlive" json:"tcpKeepAlive"`
		GracefulShutdown  *GracefulShutdown `yaml:"gracefulShutdown" json:"gracefulShutdown"`
		TLS               *TLS              `yaml:"tls" json:"tls"`
		HTTPSRedirect     *HTTPSRedirect    `yaml:"httpsRedirect" json:"httpsRedirect"`
	} `yaml:"server" json:"server"`
}

AppConfig is hime app's config

type Apps added in v0.5.0

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

Apps is the collection of App to start together

func Merge added in v0.5.0

func Merge(apps ...*App) *Apps

Merge merges multiple *App into *Apps

func (*Apps) Config added in v0.10.0

func (apps *Apps) Config(config AppsConfig) *Apps

Config merges config into apps config

func (*Apps) GracefulShutdown added in v0.5.0

func (apps *Apps) GracefulShutdown() *GracefulShutdown

GracefulShutdown changes apps to graceful shutdown mode

func (*Apps) ListenAndServe added in v0.5.0

func (apps *Apps) ListenAndServe() error

ListenAndServe starts web servers

func (*Apps) ParseConfig added in v0.10.0

func (apps *Apps) ParseConfig(data []byte) *Apps

ParseConfig parses config data

func (*Apps) ParseConfigFile added in v0.10.0

func (apps *Apps) ParseConfigFile(filename string) *Apps

ParseConfigFile parses config from file

func (*Apps) Shutdown added in v0.10.0

func (apps *Apps) Shutdown(ctx context.Context) error

Shutdown shutdowns all apps

type AppsConfig added in v0.10.0

type AppsConfig struct {
	GracefulShutdown *GracefulShutdown `yaml:"gracefulShutdown" json:"gracefulShutdown"`
	HTTPSRedirect    *HTTPSRedirect    `yaml:"httpsRedirect" json:"httpsRedirect"`
}

AppsConfig is the hime multiple apps config

type Context

type Context struct {
	*http.Request
	// contains filtered or unexported fields
}

Context is hime context

func NewAppContext added in v0.10.0

func NewAppContext(app *App, w http.ResponseWriter, r *http.Request) *Context

NewAppContext creates new hime's context with given app

func NewContext added in v0.0.15

func NewContext(w http.ResponseWriter, r *http.Request) *Context

NewContext creates new hime's context

func (*Context) AddHeader added in v0.10.0

func (ctx *Context) AddHeader(key, value string)

AddHeader adds a header to response

func (*Context) AddHeaderIfNotExists added in v0.10.0

func (ctx *Context) AddHeaderIfNotExists(key, value string)

AddHeaderIfNotExists adds a header to response if not exists

func (*Context) Bytes

func (ctx *Context) Bytes(b []byte) error

Bytes writes bytes into response writer

func (*Context) CopyFrom

func (ctx *Context) CopyFrom(src io.Reader) error

CopyFrom copies src reader into response writer

func (*Context) Deadline added in v0.10.0

func (ctx *Context) Deadline() (deadline time.Time, ok bool)

Deadline implements context.Context

func (*Context) DelHeader added in v0.10.0

func (ctx *Context) DelHeader(key string)

DelHeader deletes a header from response

func (*Context) Done added in v0.10.0

func (ctx *Context) Done() <-chan struct{}

Done implements context.Context

func (*Context) ETag added in v1.0.1

func (ctx *Context) ETag(enable bool) *Context

ETag overrides etag setting

func (*Context) Err added in v0.10.0

func (ctx *Context) Err() error

Err implements context.Context

func (*Context) Error

func (ctx *Context) Error(error string) error

Error calls http.Error

func (*Context) File added in v0.0.7

func (ctx *Context) File(name string) error

File serves file using http.ServeFile

func (*Context) FormFileHeader added in v0.10.0

func (ctx *Context) FormFileHeader(key string) (*multipart.FileHeader, error)

FormFileHeader returns file header for given key without open file

func (*Context) FormFileHeaderNotEmpty added in v0.10.0

func (ctx *Context) FormFileHeaderNotEmpty(key string) (*multipart.FileHeader, error)

FormFileHeaderNotEmpty returns file header if not empty, or http.ErrMissingFile if file is empty

This function will be deprecated after drop go1.10 support, since go1.11 bring back old behavior

func (*Context) FormFileNotEmpty added in v0.0.18

func (ctx *Context) FormFileNotEmpty(key string) (multipart.File, *multipart.FileHeader, error)

FormFileNotEmpty returns file from r.FormFile only when file size is not empty, or return http.ErrMissingFile if file is empty

func (*Context) FormValueFloat32 added in v0.0.17

func (ctx *Context) FormValueFloat32(key string) float32

FormValueFloat32 converts form value to float32

func (*Context) FormValueFloat64 added in v0.0.17

func (ctx *Context) FormValueFloat64(key string) float64

FormValueFloat64 converts form value to float64

func (*Context) FormValueInt added in v0.0.17

func (ctx *Context) FormValueInt(key string) int

FormValueInt converts form value to int

func (*Context) FormValueInt64 added in v0.0.17

func (ctx *Context) FormValueInt64(key string) int64

FormValueInt64 converts form value to int64

func (*Context) FormValueTrimSpace added in v0.0.17

func (ctx *Context) FormValueTrimSpace(key string) string

FormValueTrimSpace trims space from form value

func (*Context) FormValueTrimSpaceComma added in v0.0.17

func (ctx *Context) FormValueTrimSpaceComma(key string) string

FormValueTrimSpaceComma trims space and remove comma from form value

func (*Context) Global added in v0.0.11

func (ctx *Context) Global(key interface{}) interface{}

Global returns global value

func (*Context) HTML added in v0.10.0

func (ctx *Context) HTML(data string) error

HTML writes html to response writer

func (*Context) Handle

func (ctx *Context) Handle(h http.Handler) error

Handle calls h.ServeHTTP

func (*Context) JSON

func (ctx *Context) JSON(data interface{}) error

JSON encodes given data into json then writes to response writer

func (*Context) NoContent added in v0.0.8

func (ctx *Context) NoContent() error

NoContent writes http.StatusNoContent into response writer

func (*Context) NotFound added in v0.0.7

func (ctx *Context) NotFound() error

NotFound calls http.NotFound

func (*Context) Param added in v0.0.16

func (ctx *Context) Param(name string, value interface{}) *Param

Param is the short-hand for hime.Param

func (*Context) PostFormValueFloat32 added in v0.0.17

func (ctx *Context) PostFormValueFloat32(key string) float32

PostFormValueFloat32 converts post form value to flost32

func (*Context) PostFormValueFloat64 added in v0.0.17

func (ctx *Context) PostFormValueFloat64(key string) float64

PostFormValueFloat64 converts post form value to flost64

func (*Context) PostFormValueInt added in v0.0.17

func (ctx *Context) PostFormValueInt(key string) int

PostFormValueInt converts post form value to int

func (*Context) PostFormValueInt64 added in v0.0.17

func (ctx *Context) PostFormValueInt64(key string) int64

PostFormValueInt64 converts post form value to int64

func (*Context) PostFormValueTrimSpace added in v0.0.17

func (ctx *Context) PostFormValueTrimSpace(key string) string

PostFormValueTrimSpace trims space from post form value

func (*Context) PostFormValueTrimSpaceComma added in v0.0.17

func (ctx *Context) PostFormValueTrimSpaceComma(key string) string

PostFormValueTrimSpaceComma trims space and remove comma from post form value

func (*Context) Redirect

func (ctx *Context) Redirect(url string, params ...interface{}) error

Redirect redircets to given url

func (*Context) RedirectBack added in v0.0.25

func (ctx *Context) RedirectBack(fallback string) error

RedirectBack redirects to referer or fallback if referer not exists

func (*Context) RedirectBackToGet added in v0.0.27

func (ctx *Context) RedirectBackToGet() error

RedirectBackToGet redirects to referer or fallback with same url

func (*Context) RedirectTo

func (ctx *Context) RedirectTo(name string, params ...interface{}) error

RedirectTo redirects to route name

func (*Context) RedirectToGet added in v0.0.14

func (ctx *Context) RedirectToGet() error

RedirectToGet redirects to same url back to Get

func (*Context) ResponseWriter

func (ctx *Context) ResponseWriter() http.ResponseWriter

ResponseWriter returns response writer

func (*Context) Route added in v0.0.11

func (ctx *Context) Route(name string, params ...interface{}) string

Route gets route path from name

func (*Context) SafeRedirect

func (ctx *Context) SafeRedirect(url string, params ...interface{}) error

SafeRedirect extracts only path from url then redirect

func (*Context) SafeRedirectBack added in v0.0.25

func (ctx *Context) SafeRedirectBack(fallback string) error

SafeRedirectBack safe redirects to referer

func (*Context) SetHeader added in v0.10.0

func (ctx *Context) SetHeader(key, value string)

SetHeader sets a header to response

func (*Context) Status

func (ctx *Context) Status(code int) *Context

Status sets response status code

func (*Context) StatusText added in v0.0.8

func (ctx *Context) StatusText() error

StatusText writes status text from seted status code tnto response writer

func (*Context) String

func (ctx *Context) String(format string, a ...interface{}) error

String writes string into response writer

func (*Context) Value added in v0.10.0

func (ctx *Context) Value(key interface{}) interface{}

Value implements context.Context

func (*Context) View

func (ctx *Context) View(name string, data interface{}) error

View renders view

func (*Context) WithContext added in v0.0.24

func (ctx *Context) WithContext(nctx context.Context) *Context

WithContext returns new context with new request with given context

func (Context) WithRequest added in v0.0.26

func (ctx Context) WithRequest(r *http.Request) *Context

WithRequest returns new context with given request

func (Context) WithResponseWriter added in v0.0.26

func (ctx Context) WithResponseWriter(w http.ResponseWriter) *Context

WithResponseWriter returns new context with given response writer

func (*Context) WithValue added in v0.0.26

func (ctx *Context) WithValue(key interface{}, val interface{}) *Context

WithValue calls WithContext with value context

type ErrRouteNotFound added in v0.0.15

type ErrRouteNotFound struct {
	Route string
}

ErrRouteNotFound is the error for route not found

func (*ErrRouteNotFound) Error added in v0.0.15

func (err *ErrRouteNotFound) Error() string

type ErrTemplateDuplicate added in v0.0.15

type ErrTemplateDuplicate struct {
	Name string
}

ErrTemplateDuplicate is the error for template duplicate

func (*ErrTemplateDuplicate) Error added in v0.0.15

func (err *ErrTemplateDuplicate) Error() string

type ErrTemplateNotFound added in v0.0.15

type ErrTemplateNotFound struct {
	Name string
}

ErrTemplateNotFound is the error for template not found

func (*ErrTemplateNotFound) Error added in v0.0.15

func (err *ErrTemplateNotFound) Error() string

type Globals added in v0.0.11

type Globals map[interface{}]interface{}

Globals is the global const map

type GracefulShutdown added in v0.2.0

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

GracefulShutdown is the graceful shutdown configure

func (*GracefulShutdown) Notify added in v0.2.0

func (gs *GracefulShutdown) Notify(fn func()) *GracefulShutdown

Notify calls fn when receive terminate signal from os

func (*GracefulShutdown) Timeout added in v0.2.0

Timeout sets shutdown timeout for graceful shutdown, set to 0 to disable timeout

default is 0

func (*GracefulShutdown) UnmarshalJSON added in v0.10.0

func (gs *GracefulShutdown) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler

func (*GracefulShutdown) UnmarshalYAML added in v0.10.0

func (gs *GracefulShutdown) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements yaml.Unmarshaler

func (*GracefulShutdown) Wait added in v0.2.0

Wait sets wait time before shutdown

type HTTPSRedirect added in v0.10.0

type HTTPSRedirect struct {
	Addr string `json:"addr"`
}

HTTPSRedirect type

func (*HTTPSRedirect) ServeHTTP added in v0.10.0

func (s *HTTPSRedirect) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (HTTPSRedirect) Server added in v0.10.0

func (s HTTPSRedirect) Server() *http.Server

Server generates https redirect server

type Handler

type Handler func(*Context) error

Handler is the hime handler

func (Handler) ServeHTTP added in v0.10.0

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Param added in v0.0.16

type Param struct {
	Name  string
	Value interface{}
}

Param is the query param when redirect

type Routes added in v0.0.4

type Routes map[string]string

Routes is the map for route name => path

type SelfSign added in v0.10.0

type SelfSign struct {
	Key struct {
		Algo string `yaml:"algo" json:"algo"`
		Size int    `yaml:"size" json:"size"`
	} `yaml:"key" json:"key"`
	CN    string   `yaml:"cn" json:"cn"`
	Hosts []string `yaml:"host" json:"host"`
}

SelfSign type

type TLS added in v0.10.0

type TLS struct {
	SelfSign   *SelfSign `yaml:"selfSign" json:"selfSign"`
	CertFile   string    `yaml:"certFile" json:"certFile"`
	KeyFile    string    `yaml:"keyFile" json:"keyFile"`
	Profile    string    `yaml:"profile" json:"profile"`
	MinVersion string    `yaml:"minVersion" json:"minVersion"`
	MaxVersion string    `yaml:"maxVersion" json:"maxVersion"`
	Curves     []string  `yaml:"curves" json:"curves"`
}

TLS type

type Template added in v0.3.0

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

Template is template loader

func (*Template) Component added in v0.3.0

func (tp *Template) Component(ts ...*template.Template) *Template

Component loads html/template

func (*Template) Config added in v0.4.2

func (tp *Template) Config(cfg TemplateConfig) *Template

Config loads template config

func (*Template) Delims added in v0.3.0

func (tp *Template) Delims(left, right string) *Template

Delims sets left and right delims

func (*Template) Dir added in v0.3.0

func (tp *Template) Dir(path string) *Template

Dir sets root directory when load template

default is ""

func (*Template) Func added in v0.4.2

func (tp *Template) Func(name string, f interface{}) *Template

Func adds a template func while load template

func (*Template) Funcs added in v0.3.0

func (tp *Template) Funcs(funcs ...template.FuncMap) *Template

Funcs adds template funcs while load template

func (*Template) Minify added in v0.3.0

func (tp *Template) Minify() *Template

Minify enables minify when render html, css, js

func (*Template) MinifyWith added in v1.0.1

func (tp *Template) MinifyWith(cfg TemplateMinifyConfig) *Template

MinifyWith enables minify with custom options

func (*Template) Parse added in v0.3.0

func (tp *Template) Parse(name string, text string) *Template

Parse parses template from text

func (*Template) ParseConfig added in v0.4.2

func (tp *Template) ParseConfig(data []byte) *Template

ParseConfig parses template config data

func (*Template) ParseConfigFile added in v0.4.2

func (tp *Template) ParseConfigFile(filename string) *Template

ParseConfigFile parses template config from file

func (*Template) ParseFiles added in v0.10.0

func (tp *Template) ParseFiles(name string, filenames ...string) *Template

ParseFiles loads template from file

func (*Template) ParseGlob added in v0.10.0

func (tp *Template) ParseGlob(name string, pattern string) *Template

ParseGlob loads template from pattern

func (*Template) Preload added in v0.10.0

func (tp *Template) Preload(filename ...string) *Template

Preload loads given templates before every templates

func (*Template) Root added in v0.3.0

func (tp *Template) Root(name string) *Template

Root calls t.Lookup(name) after load template, empty string won't trigger t.Lookup

default is ""

type TemplateConfig added in v0.4.2

type TemplateConfig struct {
	Dir     string              `yaml:"dir" json:"dir"`
	Root    string              `yaml:"root" json:"root"`
	Minify  bool                `yaml:"minify" json:"minify"`
	Preload []string            `yaml:"preload" json:"preload"`
	List    map[string][]string `yaml:"list" json:"list"`
	Delims  []string            `yaml:"delims" json:"delims"`
}

TemplateConfig is template config

type TemplateMinifyConfig added in v1.0.1

type TemplateMinifyConfig struct {
	HTML minify.Minifier
	CSS  minify.Minifier
	JS   minify.Minifier
}

Jump to

Keyboard shortcuts

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