Code Monkey home page Code Monkey logo

beego-demo's Introduction

beego-demo

A web demo using Beego framework, with MongoDB, MySQL and Redis support.

这是一个基于 Beego 框架构建的应用 demo,后台数据库使用 MongoDBMySQL,并使用 Redis 存储 session 和一些统计数据。

API列表

第一部分

该部分使用的数据库是 MongoDB 和 Redis。

功能 URL Mode
注册 /v1/users/register POST
登录 /v1/users/login POST
登出 /v1/users/logout POST
修改密码 /v1/users/passwd POST
上传多个文件 /v1/users/uploads POST
下载文件 /v1/users/downloads GET

在 static/test 目录下有如下的测试表单,除了用于测试外,也可看出具体的数据通讯协议:

  • register.html
  • login.html
  • logout.html
  • passwd.html
  • uploads.html

说明:

  • 输入数据通过 form 表单提交,返回数据均为 json。
  • 使用 Beego 的 ParseForm 功能将输入数据解析到 struct 中。
  • 使用 Beego 的 Validation 功能对数据进行校验。
  • 使用 scrypt 算法进行密码处理。
  • 对于数据库返回的错误,单独区分“记录不存在”和“记录重复”两种错误。
  • 由于 Beego 本身不支持多文件上传,故单独实现了 uploads API 来展示该功能,该功能与数据库无关。

第二部分

该部分使用的数据库是 MySQL。

功能 URL Mode
获取一个角色信息 /v1/roles/:id GET
获取所有角色信息 /v1/roles GET
新增一个角色信息 /v1/roles POST
修改一个角色信息 /v1/roles/:id PUT
删除一个角色信息 /v1/roles/:id DELETE
认证 /v1/roles/auth POST

roles表结构如下:

Field Type Null Key
id bigint(20) NO PRI
name varchar(255) YES
password varchar(255) YES
reg_date datetime YES

初始建数据库表的脚本位于:scripts/sql/db.sql。

多记录 api,提供如下参数:

  • query=col1:op1:val1,col2:op2:val2 ...
  • order=col1:asc|desc,col2:asc|esc ...
  • limit=n,缺省为 10
  • offset=n,缺省为 0

query 的 op 值:

  • eq,等于
  • ne,不等于
  • gt,大于
  • ge,大于等于
  • lt,小于
  • le,小于等于

说明:

  • 参考 RESTful 模式设计 API。
  • 使用 JSON Web Token (JWT) 做认证手段。
  • 输入数据采用 json,返回数据也是 json。
  • 数据库操作使用原生 SQL,没有采用 ORM。
  • 对可能的 NULL 值做了处理。
  • 多记录查询通过拼接 SQL 语句实现,故对输入参数做了一些校验和处理。
  • 同样单独区分“记录不存在”和“记录重复”两种数据库错误。

环境

GO语言

包括安装 go,设置 $GOPATH 等,具体可参考:How to Write Go Code

MongoDB

在 conf/app.conf 中设置 MongoDB 参数,如:

[mongodb]
url = mongodb://127.0.0.1:27017/beego-demo

完整的 url 写法可参考:http://godoc.org/gopkg.in/mgo.v2#Dial

这里单独封装了一个 mymongo 包来实现数据库的初始化,以简化后续的数据库操作。

MySQL

在 conf/app.conf 中设置 MySQL 参数,如:

[mysql]
url = root:root@/beego-demo?charset=utf8&parseTime=True&loc=Local

完整的 url 写法可参考:https://github.com/go-sql-driver/mysql#dsn-data-source-name

这里单独封装了一个 mymysql 包来实现数据库的初始化,以简化后续的数据库操作。

Redis

在 conf/app.conf 中设置 Redis 参数,涉及两个地方,一个是 session,一个是 cache,两者可以不同:

sessionproviderconfig = 127.0.0.1:6379

[cache]
server = 127.0.0.1:6379
password =

这里单独封装了一个 myredis 包来实现数据库的初始化,以简化后续的数据库操作。

运行

克隆代码:

$ git clone https://github.com/gwduan/beego-demo.git
$ cd beego-demo/

之前使用 glide 工具来管理依赖包,从go1.13 开始,切换到Go Modules 。

编译:

$ go build

运行:

$ ./beego-demo

也可安装 bee 工具,这在调试阶段很好用:

$ go get -u github.com/beego/bee

通过bee运行:

$ bee run

当前版本:

$ bee version
______
| ___ \
| |_/ /  ___   ___
| ___ \ / _ \ / _ \
| |_/ /|  __/|  __/
\____/  \___| \___| v1.10.0

├── Beego     : 1.12.0
├── GoVersion : go1.13.1
├── GOOS      : linux
├── GOARCH    : amd64

