brick

package module
v0.0.0-...-4886e35 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 20 Imported by: 0

README

Brick

A web application development framework with basic functions

Basic use

// HTTP post 7077
b := brick.NewBrick(port 7077, session-time)

// Redirect '/' to "/brick/ui"
b.HttpJumpMapping("/", "/brick/ui")

// static page service
b.StaticPage("/brick/ui", "www")

// start http server
b.StartHttpServer();

// http service
b.Service("/url/", func(h brick.Http) {})

// Template with HTML
b.Service("/url/", b.TemplatePage("www/index.xhtml", 
  func(h brick.Http) (interface{}, error) { return nil, nil })

Template

A.xhtml file:

<div>A File {{ .Data }}</div>
{{ include . "B.xhtml" }}

B.xhtml file:

<div>B File</div>

build static resource

Package static resources as go source code.

node build

Static files in the compilation directory are go resource bundles Read build.json in the current directory as the build configuration run: execute the script without parameters nodejs > v6

The generated go code sets static resources into variables by accessing fm := brick.GetFileMapping().

Configuration instructions:

buiod.json file:

{
  "packageName": "brick",
  "fileName": "resource_www.go",
  "wwwDir": "../www",
  "outDir": "./resource",
  "varName": "fm"
}

Traverse the files in the wwwDir directory, save the file content to the varName variable, filename is variable index; output to GO source file at outDir/fileName, The package name is packageName; the varName variable is usually defined in other source files of the package, variable type is map[string][]byte.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFileMapping

func GetFileMapping() map[string][]byte

func LastSlice

func LastSlice(str string, maxLen int, prefix string) string

取 str 字符串末尾 maxLen 指定的长度, 如果 str 长度小于 maxLen 则返回 str 切末尾补充空格 如果发生截断, 则前面加 prefix 符号

Types

type Brick

type Brick struct {
	HttpPort int

	Debug bool
	// contains filtered or unexported fields
}

方便编写 http 服务

func NewBrick

func NewBrick(httpPort int, sessionExp time.Duration) *Brick

创建 Brick 的实例, session 对象在 sessionExp 后无效.

func (*Brick) GetCachedTemplate

func (b *Brick) GetCachedTemplate(fileName string) (*CachedTemplate, error)

编译并返回 html 模板对象, 如果模板文件有变更, 会重新编译 TODO: 支持模板之间的 define/template 调用

func (*Brick) HttpJumpMapping

func (b *Brick) HttpJumpMapping(location string, to string)

把对 location 的请求跳转到 to 上, 如果参数 location == '/', 则对没有注册过的路径的请求都会转发到 to 上.

func (*Brick) Service

func (b *Brick) Service(path string, h HttpHandler)

普通 web 服务

func (*Brick) SetErrorHandler

func (b *Brick) SetErrorHandler(p HttpErrorHandler)

func (*Brick) SetLogger

func (b *Brick) SetLogger(log Logger)

设置 brick 打印日志的目标对象

func (*Brick) SetTemplateDir

func (b *Brick) SetTemplateDir(path string)

设置 html 模板文件加载目录

func (*Brick) StartHttpServer

func (b *Brick) StartHttpServer() error

启动服务, 该方法会阻塞

func (*Brick) StaticPage

func (b *Brick) StaticPage(baseURL string, fileDir string)

设置静态文件服务, 必须在该方法之前设置 log 否则无效

func (*Brick) TemplatePage

func (b *Brick) TemplatePage(
	templateFile string, handle TemplateHandler) HttpHandler

创建模板服务 handle 返回的上下文对象中的数据绑定到 template_file 指定的模板中, 服务映射到 url 路径上. 如果使 HTTP HEAD 请求, 模板不会渲染, 如果没有错误则返回 204

type CachedTemplate

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

已经缓存的模板对象

type Http

type Http struct {
	R *http.Request
	W http.ResponseWriter

	// 在记录 http 日志时的附加条目
	L string
	// contains filtered or unexported fields
}

func (*Http) CacheTime

func (h *Http) CacheTime(d time.Duration)

设置 http 头域中的缓存时间字段, 应该在写出任何内容之前设置

func (*Http) CloseOnEnd

func (h *Http) CloseOnEnd(c Shutdown)

当 http 响应结束, 所有注册的 Shutdown 接口都被调用

func (*Http) Ctx

func (h *Http) Ctx() context.Context

返回 http 请求上下文

func (*Http) Get

func (h *Http) Get(name string) string

返回 URI 中的参数, 参数为空返回空字符串

func (*Http) GetAcceptLanguage

func (h *Http) GetAcceptLanguage() string

只返回首选 AcceptLanguage

func (*Http) GetTpl

func (h *Http) GetTpl(filename string) (*template.Template, error)

Brick.GetCachedTemplate() 的简写

func (*Http) Gets

func (h *Http) Gets(name string) []string

func (*Http) Json

func (h *Http) Json(m interface{})

返回 json 字符串

func (*Http) Session

func (h *Http) Session() *sessions.Session

启用并返回事务对象

func (*Http) Tag

func (h *Http) Tag(tagName string, body func(), attr ...string)

输出 HTML 标签, 属性长度必须是偶数

func (*Http) TextTag

func (h *Http) TextTag(tagName string, text string, attr ...string)

输出纯文本 HTML 标签

func (*Http) URLParam

func (h *Http) URLParam(fixBase string, out ...*string) int

解析 url, 从 fixBase 片段开始并把 url 中的路径片段绑定到 out 的输出参数 ULR 绑定规则示意: "/someother../fixBase/out1/out2/*" 如果输入 url 片段数量多于 out 的数量, 后面的 url 片段被丢弃, 返回丢弃的数量 如果输入 url 片段数量少于 out 的数量, 后面的 out 不改变原始值, 返回多余的 out 数量的负值 返回值 == 0 说明 url 参数和 out 数量匹配 如果在 url 中找不到 fixBase 路径片段则发生异常

func (*Http) WriteCSS

func (h *Http) WriteCSS(href string)

写一个 css 引用标签

func (*Http) WriteErr

func (h *Http) WriteErr(e error)

输出错误字符串, 该方法不影响程序流程

func (*Http) WriteStr

func (h *Http) WriteStr(s string)

type HttpErrorHandler

type HttpErrorHandler func(hd *Http, err interface{})

当发生 http 异常或 HttpHandler 返回错误, 对错误执行这个方法 通常记录日志并向客户端输出错误信息

type HttpHandler

type HttpHandler func(*Http) error

http 服务处理函数, 在可能返回 error 之前不要写出任何数据 返回的 error 会设置输出为 500 http code

type Logger

type Logger interface {
	Debug(v ...interface{})
	Info(v ...interface{})
	Warn(v ...interface{})
	Error(v ...interface{})
	Fmt(format string, v ...interface{})
}

type Msg

type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type Shutdown

type Shutdown interface {
	Close()
}

type StaticPage

type StaticPage struct {
	BaseUrl  string // web 服务的路径前缀
	FilePath string // 本地文件路径
	// contains filtered or unexported fields
}

func (*StaticPage) ServeHTTP

func (p *StaticPage) ServeHTTP(w http.ResponseWriter, r *http.Request)

type TemplateHandler

type TemplateHandler func(*Http) (interface{}, error)

html 模板处理函数, 该函数准备渲染模板需要的数据, 并在第一个参数返回 如果出错, 返回第二个参数, 此时错误会输出到客户端, 并终止模板渲染 HEAD 请求不会渲染模板.

type TplFuncCtx

type TplFuncCtx struct {
	io.Writer
	Data    *interface{}
	Dirname string
	// contains filtered or unexported fields
}

HTML 模板上下文, 即模板中 '.' 符号表示的实例, '.Data' 是 TemplateHandler 函数返回的数据.

Jump to

Keyboard shortcuts

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