http

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 23 Imported by: 4

README

http

chefgo http module.

Documentation

Index

Constants

View Source
const (
	NAME = "HTTP"
	WWW  = "www"
)
View Source
const (
	StatusContinue           = 100 // RFC 7231, 6.2.1
	StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
	StatusProcessing         = 102 // RFC 2518, 10.1
	StatusEarlyHints         = 103 // RFC 8297

	StatusOK                   = 200 // RFC 7231, 6.3.1
	StatusCreated              = 201 // RFC 7231, 6.3.2
	StatusAccepted             = 202 // RFC 7231, 6.3.3
	StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4
	StatusNoContent            = 204 // RFC 7231, 6.3.5
	StatusResetContent         = 205 // RFC 7231, 6.3.6
	StatusPartialContent       = 206 // RFC 7233, 4.1
	StatusMultiStatus          = 207 // RFC 4918, 11.1
	StatusAlreadyReported      = 208 // RFC 5842, 7.1
	StatusIMUsed               = 226 // RFC 3229, 10.4.1

	StatusMultipleChoices  = 300 // RFC 7231, 6.4.1
	StatusMovedPermanently = 301 // RFC 7231, 6.4.2
	StatusFound            = 302 // RFC 7231, 6.4.3
	StatusSeeOther         = 303 // RFC 7231, 6.4.4
	StatusNotModified      = 304 // RFC 7232, 4.1
	StatusUseProxy         = 305 // RFC 7231, 6.4.5

	StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7
	StatusPermanentRedirect = 308 // RFC 7538, 3

	StatusBadRequest                   = 400 // RFC 7231, 6.5.1
	StatusUnauthorized                 = 401 // RFC 7235, 3.1
	StatusPaymentRequired              = 402 // RFC 7231, 6.5.2
	StatusForbidden                    = 403 // RFC 7231, 6.5.3
	StatusNotFound                     = 404 // RFC 7231, 6.5.4
	StatusMethodNotAllowed             = 405 // RFC 7231, 6.5.5
	StatusNotAcceptable                = 406 // RFC 7231, 6.5.6
	StatusProxyAuthRequired            = 407 // RFC 7235, 3.2
	StatusRequestTimeout               = 408 // RFC 7231, 6.5.7
	StatusConflict                     = 409 // RFC 7231, 6.5.8
	StatusGone                         = 410 // RFC 7231, 6.5.9
	StatusLengthRequired               = 411 // RFC 7231, 6.5.10
	StatusPreconditionFailed           = 412 // RFC 7232, 4.2
	StatusRequestEntityTooLarge        = 413 // RFC 7231, 6.5.11
	StatusRequestURITooLong            = 414 // RFC 7231, 6.5.12
	StatusUnsupportedMediaType         = 415 // RFC 7231, 6.5.13
	StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4
	StatusExpectationFailed            = 417 // RFC 7231, 6.5.14
	StatusTeapot                       = 418 // RFC 7168, 2.3.3
	StatusMisdirectedRequest           = 421 // RFC 7540, 9.1.2
	StatusUnprocessableEntity          = 422 // RFC 4918, 11.2
	StatusLocked                       = 423 // RFC 4918, 11.3
	StatusFailedDependency             = 424 // RFC 4918, 11.4
	StatusTooEarly                     = 425 // RFC 8470, 5.2.
	StatusUpgradeRequired              = 426 // RFC 7231, 6.5.15
	StatusPreconditionRequired         = 428 // RFC 6585, 3
	StatusTooManyRequests              = 429 // RFC 6585, 4
	StatusRequestHeaderFieldsTooLarge  = 431 // RFC 6585, 5
	StatusUnavailableForLegalReasons   = 451 // RFC 7725, 3

	StatusInternalServerError           = 500 // RFC 7231, 6.6.1
	StatusNotImplemented                = 501 // RFC 7231, 6.6.2
	StatusBadGateway                    = 502 // RFC 7231, 6.6.3
	StatusServiceUnavailable            = 503 // RFC 7231, 6.6.4
	StatusGatewayTimeout                = 504 // RFC 7231, 6.6.5
	StatusHTTPVersionNotSupported       = 505 // RFC 7231, 6.6.6
	StatusVariantAlsoNegotiates         = 506 // RFC 2295, 8.1
	StatusInsufficientStorage           = 507 // RFC 4918, 11.5
	StatusLoopDetected                  = 508 // RFC 5842, 7.2
	StatusNotExtended                   = 510 // RFC 2774, 7
	StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6
)

