web

package module
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2021 License: MIT Imports: 6 Imported by: 75

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.Options{})

    srv.AddModuleFunc(m1.Module, m2.Module) // 注册模块信息

    srv.Serve()
}

// modules/m1/module.go
func Module(s *web.Server) (*web.Module, error) {
    return web.NewModule("m1", "1.0.0", "模块描述信息").
        Get("/admins", getAdmins).
        Get("/groups", getGroups), nil
}

// modules/m2/module.go
func Module(s *web.Server) (*web.Module, error) {
    return web.NewModule("m2", "1.0.0", "模块描述信息", "m1").
        Get("/admins", getAdmins).
        Get("/groups", getGroups), nil
}

模块

在 web 中项目以模块进行划分。每个模块返回一个 *web.Module 实例, 向项目注册自己的模块信息,在项目进行初始化时,会按照模块的依赖关系进行初始化。

用户可以在模块信息中添加当前模块的路由信息、服务、计划任务等, 这些功能在模块初始化时进行统一的注册初始化。

package m1

import "github.com/issue9/web"

func Module(s *web.Server) (*web.Module, error) {
    m := web.NewModule("test", "1.0.0", "测试模块")

    m.AddInit(func() error {
        // TODO 此处可以添加初始化模块的相关代码
        return nil
    }, "初始化函数描述")

    m.AddService(func(ctx context.Context) error {
        // TODO 此处添加服务代码
    }, "服务描述")

    return m
}
插件

web 支持以插件的形式分发模块内容,只需要以 -buildmode=plugin 的形式编译每个模块, 之后将编译后的模块通过 Server.LoadPlugin() 加载即可。具体的可参考下 internal/plugintest 下的插件示例。

Go 并不是在所有的平台下都支持插件模式,支持列表可查看:https://golang.org/pkg/plugin/

字符集和文档类型

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

import "github.com/issue9/web"

srv := web.NewServer(&web.Options{})

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

srv.Serve()

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

错误处理

框架提供了一种输出错误信息内容的机制,用户只需要实现 Result 接口,即可自定义输出的错误信息格式。 具体实现可参考 result.defaultResult 的实现。

版权

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

Documentation

Overview

Package web 一个微型的 RESTful API 框架

Index

Constants

View Source
const Version = "0.40.0"

Version 当前框架的版本

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context = server.Context

func NewContext

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

NewContext 构建 *Context 实例

type Filter added in v0.33.0

type Filter = server.Filter

type HandlerFunc added in v0.33.0

type HandlerFunc = server.HandlerFunc

type Module

type Module = server.Module

type Options added in v0.34.0

type Options = server.Options

type Responser added in v0.40.0

type Responser = server.Responser

func Object added in v0.40.0

func Object(status int, body interface{}, headers map[string]string) Responser

func Status added in v0.40.0

func Status(status int) Responser

type Server added in v0.25.0

type Server = server.Server

func DefaultServer added in v0.39.0

func DefaultServer(name, version string) (*Server, error)

DefaultServer 返回一个采用默认值进初始化的 *Server 实例

func GetServer added in v0.34.0

func GetServer(r *http.Request) *Server

GetServer 从请求中获取 *Server 实例

func LoadServer added in v0.39.0

func LoadServer(name, version string, f fs.FS, build content.BuildResultFunc) (*Server, error)

LoadServer 从配置文件加载并实例化 Server 对象

func NewServer added in v0.34.0

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

NewServer 返回 *Server 实例

Directories

Path Synopsis
cmd
web module
Package config 提供了从配置文件初始化 Server 的方法
Package config 提供了从配置文件初始化 Server 的方法
Package content 与生成内容相关的功能
Package content 与生成内容相关的功能
form
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") ctx.Render(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
gob
Package gob 提供 GOB 格式的编解码
Package gob 提供 GOB 格式的编解码
html
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewContent() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) { ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil) }
Package html 提供输出 HTML 内容的 content.MarshalFunc 函数 mt := content.NewContent() tpl := template.ParseFiles(...) mgr := html.New(tpl) mt.Add("text/html", mgr.Marshal, nil) func handle(ctx *web.Context) { ctx.Render(200, html.Tpl("index", map[string]interface{}{...}), nil) }
text
Package text 针对文本内容的编解码实现
Package text 针对文本内容的编解码实现
text/testobject
Package testobject 用于测试 mimetype 的对象
Package testobject 用于测试 mimetype 的对象
internal
charsetdata
Package charsetdata 用于测试的字符集数据
Package charsetdata 用于测试的字符集数据
filesystem
Package filesystem 提供与文件系统相关的操作
Package filesystem 提供与文件系统相关的操作
plugintest
Package plugintest 作为插件的功能测试包 NOTE: 该测试如果直接写在功能所在的包,目前版本会报错。
Package plugintest 作为插件的功能测试包 NOTE: 该测试如果直接写在功能所在的包,目前版本会报错。
Package server web 服务管理
Package server web 服务管理
Package service 服务管理
Package service 服务管理

Jump to

Keyboard shortcuts

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