|
Go 语言轻量 web 开发框架,特点是一行代码搞定 RESTFul,无需一堆的 controllers 和 models 文件,快速使用,性能优秀
Go 语言轻量 web 开发框架,特点是一行代码搞定 RESTFul,不依赖第三方 ORM,也不需要生成一堆的 controllers 和 models 文件,快速使用,性能优秀。
快速使用备注:由于嵌入了C代码,若遇到提示gcc无法编译的问题,可删掉UtilsBuild_linux.go文件后再尝试 - package main
- import (
- "net/http"
- "strconv"
- "gitee.com/konyshe/gogo"
- )
- func main() {
- //初始化日志输出功能,DEBUG为日志输出级别,具体参考函数说明
- gogo.LogInit("DEBUG", 1024)
- // 初始化数据库连接
- if err := gogo.SQLInit("mysql", "数据库用户名:数据库密码@tcp(数据库地址:数据库端口)/表名?charset=utf8", 10, 1); err != nil {
- gogo.CheckErrorExit(err)
- return
- }
- // 增
- gogo.POST("/restful/:tablename", func(ctx *gogo.HTTPContext) {
- affect, err := gogo.SQLInsert(
- ctx.GetPathParam(":tablename"),
- ctx.GetPostBody())
- if err != nil {
- gogo.LogError(err)
- ctx.WriteString(err.Error())
- } else {
- ctx.WriteString(strconv.FormatInt(affect, 10))
- }
- })
- // 删
- gogo.DELETE("/restful/:tablename/:id", func(ctx *gogo.HTTPContext) {
- affect, err := gogo.SQLDelete(
- ctx.GetPathParam(":tablename"),
- "id="+ctx.GetPathParam(":id"))
- if err != nil {
- gogo.LogError(err)
- ctx.WriteString(err.Error())
- } else {
- ctx.WriteString(strconv.FormatInt(affect, 10))
- }
- })
- // 改
- gogo.PUT("/restful/:tablename/:id", func(ctx *gogo.HTTPContext) {
- affect, err := gogo.SQLUpdate(
- ctx.GetPathParam(":tablename"),
- "id="+ctx.GetPathParam(":id"),
- ctx.GetPostBody())
- if err != nil {
- gogo.LogError(err)
- ctx.WriteString(err.Error())
- } else {
- ctx.WriteString(strconv.FormatInt(affect, 10))
- }
- })
- // 查
- gogo.GET("/restful/:tablename/:id", func(ctx *gogo.HTTPContext) {
- queryData, err := gogo.SQLQueryByMap(
- "",
- ctx.GetString("feilds"),
- ctx.GetPathParam(":tablename"),
- "id="+ctx.GetPathParam(":id"),
- "", 0, 1)
- if err != nil {
- gogo.LogError(err)
- ctx.WriteString(err.Error())
- } else {
- ctx.WriteJSON(queryData)
- }
- })
- // 查
- gogo.GET("/restful/:tablename", func(ctx *gogo.HTTPContext) {
- queryData, err := gogo.SQLQueryByMap(
- ctx.GetString("columnname"),
- ctx.GetString("feilds"),
- ctx.GetPathParam(":tablename"),
- ctx.GetString("where"),
- ctx.GetString("order"),
- ctx.GetInt("offset", 0),
- ctx.GetInt("count", 10))
- if err != nil {
- gogo.LogError(err)
- ctx.WriteString(err.Error())
- } else {
- ctx.WriteJSON(queryData)
- }
- })
- // 404页面
- gogo.STATUS(http.StatusNotFound, func(ctx *gogo.HTTPContext) {
- ctx.WriteHeaderStatus(http.StatusNotFound)
- ctx.WriteString("Page Not Found !")
- })
- // 启动HTTP服务
- gogo.StartHTTP(3009)
- }
复制代码 数据库表结构
- mysql> desc dede_flink;
- +----------+----------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+----------------------+------+-----+---------+----------------+
- | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
- | sortrank | smallint(6) | NO | | 0 | |
- | url | char(60) | NO | | | |
- | webname | char(30) | NO | | | |
- | msg | char(200) | NO | | | |
- | email | char(50) | NO | | | |
- | logo | char(60) | NO | | | |
- | dtime | int(10) unsigned | NO | | 0 | |
- | typeid | smallint(5) unsigned | NO | | 0 | |
- | ischeck | smallint(6) | NO | | 1 | |
- +----------+----------------------+------+-----+---------+----------------+
- 10 rows in set
复制代码 Windows
- go build -o app.exe
- app.exe
复制代码
Linux/MacOS
浏览器访问测试- 增
- POST http://localhost:3009/restful/dede_flink
复制代码 以下是 application/json 内容,可以批量添加多条数据
- [
- {
- "id": 20,
- "ischeck": 1,
- "webname": "hxyw",
- "url":"http://www.hxyw.org"
- },
- {
- "id": 21,
- "ischeck": 1,
- "webname": "bejson",
- "url":"http://www.bejson.com"
- }
- ]
复制代码
- 改
- PUT http://localhost:3009/restful/dede_flink/21
复制代码 以下是 application/json 内容,这里只将 id=21 的数据,webname 字段修改为"hello"
- 查
- GET http://localhost:3009/restful/dede_flink/21
- GET http://localhost:3009/restful/dede_flink?order=-id
- GET http://localhost:3009/restful/dede_flink?offset=10&&count=100&&columnname=webname
复制代码
- 删
- DELETE http://localhost:3009/restful/dede_flink/1
复制代码
- 404
- GET http://localhost:3009/weqwe
复制代码
链接:https://pan.quark.cn/s/9d7ec0ac5fe8
提取码下载:
|
|