HTTP status codes as registered with IANA. See: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml

Variables

This section is empty.

Functions

func Configure

func Configure(cfg Map)

Configure 更新配置

func Go

func Go()

Go 直接开跑 此方法只单独使用模块时用

func Ready

func Ready()

Ready 准备运行 此方法只单独使用模块时用

func Register

func Register(name string, value Any, overrides ...bool)

Register 开放给外

func Route

func Route(name string, args ...Map) string

func Routers

func Routers(sites ...string) map[string]Router

func SiteHosts

func SiteHosts(site string) []string

func SiteUrl

func SiteUrl(name string, path string, options ...Map) string

func StatusText

func StatusText(code int) string

StatusText returns a text for the HTTP status code. It returns the empty string if the code is unknown.

Types

type Config

type Config struct {
	// Driver 驱动
	Driver string
	// Port 监听端口
	Port int

	// Host 绑定的IP,默认绑定所有IP
	Host string

	// CertFile SSL证书cert文件
	CertFile string
	// Key SSL证书key文件
	KeyFile string

	// Domain 主域,不是子域名,如 chefgo.org
	Domain string

	// Charset 默认字符集
	// 默认值 utf-8
	Charset string

	// Cookie 默认cookie名称
	// 用于token或sessionid的浏览器cookie名
	// 如果不为空,则表示,token写入cookie
	Cookie string

	// Token 是否自动生成TOKEN
	// 当cookie不为空,且token=true的时候,会自动生成空token
	Token bool
	// Expiry 下发token的有效期,
	// 默认使用token本身的有效期设置
	Expiry time.Duration

	// Crypto 表示cookie是否加密
	Crypto bool
	// MaxAge cookie的超时时间
	// 0  表示不过时
	MaxAge time.Duration
	// HttpOnly COOKIE设置是否httponly
	HttpOnly bool

	// Upload 上传文件临时目录
	// 默认 os.TempDir()
	Upload string
	// Static 静态文件目录
	// 默认 asset/statics
	Static string
	// Shared 默认共享目录
	// 静态文件搜索的共享目录名
	// 默认值 shared
	Shared string

	// Defaults 默认文件名
	// 当访问静态文件时,如果目录的是目录,默认搜索的文件名
	// 默认 index.html default.html
	Defaults []string

	// Validate 请求验证是否开启
	// 开启的话,所有接口请求要验证后才能正常请求
	Validate string
	// Format 请求验证时候的内容格式
	Format string
	// Timeout 请求超时
	Timeout time.Duration

	// Confuse api输出时候内容混淆
	// 使用Codec定义,比如,text, string, rsa, aes 啥的
	// 留空表示不混淆
	Confuse string

	// Setting 设置
	Setting Map
}

type Connect

type Connect interface {
	// Open 打开连接
	Open() error
	// Health 运行状态
	// 返回驱动的运行状态信息,用于监控
	Health() (Health, error)
	//Close 关闭连接
	Close() error

	// Accept 委托
	Accept(Delegate) error
	// Register 注册路由
	Register(name string, info Info) error

	// Start 启动HTTP
	Start() error
	// StartTLS 以TLS的方式启动HTTP
	StartTLS(certFile, keyFile string) error
}

Connect 连接

type Context

