Categorygithub.com/henrylee2cn/lessgo
modulepackage
1.0.0
Repository: https://github.com/henrylee2cn/lessgo.git
Documentation: pkg.go.dev

# README

Lessgo Web Framework GoDoc GitHub release

Lessgo Favicon

概述

Lessgo是一款Go语言开发的简单、稳定、高效、灵活的 web开发框架。它的项目组织形式经过精心设计,实现前后端分离、系统与业务分离,完美兼容MVC与MVVC等多种开发模式,非常利于企业级应用与API接口的开发。当然,最值得关注的是它突破性支持运行时路由重建,开发者可在Admin后台轻松配置路由,并实现启用/禁用模块或操作、添加/移除中间件等!同时,它以ApiHandler与ApiMiddleware为项目基本组成单元,可实现编译期或运行时的自由搭配组合,也令开发变得更加灵活富有趣味性。

官方QQ群:Go-Web 编程 42730308 Go-Web 编程群

适用场景

  • 网站
  • web应用
  • Restful API服务应用
  • 企业应用

当前版本

  • V0.7.0
  • 发布日期:2016.06.01

最新功能特性

  • 使用简单、运行稳定高效(核心架构来自对echo真正意义的二次开发)
  • 兼容流行系统模式如:MVC、MVVC、Restful...
  • httprouter真实路由配合强大的虚拟路由层,不仅性能优秀更可同时支持在源码或admin中动态配置
  • 多异构数据库支持,且用户可以选择xorm或者gorm两种引擎(当然愿意,用户还可以同时使用两种引擎)
  • 优化的项目目录组织最佳实践,满足复杂企业应用需要
  • 集成统一的系统日志(system、database独立完整的日志)
  • 提供Session管理(优化beego框架中的session包)
  • 强大的前端模板渲染引擎(pongo2)
  • 天生支持运行时可更新的API测试网页(swagger2.0)
  • 配置文件自动补填默认值,并按字母排序
  • 支持热编译
  • 支持热升级
  • 另外灵活的扩展包中还包含HOTP、TOTP、UUID以及各种条码生成工具等常用工具包

Lessgo Server Lessgo Server

框架下载

go get -u github.com/henrylee2cn/lessgo
go get -u github.com/henrylee2cn/less
go get -u github.com/henrylee2cn/lessgoext/...

框架构成

代码示例

  • main.go
import (
    "github.com/henrylee2cn/lessgo"
    "github.com/henrylee2cn/lessgoext/swagger"

    _ "github.com/henrylee2cn/lessgoext/dbservice/xorm"
    // _ "github.com/henrylee2cn/lessgoext/dbservice/gorm"

    _ "github.com/henrylee2cn/lessgo_demo/middleware"
    _ "github.com/henrylee2cn/lessgo_demo/router"
)

func main() {
    // 开启自动api文档,通过config/apidoc_allow.myconfig进行配置
    swagger.Reg()
    // 指定根目录URL
    lessgo.SetHome("/home")
    // 开启网络服务
    lessgo.Run()
}
  • 定义一个较复杂的操作
import (
    . "github.com/henrylee2cn/lessgo"
    "github.com/henrylee2cn/lessgo_demo/sysmodel/admin"
)

var Index = ApiHandler{
    Desc:   "后台管理登录操作",
    Method: "POST|PUT",
    Params: []Param{
        {"user", "formData", true, "henry11111", "用户名"},
        {"user", "query", true, "henry22222", "用户名"},
        {"user", "path", true, "henry33333", "用户名"},
        {"password", "formData", true, "1111111111", "密码"},
        {"password", "query", true, "2222222222", "密码"},
        {"password", "path", true, "3333333333", "密码"},
    },
    Handler: func(c *Context) error {
        // 测试读取cookie
        id := c.CookieParam(Config.Session.SessionName)
        c.Log().Info("cookie中的%v: %#v", Config.Session.SessionName, id)

        // 测试session
        c.Log().Info("从session读取上次请求的输入: %#v", c.GetSession("info"))

        c.SetSession("info", map[string]interface{}{
            "user":     c.FormParam("user"),
            "password": c.FormParam("password"),
        })
        c.Log().Info("path用户名: %#v", c.PathParam("user"))
        c.Log().Info("query用户名: %#v", c.QueryParam("user"))
        c.Log().Info("formData用户名: %#v", c.FormParam("user"))
        c.Log().Info("path密码: %#v", c.PathParam("password"))
        c.Log().Info("query密码: %#v", c.QueryParam("password"))
        c.Log().Info("formData密码: %#v", c.FormParam("password"))

        return c.Render(200,
            "sysview/admin/login/index.tpl",
            map[string]interface{}{
                "name":       c.FormParam("user"),
                "password":   c.FormParam("password"),
                "repeatfunc": admin.Login.Repeatfunc,
            },
        )
    },
}.Reg()
  • 一个简单的数据模型
