配置引入

由于bootrouter包使用了init包初始化方式来进行相关配置,因此我们需要使用:

  1. import _ "PATH"

方式来引入。

需要注意,由于Goimport是存在先后顺序的,往往需要将这两个包置于所有业务包的最上方引入。

main

每个项目至少存在一个package main,用于程序的入口执行。

/main.go

  1. package main
  2. import (
  3. _ "github.com/gogf/gf-demos/boot"
  4. _ "github.com/gogf/gf-demos/router"
  5. "github.com/gogf/gf/frame/g"
  6. )
  7. func main() {
  8. g.Server().Run()
  9. }

需要非常注意:boot包的引入往往需要在main包中的最顶层位置,以保证应用的初始化最开始进行。特别是一些单例对象的初始化/配置管理操作往往放在boot包中,这样其他包才能正常使用到这些单例对象。

这里建议引入boot包、router包和其他包之间加入一个空行以作区分,特别是Goland IDE的import插件不会将引入包进行自动排序。

编译运行

我们可以使用IDE执行运行,也可以使用以下命令编译运行。

  1. $ go build main.go
  2. $ ./main

执行后,终端输出的路由表如下:

  1. SERVER | DOMAIN | ADDRESS | METHOD | ROUTE | HANDLER | MIDDLEWARE
  2. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  3. default | default | :8199 | ALL | /chat | github.com/gogf/gf-demos/app/api.(*apiChat).Index | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  4. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  5. default | default | :8199 | ALL | /chat/index | github.com/gogf/gf-demos/app/api.(*apiChat).Index | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  6. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  7. default | default | :8199 | ALL | /chat/setname | github.com/gogf/gf-demos/app/api.(*apiChat).SetName | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  8. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  9. default | default | :8199 | ALL | /chat/websocket | github.com/gogf/gf-demos/app/api.(*apiChat).WebSocket | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  10. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  11. default | default | :8199 | ALL | /swagger/* | github.com/gogf/swagger.(*Swagger).Install.func1.1 | HOOK_BEFORE_SERVE
  12. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  13. default | default | :8199 | ALL | /user/checknickname | github.com/gogf/gf-demos/app/api.(*apiUser).CheckNickName | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  14. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  15. default | default | :8199 | ALL | /user/checkpassport | github.com/gogf/gf-demos/app/api.(*apiUser).CheckPassport | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  16. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  17. default | default | :8199 | ALL | /user/issignedin | github.com/gogf/gf-demos/app/api.(*apiUser).IsSignedIn | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  18. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  19. default | default | :8199 | ALL | /user/profile | github.com/gogf/gf-demos/app/api.(*apiUser).Profile-fm | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm,service.(*serviceMiddleware).Auth-fm
  20. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  21. default | default | :8199 | ALL | /user/signin | github.com/gogf/gf-demos/app/api.(*apiUser).SignIn | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  22. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  23. default | default | :8199 | ALL | /user/signout | github.com/gogf/gf-demos/app/api.(*apiUser).SignOut | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  24. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------
  25. default | default | :8199 | ALL | /user/signup | github.com/gogf/gf-demos/app/api.(*apiUser).SignUp | service.(*serviceMiddleware).Ctx-fm,service.(*serviceMiddleware).CORS-fm
  26. ----------|---------|---------|--------|---------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------

接口测试

我们通过curl命令来对其中两个接口执行简单的测试。

1. 用户注册 - /user/signup

注册一个账号test001,昵称为john,密码为123456

  1. curl -d 'nickname=john&passport=test001&password=123456&password2=123456' http://127.0.0.1:8199/user/signup
  1. {"data":null,"code":0,"message":"ok"}

我们再次使用刚才的信息注册一次试试。

  1. curl -d 'nickname=john&passport=test001&password=123456&password2=123456' http://127.0.0.1:8199/user/signup
  1. {"data":null,"code":1,"message":"账号 test001 已经存在"}

可以看到注册失败了,相同名称只能注册一个账号。

2.用户登录 - /user/signin

我们先访问获取用户信息的接口,验证鉴权中间件是否生效。

  1. curl http://127.0.0.1:8199/user/profile
  1. Forbidden

我们用刚才注册的账号登录。

  1. curl -i -d 'passport=test001&password=123456' http://127.0.0.1:8199/user/signin
  1. HTTP/1.1 200 OK
  2. Access-Control-Allow-Credentials: true
  3. Access-Control-Allow-Headers: Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With
  4. Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE
  5. Access-Control-Allow-Origin: *
  6. Access-Control-Max-Age: 3628800
  7. Content-Type: application/json
  8. Server: gf-demos
  9. Set-Cookie: gfsessionid=BZT1SP2OX980EHALYV; Path=/; Expires=Sun, 10 Jan 2021 14:56:36 GMT
  10. Date: Sat, 11 Jan 2020 14:56:36 GMT
  11. Content-Length: 37
  12. {"code":0,"message":"ok","data":null}

我们这里使用了-i选项用于终端打印出服务端返回的Header信息,目的是为了获取sessionidGF框架默认的sessionid名称为gfsessionid,我们看到返回的Header中已经有了,并且是通过Cookie方式返回的。

随后我们再次访问获取用户信息接口,并且这次提交gfsessionid,该信息可以通过Header也可以通过Cookie提交,服务端都是能够自动识别的。

  1. curl -H 'gfsessionid:BZT1SP2OX980EHALYV' http://127.0.0.1:8199/user/profile
  1. {"code":0,"message":"","data":{"id":1,"passport":"test001","password":"123456","nickname":"john","create_time":"2020-01-10 23:51:41"}}