type Context struct {
	chef.Meta

	// 以下几个字段必须独立
	// 要不然,Invoke的时候,会被修改掉
	Name    string
	Config  Router
	Setting Map

	Site string

	Method    string
	Host      string
	Domain    string
	Subdomain string
	Path      string
	Uri       string

	Ajax bool

	Params Map
	Query  Map
	Form   Map
	Upload Map

	Value  Map
	Args   Map
	Locals Map

	Code int
	Type string
	Data Map
	Body Any

	Url httpUrl
	// contains filtered or unexported fields
}

func (*Context) Agent

func (ctx *Context) Agent() string

User-Agent

func (*Context) Alert

func (ctx *Context) Alert(res Res, urls ...string)

func (*Context) Answer

func (ctx *Context) Answer(res Res, args ...Any)

Echo 是 Answer的别名

func (*Context) Binary

func (ctx *Context) Binary(bytes []byte, args ...string)

func (*Context) Buffer

func (ctx *Context) Buffer(rd io.ReadCloser, args ...string)

func (*Context) Charset

func (ctx *Context) Charset(charsets ...string) string

func (*Context) Cookie

func (ctx *Context) Cookie(key string, vals ...Any) string

func (*Context) Denied

func (ctx *Context) Denied(res Res)

func (*Context) Echo added in v0.0.4

func (ctx *Context) Echo(res Res, args ...Any)

接口统一输出方法 args表示返回给客户端的data 也会从 ctx.Data 先读取数据,然后使用args中的覆盖

func (*Context) Error

func (ctx *Context) Error(res Res)

func (*Context) Failed

func (ctx *Context) Failed(res Res)

func (*Context) File

func (ctx *Context) File(file string, args ...string)

func (*Context) Found

func (ctx *Context) Found()

func (*Context) Goto

func (ctx *Context) Goto(url string)

func (*Context) HTML

func (ctx *Context) HTML(html Any, args ...Any)

func (*Context) Header

func (ctx *Context) Header(key string, vals ...string) string

func (*Context) Html

func (ctx *Context) Html(html Any, args ...Any)

func (*Context) IP

func (ctx *Context) IP() string

func (*Context) Ip

func (ctx *Context) Ip() string

func (*Context) JSON

func (ctx *Context) JSON(json Any, args ...Any)

func (*Context) JSONP

func (ctx *Context) JSONP(callback string, json Any, args ...Any)

Jsonp

func (*Context) Json

func (ctx *Context) Json(json Any, args ...Any)

func (*Context) Jsonp added in v0.0.4

func (ctx *Context) Jsonp(callback string, json Any, args ...Any)

Jsonp

func (*Context) NewSign added in v0.0.12

func (ctx *Context) NewSign(auth bool, payload Map, expires time.Duration, kinds ...string) string

NewSign 会生成新的ID

func (*Context) Next

func (ctx *Context) Next()

func (*Context) Proxy

func (ctx *Context) Proxy(url string)

func (*Context) Redirect

func (ctx *Context) Redirect(url string)

func (*Context) Route

func (ctx *Context) Route(name string, values ...Map)

func (*Context) Script

func (ctx *Context) Script(script string, args ...Any)

func (*Context) Show

func (ctx *Context) Show(res Res, urls ...string)

展示通用的提示页面

func (*Context) Sign

func (ctx *Context) Sign(auth bool, payload Map, expires time.Duration, kinds ...string) string

Sign 不会生成新的ID

func (*Context) Status

func (ctx *Context) Status(code int, texts ...string)

func (*Context) Text

func (ctx *Context) Text(text Any, args ...Any)

func (*Context) UserAgent

func (ctx *Context) UserAgent() string

func (*Context) View

func (ctx *Context) View(view string, args ...Any)

View Map is Model for view string is type

func (*Context) Xml

func (ctx *Context) Xml(xml Any, args ...Any)
type Cookie = http.Cookie

type Cross

type Cross struct {
	Allow   bool
	Method  string
	Methods []string
	Origin  string
	Origins []string
	Header  string
	Headers []string
}

type Delegate