import (
    "strings"
)
type login struct{}
var Login = login{}

func (_ login) Repeatfunc(s string, count int) string {
    return strings.Repeat(s, count)
}
  • 一个简单的中间件
var ShowHeader = lessgo.ApiMiddleware{
    Name:   "显示Header",
    Desc:   "显示Header测试",
    Config: nil,
    Middleware: func(c *lessgo.Context) error {
        c.Log().Info("测试中间件-显示Header:%v", c.Request().Header)
        return nil
    },
}.Reg()
  • 在源码中定义路由
package router

import (
    "github.com/henrylee2cn/lessgo"

    "github.com/henrylee2cn/lessgo_demo/bizhandler/home"
    "github.com/henrylee2cn/lessgo_demo/middleware"
)

func init() {
    lessgo.Root(
        lessgo.Leaf("/websocket", home.WebSocket, middleware.ShowHeader),
        lessgo.Branch("/home", "前台",
            lessgo.Leaf("/index", home.Index, middleware.ShowHeader),
        ).Use(middleware.Print),
    )
}

系统文档

项目架构

Lessgo Web Framework

项目目录结构

─Project 项目开发目录
├─config 配置文件目录
│  ├─app.config 系统应用配置文件
│  └─db.config 数据库配置文件
├─common 后端公共目录
│  └─... 如utils等其他
├─middleware 后端公共中间件目录
├─static 前端公共目录 (url: /static)
│  ├─tpl 公共tpl模板目录
│  ├─js 公共js目录 (url: /static/js)
│  ├─css 公共css目录 (url: /static/css)
│  ├─img 公共img目录 (url: /static/img)
│  └─plugin 公共js插件 (url: /static/plugin)
├─uploads 默认上传下载目录
├─router 源码路由配置
│  ├─sys_router.go 系统模块路由文件
│  ├─biz_router.go 业务模块路由文件
├─sys_handler 系统模块后端目录
│  ├─xxx 子模块目录
│  │  ├─example.go example操作
│  │  └─... xxx的子模块目录
│  └─... 其他子模块目录
├─sys_model 系统模块数据模型目录
├─sys_view 系统模块前端目录 (url: /sys)
│  ├─xxx 与sys_handler对应的子模块目录 (url: /sys/xxx)
│  │  ├─example.tpl 相应操作的模板文件
│  │  ├─example2.html 无需绑定操作的静态html文件
│  │  ├─xxx.css css文件(可有多个)
│  │  ├─xxx.js js文件(可有多个)
│  │  └─... xxx的子模块目录
├─biz_handler 业务模块后端目录
│  ├─xxx 子模块目录
│  │  ├─example.go example操作
│  │  └─... xxx的子模块目录
│  └─... 其他子模块目录
├─biz_model 业务模块数据模型目录
├─biz_view 业务模块前端目录 (url: /biz)
│  ├─xxx 与biz_handler对应的子模块目录 (url: /biz/xxx)
│  │  ├─example.tpl 相应操作的模板文件
│  │  ├─example2.html 无需绑定操作的静态html文件
│  │  ├─xxx.css css文件(可有多个)
│  │  ├─xxx.js js文件(可有多个)
│  │  └─... xxx的子模块目录
├─database 默认数据库文件存储目录
├─logger 运行日志输出目录
└─main.go 应用入口文件

贡献者名单

贡献者贡献概要
henrylee2cn代码的主要实现者 (第一作者)
changyu72架构的主要设计者 (第二作者)
LeSou

开源协议

Lessgo 项目采用商业应用友好的 MIT 协议发布。

# Packages

