1 Star 0 Fork 41

甘欣亮 / lessgo

forked from andeyalee / lessgo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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 协议发布。

Copyright (c) 2013 Julien Schmidt. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JULIEN SCHMIDT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

Lessgo 是一款简单、稳定、高效、灵活的 golang web 开发框架,支持动态路由、自动化API测试文档、热编译、热更新等,实现前后端分离、系统与业务分离,完美兼容MVC与MVVC等多种开发模式,非常利于企业级应用与API接口的开发。 展开 收起
Go
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/ganxinliang/lessgo.git
git@gitee.com:ganxinliang/lessgo.git
ganxinliang
lessgo
lessgo
master

搜索帮助

14c37bed 8189591 565d56ea 8189591