依赖包列表:

  • github.com/astaxie/beego
  • github.com/astaxie/beego/session/redis
  • gopkg.in/mgo.v2
  • github.com/gomodule/redigo/redis
  • github.com/go-sql-driver/mysql
  • golang.org/x/crypto/scrypt
  • github.com/dgrijalva/jwt-go

部署

正式部署时,可通过系统的 Init 服务来启动。在 scripts 目录下有 upstart 和 systemd 两套简易示例脚本,可参考使用。

例如,在 CentOS 6 下,复制 upstart/bdemo.conf 到 /etc/init/,相应修改后,执行:

# start bdemo

在 CentOS 7 下,复制 systemd/bdemo.service 到 /etc/systemd/system/,相应修改后,执行:

# systemctl daemon-reload
# systemctl enable bdemo.service
# systemctl start bdemo.service

由于 Init 是由 root 控制的,相应的服务缺省也具有 root 权限,故一般都应该做降权处理。可在 systemd 和 upstart 脚本中设置运行时的普通用户名和组名,具体可参考官方文档。

降权的问题在于普通用户无法绑定特权端口(如 80 ),不过实际环境下,还是建议在前面部署 Nginx 等成熟的 web 服务器,通过反向代理来访问应用。

beego-demo's People

Contributors

gwduan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

beego-demo's Issues

bee run

.\main.go:28: undefined: syscall.Setregid
.\main.go:31: undefined: syscall.Setreuid
2016/09/08 14:27:58 [ERRO] ============== Build failed ===================

controllers\roles.go:47: invalid operation: token.Claims["id"] (type jwt.Claims does not support indexing)

this errors:
controllers\roles.go:47: invalid operation: token.Claims["id"](type jwt.Claims does not support indexing)
controllers\roles.go:48: invalid operation: token.Claims["name"](type jwt.Claim s does not support indexing)
controllers\roles.go:49: invalid operation: token.Claims["exp"](type jwt.Claims does not support indexing)
controllers\roles.go:69: invalid operation: token.Claims["id"](type jwt.Claims does not support indexing)
controllers\roles.go:203: invalid operation: token.Claims["id"](type jwt.Claims does not support indexing)
controllers\roles.go:257: invalid operation: token.Claims["id"](type jwt.Claims does not support indexing)

注册报文是什么格式?

image

2019/06/08 17:06:16.860 [D] [users.go:76]  ParseLoginForm: &{ 123456}
2019/06/08 17:06:16.860 [D] [users.go:79]  ValidLoginForm: Phone.Required:Can not be empty;Phone.Mobile:Must be valid mobile number;
2019/06/08 17:06:16.862 [D] [server.go:2774]  |            ::1| 200 |     4.9747ms|   match| POST     /v1/users/login   r:/v1/users/login
2019/06/08 17:08:28.083 [D] [users.go:76]  ParseLoginForm: &{ } 
2019/06/08 17:08:28.084 [D] [users.go:79]  ValidLoginForm: Phone.Required:Can not be empty;Phone.Mobile:Must be valid mobile number;Password.Required:Can not be empty;       
2019/06/08 17:08:28.085 [D] [server.go:2774]  |            ::1| 200 |     3.0024ms|   match| POST     /v1/users/login   r:/v1/users/login
2019/06/08 17:08:52.523 [D] [users.go:76]  ParseLoginForm: &{ }
2019/06/08 17:08:52.524 [D] [users.go:79]  ValidLoginForm: Phone.Required:Can not be empty;Phone.Mobile:Must be valid mobile number;Password.Required:Can not be empty;       
2019/06/08 17:09:01.711 [D] [users.go:79]  ValidLoginForm: Phone.Required:Can not be empty;Phone.Mobile:Must be valid mobile number;Password.Required:Can not be empty;       
2019/06/08 17:09:01.712 [D] [server.go:2774]  |            ::1| 200 |     1.9957ms|   match| POST     /v1/users/login   r:/v1/users/login

router源码类中的疑问

在router源码类中有这么一段代码:
744 :if r.Method == http.MethodPost && context.Input.Query("_method") == http.MethodPost {
745 : method = http.MethodPut
746 : }
747 : if r.Method == http.MethodPost && context.Input.Query("_method") == http.MethodDelete {
748 : method = http.MethodDelete
749 :}
通过这两者的比较,744行中context.Input.Query("_method") == http.MethodPost 是不是应该改为context.Input.Query("_method") == http.MethodPut ?

panic: dial tcp 192.168.1.xxx:6379: connectex: No connection could be made because the target machine actively refused it.

