web

package module
v0.62.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 10 Imported by: 69

README

web

Test Go Report Card codecov PkgGoDev Go version License

web 是一个比较完整的 API 开发框架,相对于简单的路由,提供了更多的便利功能。 如果你只是需要一个简单的路由工具,那么你可以移步到 mux

package main

import "github.com/issue9/web"

// main.go
func main() {
    srv := web.NewServer("web", "1.0.0", &web.Options{})
    router := srv.Routers().NewRouter()
    router.Get("/admins", getAdmins).
        Get("/groups", getGroups)

    srv.Serve()
}

func getAdmins(ctx* web.Context) web.Responser {
    return ctx.NotImplemented();
}

func getGroups(ctx* web.Context) web.Responser {
    return ctx.NotImplemented();
}

字符集和文档类型

文档类型由 Server.Mimetypes 指定。 字符类型无需用户指定,https://www.iana.org/assignments/character-sets/character-sets.xhtml 中列出的字符集都能自动转换。

import "github.com/issue9/web"

srv := web.NewServer("app", "1.0.0", &web.Options{})

srv.Mimetypes().Add("application/json", json.Marshal, json.Unmarshal)
srv.Mimetypes().Add("application/xml", xml.Marshal, xml.Unmarshal)

srv.Serve()

客户端只要在请求时设置 Accept 报头就可返回相应类型的数据,而 Accept-Charset 报头可设置接收的字符集。 Content-Type 则可以有向服务器指定提交内容的文档类型和字符集。

错误处理

框架提供了一种输出错误信息内容的机制,用户只需要实现 Problem 接口,即可自定义输出的错误信息格式。 可参考内置的 RFC7807 实现:internal/problems.rfc7807

中间件

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package web 通用的 web 开发框架

Index

Constants

View Source
const (
	// 特殊的值,当不想向用户展示 type 值时,该对象的 type 会被设置为该值。
	ProblemAboutBlank = problems.ProblemAboutBlank

	// 400
	ProblemBadRequest                   = problems.ProblemBadRequest
	ProblemUnauthorized                 = problems.ProblemUnauthorized
	ProblemPaymentRequired              = problems.ProblemPaymentRequired
	ProblemForbidden                    = problems.ProblemForbidden
	ProblemNotFound                     = problems.ProblemNotFound
	ProblemMethodNotAllowed             = problems.ProblemMethodNotAllowed
	ProblemNotAcceptable                = problems.ProblemNotAcceptable
	ProblemProxyAuthRequired            = problems.ProblemProxyAuthRequired
	ProblemRequestTimeout               = problems.ProblemRequestTimeout
	ProblemConflict                     = problems.ProblemConflict
	ProblemGone                         = problems.ProblemGone
	ProblemLengthRequired               = problems.ProblemLengthRequired
	ProblemPreconditionFailed           = problems.ProblemPreconditionFailed
	ProblemRequestEntityTooLarge        = problems.ProblemRequestEntityTooLarge
	ProblemRequestURITooLong            = problems.ProblemRequestURITooLong
	ProblemUnsupportedMediaType         = problems.ProblemUnsupportedMediaType
	ProblemRequestedRangeNotSatisfiable = problems.ProblemRequestedRangeNotSatisfiable
	ProblemExpectationFailed            = problems.ProblemExpectationFailed
	ProblemTeapot                       = problems.ProblemTeapot
	ProblemMisdirectedRequest           = problems.ProblemMisdirectedRequest
	ProblemUnprocessableEntity          = problems.ProblemUnprocessableEntity
	ProblemLocked                       = problems.ProblemLocked
	ProblemFailedDependency             = problems.ProblemFailedDependency
	ProblemTooEarly                     = problems.ProblemTooEarly
	ProblemUpgradeRequired              = problems.ProblemUpgradeRequired
	ProblemPreconditionRequired         = problems.ProblemPreconditionRequired
	ProblemTooManyRequests              = problems.ProblemTooManyRequests
	ProblemRequestHeaderFieldsTooLarge  = problems.ProblemRequestHeaderFieldsTooLarge
	ProblemUnavailableForLegalReasons   = problems.ProblemUnavailableForLegalReasons

	// 500
	ProblemInternalServerError           = problems.ProblemInternalServerError
	ProblemNotImplemented                = problems.ProblemNotImplemented
	ProblemBadGateway                    = problems.ProblemBadGateway
	ProblemServiceUnavailable            = problems.ProblemServiceUnavailable
	ProblemGatewayTimeout                = problems.ProblemGatewayTimeout
	ProblemHTTPVersionNotSupported       = problems.ProblemHTTPVersionNotSupported
	ProblemVariantAlsoNegotiates         = problems.ProblemVariantAlsoNegotiates
	ProblemInsufficientStorage           = problems.ProblemInsufficientStorage
	ProblemLoopDetected                  = problems.ProblemLoopDetected
	ProblemNotExtended                   = problems.ProblemNotExtended
	ProblemNetworkAuthenticationRequired = problems.ProblemNetworkAuthenticationRequired
)

预定义的 Problem id 值

View Source
const (
	ServiceStopped = scheduled.Stopped // 停止状态,默认状态
	ServiceRunning = scheduled.Running // 正在运行
	ServiceFailed  = scheduled.Failed  // 出错,不再执行后续操作
)

服务的几种状态

View Source
const Version = "0.62.0"

Version 当前框架的版本

Variables

This section is empty.

Functions

