web

package module
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 8 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", "模块描述信息").
        Get("/admins", getAdmins).
        Get("/groups", getGroups), nil
}

// modules/m2/module.go
func Module(s *web.Server) (*web.Module, error) {
    return web.NewModule("m2", "模块描述信息", "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", "测试模块")

    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.Mimetypes 指定。 字符类型无需用户指定,https://www.iana.org/assignments/character-sets/character-sets.xhtml 中列出的字符集都能自动转换。

import "github.com/issue9/web"

srv := web.NewServer(&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 则可以有向服务器指定提交内容的文档类型和字符集。

错误处理

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

版权

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

Documentation

Overview

Package web 一个微型的 RESTful API 框架

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version 当前框架的版本

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

func NewModule

func NewModule(id, desc string, deps ...string) *Module

NewModule 声明一个新的模块

type Options added in v0.34.0

type Options = server.Options

type Server added in v0.25.0

type Server = server.Server

func DefaultServer added in v0.39.0

func DefaultServer(name, version, url 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, c catalog.Catalog, build result.BuildFunc) (*Server, error)

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

func NewServer added in v0.34.0

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

New 返回 *Server 实例

type Tag added in v0.28.0

type Tag = server.Tag

Directories

Path Synopsis
cmd
web command
简单的辅助功能命令行工具
简单的辅助功能命令行工具
Package config 提供了加载配置项内容的各类方法
Package config 提供了加载配置项内容的各类方法
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.NewMimetypes() 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.NewMimetypes() 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) }
mimetypetest
Package mimetypetest 针对文本内容的编解码实现,仅作为测试用例。
Package mimetypetest 针对文本内容的编解码实现,仅作为测试用例。
internal
cmd
Package cmd 命令行相关操作
Package cmd 命令行相关操作
cmd/build
Package build 提供编译相关的功能
Package build 提供编译相关的功能
cmd/release
Package release 发布版本号管理
Package release 发布版本号管理
cmd/version
Package version 显示版本号信息
Package version 显示版本号信息
cmd/watch
Package watch 提供热编译功能 功能与 github.com/caixw/gobuild 相同。
Package watch 提供热编译功能 功能与 github.com/caixw/gobuild 相同。
dep
Package dep 依赖管理
Package dep 依赖管理
filesystem
Package filesystem 提供与文件系统相关的操作
Package filesystem 提供与文件系统相关的操作
plugintest
Package plugintest 作为插件的功能测试包 NOTE: 该测试如果直接写在功能所在的包,目前版本会报错。
Package plugintest 作为插件的功能测试包 NOTE: 该测试如果直接写在功能所在的包,目前版本会报错。
version
Package version 版本定义
Package version 版本定义
versioninfo
Package versioninfo 提供对版本信息的一些操作
Package versioninfo 提供对版本信息的一些操作
Package result 提供对自定义错误代码的支持
Package result 提供对自定义错误代码的支持
Package service 服务管理
Package service 服务管理

Jump to

Keyboard shortcuts

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