I`m stuck on this error with Redis config....

Beego-demo runs on Win7 with Redis app "Docked" on a Synology NAS (reached at 192.168.1.xxx)
In conf/conf.app file, I've updated sessionproviderconfig and [cache] server sections with proper Redis ip/port.

But I still have this err msg :
panic: dial tcp 192.168.1.xxx:6379: connectex: No connection could be made because the target machine actively refused it.

Any idea ? Thx in advance.

Eric

jwt-go的问题

请问你的jwt-go具体是怎么使用的?
我看代码是在/roles/auth这个路由里面生成了一个token然后封装成json传给客户端,但是客户端也没有任何操作,这个是怎么写到http header里的?GetOne GetAll都有解析token,但是我没看见怎么写入的,能讲解一下吗

can't find ico file

2016/05/18 15:12:16 [router.go:829][D] | GET | / | 7.134515ms | match | / |
2016/05/18 15:12:16 [staticfile.go:51][W] Can't find/open the file: /favicon.ico file not find

我项目启动了。怎么访问呢

我用postman访问/v1/roles/auth
报503 | 11.0006ms| nomatch
路径没有匹配
前面还有,NOAUTH Authentication required.
这个是程序的无权访问吗,但是这个路由路径是获取token的,这也有权限限制那怎么搞

panic: Error 1045: Access denied for user 'root'@'localhost' (using password: YES)

when I run 'bee run':


| ___
| |/ / ___ ___
| ___ \ / _ \ / _
| |
/ /| /| /
_
/ _
| __| v1.8.0
2018/03/13 18:51:11 INFO ▶ 0001 Using 'beego-demo' as 'appname'
2018/03/13 18:51:11 INFO ▶ 0002 Initializing watcher...
2018/03/13 18:51:13 SUCCESS ▶ 0003 Built Successfully!
2018/03/13 18:51:13 INFO ▶ 0004 Restarting 'beego-demo'...
2018/03/13 18:51:13 SUCCESS ▶ 0005 './beego-demo' is running...
panic: Error 1045: Access denied for user 'root'@'localhost' (using password: YES)

goroutine 1 [running]:
panic(0x52b6c0, 0xc420188580)
/usr/local/go1.7.3/src/runtime/panic.go:500 +0x1a1
beego-demo/models/mymysql.init.1()
/Users/annie/code/golang/src/beego-demo/models/mymysql/mymysql.go:26 +0x106
beego-demo/models/mymysql.init()
/Users/annie/code/golang/src/beego-demo/models/mymysql/mymysql.go:31 +0x3d
beego-demo/models.init()
/Users/annie/code/golang/src/beego-demo/models/users_io.go:38 +0x3d
beego-demo/controllers.init()
/Users/annie/code/golang/src/beego-demo/controllers/users.go:271 +0x71
main.init()
/Users/annie/code/golang/src/beego-demo/main.go:46 +0x42

Then I change the password to my root password and re-run:


| ___
| |/ / ___ ___
| ___ \ / _ \ / _
| |
/ /| /| /
_
/ _
| __| v1.8.0
2018/03/13 18:52:54 INFO ▶ 0001 Using 'beego-demo' as 'appname'
2018/03/13 18:52:54 INFO ▶ 0002 Initializing watcher...
2018/03/13 18:52:55 SUCCESS ▶ 0003 Built Successfully!
2018/03/13 18:52:55 INFO ▶ 0004 Restarting 'beego-demo'...
2018/03/13 18:52:55 SUCCESS ▶ 0005 './beego-demo' is running...
panic: Error 1044: Access denied for user 'root'@'localhost' to database 'beego_demo'

goroutine 1 [running]:
panic(0x52b6c0, 0xc420188580)
/usr/local/go1.7.3/src/runtime/panic.go:500 +0x1a1
beego-demo/models/mymysql.init.1()
/Users/annie/code/golang/src/beego-demo/models/mymysql/mymysql.go:26 +0x106
beego-demo/models/mymysql.init()
/Users/annie/code/golang/src/beego-demo/models/mymysql/mymysql.go:31 +0x3d
beego-demo/models.init()
/Users/annie/code/golang/src/beego-demo/models/users_io.go:38 +0x3d
beego-demo/controllers.init()
/Users/annie/code/golang/src/beego-demo/controllers/users.go:271 +0x71
main.init()
/Users/annie/code/golang/src/beego-demo/main.go:46 +0x42

Do I need to create database'beego_demo' myself? or it already exised? how can I get access to it?

Session使用redis的问题

你好,我根据beego提供的controller session机制,使用redis作为存储,我跟了下代码打印,的确进入了sess_redis.go这个里面的set方法,但是redis-server查询不到对应的key。但是在代码里里面通过getSession可以获取到,是不是beego在使用redis存储session的时候,用了其他的名字?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.