Package config is used to parse config Usage: import( "github.com/astaxie/beego/config" ) cnf, err := config.NewConfig("ini", "config.conf") cnf APIS: cnf.Set(key, val string) error cnf.String(key string) string cnf.Strings(key string) []string cnf.Int(key string) (int, error) cnf.Int64(key string) (int64, error) cnf.Bool(key string) (bool, error) cnf.Float(key string) (float64, error) cnf.DefaultString(key string, defaultVal string) string cnf.DefaultStrings(key string, defaultVal []string) []string cnf.DefaultInt(key string, defaultVal int) int cnf.DefaultInt64(key string, defaultVal int64) int64 cnf.DefaultBool(key string, defaultVal bool) bool cnf.DefaultFloat(key string, defaultVal float64) float64 cnf.DIY(key string) (interface{}, error) cnf.GetSection(section string) (map[string]string, error) cnf.SaveConfigFile(filename string) error more docs http://beego.me/docs/module/config.md.
No description provided by the author
Blackfriday markdown processor.
A Django-syntax like template-engine Blog posts about pongo2 (including introduction and migration): https://www.florian-schlachter.de/?tag=pongo2 Complete documentation on the template language: https://docs.djangoproject.com/en/dev/topics/templates/ Try out pongo2 live in the pongo2 playground: https://www.florian-schlachter.de/pongo2/ Make sure to read README.md in the repository as well.
Package session provider Usage: import( "github.com/astaxie/beego/session" ) func init() { globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":3600, "maxLifetime": 3600, "secure": false, "cookieLifeTime": 3600, "providerConfig": ""}`) go globalSessions.GC() } more docs: http://beego.me/docs/module/session.md.
No description provided by the author
Package websocket implements a client and server for the WebSocket protocol as specified in RFC 6455.

# Functions

插入到处理链中路由操作后一位的中间件(子链).
操作列表(禁止修改).
插入到处理链中路由操作前一位的中间件(子链).
配置虚拟路由分组(必须在init()中调用).
判断文件缓存是否开启.
CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating .
ContentTypeByExtension returns the MIME type associated with the file based on its extension.
判断当前是否为调试模式.
关闭文件缓存.
关闭网站服务.
启用文件缓存.
开启网站服务.
单独注册静态文件虚拟路由VirtFile(无法在Root()下使用).
返回设置的主页.
根据id获取虚拟路由节点.
根据path获取虚拟路由节点.
获取已注册的操作列表.
配置虚拟路由操作(必须在init()中调用).
获取已注册的中间件列表.
创建虚拟路由动态分组.
创建虚拟路由动态操作.
NewHTTPError creates a new HTTPError instancthis.
No description provided by the author
New creates a new Pongo2Render instance with custom Options.
NewResponse creates a new instance of Response.
No description provided by the author
添加到处理链最前端的中间件(子链).
No description provided by the author
返回底层注册的路由列表(全部真实注册的路由).
主动刷新缓存文件.
Reregister real router.
清空用户添加到处理链中路由操作后的所有中间件(子链).
清空用户添加到处理链中路由操作前的所有中间件(子链).
清空用户单独注册静态文件虚拟路由VirtFile.
清空用户单独注册静态目录虚拟路由VirtFile.
从根路由开始配置虚拟路由(必须在init()中调用).
虚拟路由根节点.
运行服务 @param graceExitCallback设置优雅关闭或重启时的收尾函数.
查询网站服务状态.
Session管理平台实例.
设置捆绑数据处理接口(内部有默认实现).
设置运行模式.
设置失败状态默认的响应操作(内部有默认实现).
设置主页(内部已默认为"/").
设置获取请求过程中恐慌Stack信息的函数(内部有默认实现).
设置html模板处理接口(内部有默认实现).
单独注册静态目录虚拟路由VirtStatic(无法在Root()下使用).
创建静态目录服务的操作(用于在Root()下).
追加到处理链最末端的中间件(子链).
No description provided by the author
返回单独注册的静态文件虚拟路由列表(非Root()下注册).
返回当前虚拟的路由列表(不含单独注册的静态路由VirtFiles/VirtStatics).
返回单独注册的静态目录虚拟路由列表(非Root()下注册).
自动转换某些允许的函数为中间件函数.
自动转换某些允许的对象为中间件配置类型.
No description provided by the author

# Constants

No description provided by the author
exclusion of all methods out of "WS".
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
HTTP methods.
项目固定目录文件名称.
HTTP methods.
HTTP methods.
虚拟路由节点类型.
虚拟路由节点类型.
HTTP methods.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Security.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
No description provided by the author
项目固定目录文件名称.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
No description provided by the author
HTTP methods.
HTTP methods.
项目固定目录文件名称.
POST
HTTP methods.
HTTP methods.
虚拟路由节点类型.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
项目固定目录文件名称.
HTTP methods.
项目固定目录文件名称.
No description provided by the author
websocket "GET".

# 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
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
No description provided by the author
全局运行日志实例(来自数据库的日志除外).
文件上传默认内存缓存大小,默认值是64MB。.
软件自身md5值.
No description provided by the author

# Structs

No description provided by the author
* 中间件 * ApiMiddleware.Middleware 支持的处理函数类型: * MiddlewareFunc * func(HandlerFunc) HandlerFunc * HandlerFunc * func(Context) error * ConfMiddlewareFunc * func(confObject interface{}) MiddlewareFunc */.
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
HTTPError represents an error that occured while handling a request.
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
Response wraps an http.ResponseWriter and implements its interface to be used by an HTTP handler to construct an HTTP response.
No description provided by the author
No description provided by the author
No description provided by the author
Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes.
No description provided by the author
No description provided by the author
单独注册的静态文件虚拟路由(无法在Root()下使用,暂不支持运行时修改).
虚拟路由(在Root()下使用,支持运行时修改).
单独注册的静态目录虚拟路由(无法在Root()下使用,暂不支持运行时修改).

# 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

# 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
No description provided by the author
No description provided by the author