type Delegate interface {
	// Serve 响应请求
	Serve(name string, params Map, res http.ResponseWriter, req *http.Request)
}

type Driver

type Driver interface {
	Connect(config Config) (Connect, error)
}

Driver 数据驱动

type File

type File struct {

	// Checksum 使用 sha256算法
	Checksum string `json:"checksum"`

	// Filename 是文件的原始文件名
	Filename string `json:"filename"`

	// Extension 扩展名,不包括点(.)
	Extension string `json:"extension"`

	// Mimetype 是文件的MIMEType
	Mimetype string `json:"mimetype"`

	// Length 文件大小,单位:字节
	Length int64 `json:"length"`

	// Tempfile 临时文件路径
	Tempfile string `json:"tempfile"`
}

type Filter

type Filter struct {
	Name     string  `json:"name"`
	Text     string  `json:"text"`
	Serve    ctxFunc `json:"-"`
	Request  ctxFunc `json:"-"`
	Execute  ctxFunc `json:"-"`
	Response ctxFunc `json:"-"`
	// contains filtered or unexported fields
}

Filter 拦截器

type Find

type Find map[string]Item

type Handler

type Handler struct {
	Name   string  `json:"name"`
	Text   string  `json:"text"`
	Found  ctxFunc `json:"-"`
	Error  ctxFunc `json:"-"`
	Failed ctxFunc `json:"-"`
	Denied ctxFunc `json:"-"`
	// contains filtered or unexported fields
}

Handler 处理器

type Health

type Health struct {
	// Workload 当前负载数
	Workload int64
}

type Helper

type Helper struct {
	Name   string   `json:"name"`
	Desc   string   `json:"desc"`
	Alias  []string `json:"alias"`
	Action Any      `json:"-"`
}

type Info

type Info struct {
	// Method 请求的METHOD
	// GET, POST 这些
	Method string
	// Uri 请求的Uri
	Uri string
	// Router 路由器名
	Router string
	// Site 对应的站点
	Site string
	// Hosts 绑定的域名
	Hosts []string
	// contains filtered or unexported fields
}

type Instance

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

func (*Instance) Serve

func (this *Instance) Serve(name string, params Map, res http.ResponseWriter, req *http.Request)

收到消息 待优化,加入协程池,限制单个HTTP的并发

type Item

type Item struct {
	Required bool   `json:"required"`
	Method   string `json:"method"`
	Value    string `json:"value"` //http
	Args     string `json:"args"`  //method
	Name     string `json:"name"`
	Text     string `json:"text"`
	Empty    Res    `json:"-"`
	Error    Res    `json:"-"`
}

type Module

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

func (*Module) Config

func (this *Module) Config(name string, config Config, override bool)

func (*Module) Configure

func (this *Module) Configure(global Map)

func (*Module) Connect

func (this *Module) Connect()

func (*Module) Driver

func (module *Module) Driver(name string, driver Driver, override bool)

Driver 注册驱动

func (*Module) Filter

func (this *Module) Filter(name string, config Filter, override bool)

Filter 注册 拦截器

func (*Module) Handler

func (this *Module) Handler(name string, config Handler, override bool)

Handler 注册 处理器

func (*Module) Helper

func (this *Module) Helper(name string, config Helper, override bool)

func (*Module) Initialize

func (this *Module) Initialize()

func (*Module) Launch

func (this *Module) Launch()

func (*Module) Register

func (this *Module) Register(name string, value Any, override bool)

func (*Module) Router

func (this *Module) Router(name string, config Router, override bool)

Router 注册路由 这里就直接按methods拆分路由,因为要继承 *不在这里处理,因为注册的时候,可能配置文件还没有加载 sitesConifg 有可能还没有准备好。

func (*Module) Routers

func (module *Module) Routers(sites ...string) map[string]Router

func (*Module) Site

func (this *Module) Site(name string, site Site, override bool)

func (*Module) Sites

func (this *Module) Sites(name string, site Sites, override bool)

