modulepackage
0.0.0-20220325183229-6c8eee4ff8a5
Repository: https://github.com/nbvghost/gweb.git
Documentation: pkg.go.dev
# README
gweb
golang web 轻框架
最轻量的包装,灵活极易扩展,采用流行的MVC框架思想。
支持特性:
- 子路径独立拦截器和控制器
- 添加子控制器
- 支持RESTful,OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
- 支持 {id}/path 路径映射
- 内置返回类型有:ViewResult,HTMLResult,JsonResult,TextResult,RedirectToUrlResult,ImageResult,ImageBytesResult
- 内置模板函数,除了golang的函数,还增加了IncludeHTML,Split,FromJSONToMap,FromJSONToArray,CipherDecrypter,CipherEncrypter,Int2String,Uint2String,Float2String,ToJSON
安装:
go get github.com/nbvghost/gweb
配制文件说明:
默认配制文件信息: {"ViewDir":"view","ResourcesDir":"resources","DefaultPage":"index","JsonDataPath":"data.json","HttpPort":":80","HttpsPort":":443","ViewSuffix":".html","ViewActionMapping":true,"TLSCertFile":"","TLSKeyFile":""}]
- ViewDir:视图文件夹名
- ResourcesDir:资源文件夹名
- DefaultPage:默认文件名
- JsonDataPath:json 数据文件,这个文件在所有视图文件中可以读取到,可用于程序业务配制信息一块。
- HttpPort:http 端口
- HttpsPort:https 端口
- ViewSuffix:视图文件后缀
- TLSCertFile,TLSKeyFile: https 证书文件
使用例子:
package main
import (
"github.com/nbvghost/gweb"
"github.com/nbvghost/gweb/conf"
"net/http"
"net/url"
"time"
)
//拦截器
type InterceptorManager struct {
}
//拦截器 方法,如果 允许登陆 返回true
func (this InterceptorManager) Execute(context *gweb.Context) (bool, gweb.Result) {
if context.Session.Attributes.Get("admin") == nil { //判断当前 session 信息
redirect := "" // 跳转地址
if len(context.Request.URL.Query().Encode()) == 0 {
redirect = context.Request.URL.Path
} else {
redirect = context.Request.URL.Path + "?" + context.Request.URL.Query().Encode()
}
//http.Redirect(Response, Request, "/account/login?redirect="+url.QueryEscape(redirect), http.StatusFound)
return false, &gweb.RedirectToUrlResult{Url: "/account/login?redirect=" + url.QueryEscape(redirect)}
} else {
return true, nil
}
}
type User struct {
Name string
Age int
}
//index路由控制器
type IndexController struct {
gweb.BaseController
}
func (c *IndexController) Apply() {
c.Interceptors.Add(&InterceptorManager{}) //拦截器
// 添加index地址映射
//访问路径示例:http://127.0.0.1:80/index
c.AddHandler(gweb.ALLMethod("index", func(context *gweb.Context) gweb.Result {
return &gweb.HTMLResult{}
}))
//访问子路径示例:http://127.0.0.1:80/wx/
wx := &WxController{}
wx.Interceptors = c.Interceptors //使用 父级 拦截器
c.AddSubController("/wx/", wx) // 添加子控制器,相关的路由定义在 WxController.Apply() 里
}
//account路由控制器
type AccountController struct {
gweb.BaseController
}
func (c *AccountController) Apply() {
//访问路径示例:http://127.0.0.1:80/account/login
c.AddHandler(gweb.GETMethod("login", func(context *gweb.Context) gweb.Result {
user := &User{Name: "user name", Age: 12}
context.Session.Attributes.Put("admin", user)
redirect := context.Request.URL.Query().Get("redirect")
return &gweb.RedirectToUrlResult{Url: redirect}
}))
}
// /wx 路由控制器
type WxController struct {
gweb.BaseController
}
func (c *WxController) Apply() {
//访问路径示例:http://127.0.0.1:80/wx/44545/path
c.AddHandler(gweb.GETMethod("{id}/path", func(context *gweb.Context) gweb.Result {
user := context.Session.Attributes.Get("admin").(*User)
return &gweb.HTMLResult{Name: "wx/path", Params: map[string]interface{}{"User": user, "Id": context.PathParams}}
}))
//访问路径示例:http://127.0.0.1:80/wx/info
c.AddHandler(gweb.GETMethod("info", func(context *gweb.Context) gweb.Result {
user := context.Session.Attributes.Get("admin").(*User)
return &gweb.HTMLResult{Params: map[string]interface{}{"User": user}}
}))
}
func main() {
//初始化控制器,拦截 / 路径
index := &IndexController{}
index.NewController("/", index)
//初始化控制器,拦截 /account 路径
account := &AccountController{}
account.NewController("/account", account)
httpServer := &http.Server{
Addr: conf.Config.HttpPort,
Handler: http.DefaultServeMux,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
//MaxHeaderBytes: 1 << 20,
}
//启动web服务器
gweb.StartServer(http.DefaultServeMux,httpServer, nil)
//也可用,内置函数,gweb只是简单的做一个封装的
//err := http.ListenAndServe(conf.Config.HttpPort, nil)
//log.Println(err)
}
具体代码请查看demo目录:https://github.com/nbvghost/gweb/tree/master/demo/gwebtest
也可以加我QQ:274455411
推荐使用jetbrains goland开发
# Functions
No description provided by the author
No description provided by the author
func (static Static) FileLoad(ctx *Context) Result {
path := ctx.Request.URL.Query().Get("path")
urldd, err := url.Parse(path)
if glog.Error(err) || (strings.EqualFold(urldd.Scheme, "") && strings.EqualFold(urldd.Host, "")) {
//dir, _ := filepath.Split(path)
http.ServeFile(ctx.Response, ctx.Request, strings.Trim(conf.Config.UploadDir, "/")+"/"+strings.Trim(conf.Config.UploadDirName, "/")+"/"+path)
return &EmptyResult{}
}
return &RedirectToUrlResult{Url: path}
}*/func (static Static) FileTempLoad(ctx *Context) Result {
path := ctx.Request.URL.Query().Get("path")
http.ServeFile(ctx.Response, ctx.Request, "temp/"+path)
return &EmptyResult{}
}
*/.
No description provided by the author
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Mobile Safari/537.36Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1.
No description provided by the author
No description provided by the author
NewController 根目录控制器,非具体的handler.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
func RequestByHeader(url string, UserAgent string, Referer string) ([]byte,error) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil,err
}
//req.Header.Add("User-Agent","Mozilla/5.0 (Linux; Android 7.0; SLA-AL00 Build/HUAWEISLA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN")
if !strings.EqualFold(UserAgent, "") {
req.Header.Add("User-Agent", UserAgent)
}
if !strings.EqualFold(Referer, "") {
req.Header.Add("Referer", Referer)
}
resp, err := client.Do(req)
if err != nil {
return nil,err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
return b,err
}*/.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
HTMLResult 只映射已经定义的后缀模板文件.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
*
类型/子类型 扩展名
application/json json
application/envoy evy
application/fractals fif
application/futuresplash spl
application/hta hta
application/internet-property-stream acx
application/mac-binhex40 hqx
application/msword doc
application/msword dot
application/octet-stream *
application/octet-stream bin
application/octet-stream class
application/octet-stream dms
application/octet-stream exe
application/octet-stream lha
application/octet-stream lzh
application/oda oda
application/olescript axs
application/pdf pdf
application/pics-rules prf
application/pkcs10 p10
application/pkix-crl crl
application/postscript ai
application/postscript eps
application/postscript ps
application/rtf rtf
application/set-payment-initiation setpay
application/set-registration-initiation setreg
application/vnd.ms-excel xla
application/vnd.ms-excel xlc
application/vnd.ms-excel xlm
application/vnd.ms-excel xls
application/vnd.ms-excel xlt
application/vnd.ms-excel xlw
application/vnd.ms-outlook msg
application/vnd.ms-pkicertstore sst
application/vnd.ms-pkiseccat cat
application/vnd.ms-pkistl stl
application/vnd.ms-powerpoint pot
application/vnd.ms-powerpoint pps
application/vnd.ms-powerpoint ppt
application/vnd.ms-project mpp
application/vnd.ms-works wcm
application/vnd.ms-works wdb
application/vnd.ms-works wks
application/vnd.ms-works wps
application/winhlp hlp
application/x-bcpio bcpio
application/x-cdf cdf
application/x-compress z
application/x-compressed tgz
application/x-cpio cpio
application/x-csh csh
application/x-director dcr
application/x-director dir
application/x-director dxr
application/x-dvi dvi
application/x-gtar gtar
application/x-gzip gz
application/x-hdf hdf
application/x-internet-signup ins
application/x-internet-signup isp
application/x-iphone iii
application/x-javascript js
application/x-latex latex
application/x-msaccess mdb
application/x-mscardfile crd
application/x-msclip clp
application/x-msdownload dll
application/x-msmediaview m13
application/x-msmediaview m14
application/x-msmediaview mvb
application/x-msmetafile wmf
application/x-msmoney mny
application/x-mspublisher pub
application/x-msschedule scd
application/x-msterminal trm
application/x-mswrite wri
application/x-netcdf cdf
application/x-netcdf nc
application/x-perfmon pma
application/x-perfmon pmc
application/x-perfmon pml
application/x-perfmon pmr
application/x-perfmon pmw
application/x-pkcs12 p12
application/x-pkcs12 pfx
application/x-pkcs7-certificates p7b
application/x-pkcs7-certificates spc
application/x-pkcs7-certreqresp p7r
application/x-pkcs7-mime p7c
application/x-pkcs7-mime p7m
application/x-pkcs7-signature p7s
application/x-sh sh
application/x-shar shar
application/x-shockwave-flash swf
application/x-stuffit sit
application/x-sv4cpio sv4cpio
application/x-sv4crc sv4crc
application/x-tar tar
application/x-tcl tcl
application/x-tex tex
application/x-texinfo texi
application/x-texinfo texinfo
application/x-troff roff
application/x-troff t
application/x-troff tr
application/x-troff-man man
application/x-troff-me me
application/x-troff-ms ms
application/x-ustar ustar
application/x-wais-source src
application/x-x509-ca-cert cer
application/x-x509-ca-cert crt
application/x-x509-ca-cert der
application/ynd.ms-pkipko pko
application/zip zip
audio/basic au
audio/basic snd
audio/mid mid
audio/mid rmi
audio/mpeg mp3
audio/x-aiff aif
audio/x-aiff aifc
audio/x-aiff aiff
audio/x-mpegurl m3u
audio/x-pn-realaudio ra
audio/x-pn-realaudio ram
audio/x-wav wav
image/bmp bmp
image/cis-cod cod
image/gif gif
image/ief ief
image/jpeg jpe
image/jpeg jpeg
image/jpeg jpg
image/pipeg jfif
image/svg+xml svg
image/tiff tif
image/tiff tiff
image/x-cmu-raster ras
image/x-cmx cmx
image/x-icon ico
image/x-portable-anymap pnm
image/x-portable-bitmap pbm
image/x-portable-graymap pgm
image/x-portable-pixmap ppm
image/x-rgb rgb
image/x-xbitmap xbm
image/x-xpixmap xpm
image/x-xwindowdump xwd
message/rfc822 mht
message/rfc822 mhtml
message/rfc822 nws
text/css css
text/h323 323
text/html htm
text/html html
text/html stm
text/iuls uls
text/plain bas
text/plain c
text/plain h
text/plain txt
text/richtext rtx
text/scriptlet sct
text/tab-separated-values tsv
text/webviewhtml htt
text/x-component htc
text/x-setext etx
text/x-vcard vcf
video/mpeg mp2
video/mpeg mpa
video/mpeg mpe
video/mpeg mpeg
video/mpeg mpg
video/mpeg mpv2
video/quicktime mov
video/quicktime qt
video/x-la-asf lsf
video/x-la-asf lsx
video/x-ms-asf asf
video/x-ms-asf asr
video/x-ms-asf asx
video/x-msvideo avi
video/x-sgi-movie movie
x-world/x-vrml flr
x-world/x-vrml vrml
x-world/x-vrml wrl
x-world/x-vrml wrz
x-world/x-vrml xaf
x-world/x-vrml xof
*/.
不做处理,返回原 Response.
No description provided by the author
# Interfaces
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author