Business code

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

In the previous section, we have written user.api based on the preliminary requirements to describe which services the user service provides to the outside world. In this section, we will continue with the previous steps. Use business coding to tell how go-zero is used in actual business.

Add Mysql configuration

  1. $ vim service/user/api/internal/config/config.go
  1. package config
  2. import "github.com/zeromicro/go-zero/rest"
  3. type Config struct {
  4. rest.RestConf
  5. Mysql struct{
  6. DataSource string
  7. }
  8. CacheRedis cache.CacheConf
  9. }

Improve yaml configuration

  1. $ vim service/user/api/etc/user-api.yaml
  1. Name: user-api
  2. Host: 0.0.0.0
  3. Port: 8888
  4. Mysql:
  5. DataSource: $user:$password@tcp($url)/$db?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
  6. CacheRedis:
  7. - Host: $host
  8. Pass: $pass
  9. Type: node

[!TIP] $user: mysql database user

$password: mysql database password

$url: mysql database connection address

$db: mysql database db name, that is, the database where the user table is located

$host: Redis connection address Format: ip:port, such as: 127.0.0.1:6379

$pass: redis password

For more configuration information, please refer to api configuration introduction

Improve service dependence

  1. $ vim service/user/api/internal/svc/servicecontext.go
  1. type ServiceContext struct {
  2. Config config.Config
  3. UserModel model.UserModel
  4. }
  5. func NewServiceContext(c config.Config) *ServiceContext {
  6. conn:=sqlx.NewMysql(c.Mysql.DataSource)
  7. return &ServiceContext{
  8. Config: c,
  9. UserModel: model.NewUserModel(conn,c.CacheRedis),
  10. }
  11. }

Fill in the login logic

  1. $ vim service/user/api/internal/logic/loginlogic.go
  1. func (l *LoginLogic) Login(req types.LoginReq) (*types.LoginReply, error) {
  2. if len(strings.TrimSpace(req.Username)) == 0 || len(strings.TrimSpace(req.Password)) == 0 {
  3. return nil, errors.New("Invalid parameter")
  4. }
  5. userInfo, err := l.svcCtx.UserModel.FindOneByNumber(req.Username)
  6. switch err {
  7. case nil:
  8. case model.ErrNotFound:
  9. return nil, errors.New("Username does not exist")
  10. default:
  11. return nil, err
  12. }
  13. if userInfo.Password != req.Password {
  14. return nil, errors.New("User password is incorrect")
  15. }
  16. // ---start---
  17. now := time.Now().Unix()
  18. accessExpire := l.svcCtx.Config.Auth.AccessExpire
  19. jwtToken, err := l.getJwtToken(l.svcCtx.Config.Auth.AccessSecret, now, l.svcCtx.Config.Auth.AccessExpire, userInfo.Id)
  20. if err != nil {
  21. return nil, err
  22. }
  23. // ---end---
  24. return &types.LoginReply{
  25. Id: userInfo.Id,
  26. Name: userInfo.Name,
  27. Gender: userInfo.Gender,
  28. AccessToken: jwtToken,
  29. AccessExpire: now + accessExpire,
  30. RefreshAfter: now + accessExpire/2,
  31. }, nil
  32. }

[!TIP] For the code implementation of [start]-[end] in the above code, please refer to the Jwt Authentication chapter

Guess you wants