func (*Module) Terminate

func (this *Module) Terminate()

type RawBody

type RawBody string

type Router

type Router struct {
	Method string

	Uri      string   `json:"uri"`
	Uris     []string `json:"uris"`
	Name     string   `json:"name"`
	Text     string   `json:"text"`
	Nullable bool     `json:"-"`
	Socket   bool     `json:"socket"` //预留,为后面的websocket模块
	Args     Vars     `json:"args"`
	Data     Vars     `json:"data"`
	Setting  Map      `json:"-"`

	Find Find `json:"find"`

	Coding bool `json:"-"`

	Routing Routing   `json:"routing"`
	Actions []ctxFunc `json:"-"`
	Action  ctxFunc   `json:"-"`

	Sign bool   `json:"Sign"`
	Auth bool   `json:"auth"`
	Kind string `json:"kind"`

	// 路由单独可定义的处理器
	Found  ctxFunc `json:"-"`
	Error  ctxFunc `json:"-"`
	Failed ctxFunc `json:"-"`
	Denied ctxFunc `json:"-"`
	// contains filtered or unexported fields
}

type Routing

type Routing map[string]Router

type Site

type Site struct {
	// Name 名称
	Name string

	// Ssl 是否开启SSL
	Ssl bool

	// Domain 主域
	// 为空则从 http.Config 中继承
	Domain string

	// Hosts 绑定的域名列表
	// 如果为空,为自动设置为当前站点的 key + Domain
	// 如果Hosts中带主域名,比如,只设置为 ["aaa", "bbb"]
	// 系统会自动将 aaa,bbb 加上 主域名,如 aaa.chefgo.org
	Hosts []string

	// Charset 字符集
	// 为空则从 http.Config 中继承,默认 utf-8
	Charset string

	// Cookie 默认cookie名称
	// 用于token或sessionid的浏览器cookie名
	Cookie string

	// Token 是否自动生成
	Token bool
	// Expiry 下发token的有效期,
	// 默认使用token本身的有效期设置
	Expiry time.Duration

	// Crypto 表示cookie是否加密
	Crypto bool
	// MaxAge cookie的超时时间
	// 0  表示不过时
	MaxAge time.Duration
	// HttpOnly COOKIE设置是否httponly
	HttpOnly bool

	// Validate 请求验证是否开启
	// 开启的话,所有接口请求要验证后才能正常请求
	Validate string
	// Format 请求验证时候的内容格式
	Format string
	// Timeout 请求超时
	Timeout time.Duration

	// Confuse api输出时候内容混淆
	// 使用Codec定义,比如,text, string, rsa, aes 啥的
	// 留空表示不混淆
	Confuse string

	// Setting 设置
	Setting Map
}

type Sites

type Sites map[string]Site

type TOMLBuffer

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

func (*TOMLBuffer) Close

func (buf *TOMLBuffer) Close() error

func (*TOMLBuffer) Read

func (buf *TOMLBuffer) Read(p []byte) (n int, err error)

func (*TOMLBuffer) Seek

func (buf *TOMLBuffer) Seek(offset int64, whence int) (int64, error)

type TOMLBuilder

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

func NewTOMLBuilder

func NewTOMLBuilder() *TOMLBuilder

func (*TOMLBuilder) Append

func (this *TOMLBuilder) Append(key string, vals ...string)

func (*TOMLBuilder) Build

func (this *TOMLBuilder) Build() string

func (*TOMLBuilder) Files

func (this *TOMLBuilder) Files() map[string][]TOMLFile

func (*TOMLBuilder) Forms

func (this *TOMLBuilder) Forms() map[string][]string

func (*TOMLBuilder) Store

func (this *TOMLBuilder) Store(key, name, mime string, buf io.ReadSeekCloser)

type TOMLFile

type TOMLFile struct {
	Name   string
	MIME   string
	Buffer io.ReadSeekCloser
}

Jump to

Keyboard shortcuts

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