func NewConfigError added in v0.61.0

func NewConfigError(field string, msg any) *errs.ConfigError

NewConfigError 返回表示配置文件错误的对象

field 表示错误的字段名; msg 表示错误信息,可以是任意类型,如果 msg 是 ConfigError 类型,那么此操作仅修改此类型的 Field 值;

func NewLocaleError added in v0.62.0

func NewLocaleError(format string, v ...any) error

NewLocaleError 本地化的错误信息

func NewStackError added in v0.61.0

func NewStackError(err error) error

NewStackError 为 err 带上调用信息

Types

type CTXSanitizer added in v0.38.0

type CTXSanitizer = server.CTXSanitizer

type ConfigError added in v0.42.0

type ConfigError = errs.ConfigError

type Context

type Context = server.Context

type HandlerFunc added in v0.33.0

type HandlerFunc = server.HandlerFunc

type Job added in v0.33.0

type Job = scheduled.Job

type JobFunc added in v0.33.0

type JobFunc = scheduled.JobFunc

type LocaleStringer added in v0.43.0

type LocaleStringer = localeutil.LocaleStringer

LocaleStringer 本地化字符串需要实在的接口

部分 error 返回可能也实现了该接口。

func Phrase added in v0.42.0

func Phrase(key message.Reference, v ...any) LocaleStringer

Phrase 生成本地化的语言片段

type Logger added in v0.50.2

type Logger = logs.Logger

type Middleware added in v0.33.0

type Middleware = server.Middleware

type MiddlewareFunc added in v0.46.0

type MiddlewareFunc = server.MiddlewareFunc

type Options added in v0.34.0

type Options = server.Options

type QueryUnmarshaler added in v0.58.0

type QueryUnmarshaler = query.Unmarshaler

QueryUnmarshaler 对查询参数的解析接口

type Responser added in v0.40.0

type Responser = server.Responser

func Created added in v0.43.0

func Created(v any, location string) Responser

func NoContent added in v0.43.0

func NoContent() Responser

func OK added in v0.44.0

func OK(v any) Responser

OK 返回 200 状态码下的对象

func Object added in v0.40.0

func Object(status int, body any, kv ...string) Responser

Object 输出状态和对象至客户端

body 表示需要输出的对象,该对象最终会被转换成相应的编码; kv 为报头,必须以偶数数量出现,奇数位为报头名,偶数位为对应的报头值;

func Redirect added in v0.49.0

func Redirect(status int, url string) Responser

Redirect 重定向至新的 URL

func Status added in v0.40.0

func Status(code int, kv ...string) Responser

Status 仅向客户端输出状态码和报头

kv 为报头,必须以偶数数量出现,奇数位为报头名,偶数位为对应的报头值;

NOTE: 即使 code 为 400 等错误代码,当前函数也不会返回 [Problem] 对象。

type ResponserFunc added in v0.56.0

type ResponserFunc = server.ResponserFunc

type Router added in v0.34.0

type Router = server.Router

type Rule added in v0.55.0

type Rule = server.Rule

func NewRule added in v0.53.1

func NewRule(msg LocaleStringer, v Validator) *Rule

NewRule 新建验证规则

func NewRuleFunc added in v0.55.2

func NewRuleFunc(msg LocaleStringer, f func(any) bool) *Rule

NewRuleFunc 新建验证规则

type Scheduler added in v0.27.0

type Scheduler = scheduled.Scheduler

type Server added in v0.25.0

type Server = server.Server

func NewServer added in v0.34.0

func NewServer(name, version string, o *Options) (*Server, error)

type Service added in v0.25.0

type Service = service.Service

type ValidateFunc added in v0.55.0

type ValidateFunc = server.ValidateFunc

type Validation added in v0.55.2

type Validation = server.Validation

type Validator added in v0.34.0

type Validator = server.Validator

Directories

Path Synopsis
Package app 为构建程序提供相对简便的方法
Package app 为构建程序提供相对简便的方法
cmd
web Module
Package errs 与错误相关的定义
Package errs 与错误相关的定义
internal
encoding
Package encoding 处理 Accept-encoding 报头内容
Package encoding 处理 Accept-encoding 报头内容
files
Package files 配置文件管理
Package files 配置文件管理
header
Package header 与报头相关的处理方法
Package header 与报头相关的处理方法
mimetypes
Package mimetype 管理与 Mime type 相关的数据
Package mimetype 管理与 Mime type 相关的数据
problems
Package problems 提供对 Problem 相关内容的管理
Package problems 提供对 Problem 相关内容的管理
service
Package service 服务管理
Package service 服务管理
Package locales 为 web 包提供了本地化的内容
Package locales 为 web 包提供了本地化的内容
Package serializer 序列化的相关操作
Package serializer 序列化的相关操作
form
Package form 用于处理 www-form-urlencoded 编码
Package form 用于处理 www-form-urlencoded 编码
html
Package html 提供输出 HTML 内容的解码函数
Package html 提供输出 HTML 内容的解码函数
json
Package json JSON 格式的序列化方法
Package json JSON 格式的序列化方法
jsonp
Package jsonp JSONP 序列化操作
Package jsonp JSONP 序列化操作
xml
Package xml XML 编码的序列化操作
Package xml XML 编码的序列化操作
Package server 服务管理
Package server 服务管理
servertest
Package servertest 针对 server 的测试用例
Package servertest 针对 server 的测试用例

Jump to

Keyboard shortcuts

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