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

$ vim service/user/cmd/api/internal/config/config.go
package config

import ""

type Config struct {
    Mysql struct{
        DataSource string

    CacheRedis cache.CacheConf

Improve yaml configuration

$ vim service/user/cmd/api/etc/user-api.yaml
Name: user-api
Port: 8888
  DataSource: $user:$password@tcp($url)/$db?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
  - Host: $host
    Pass: $pass
    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:

$pass: redis password

For more configuration information, please refer to api configuration introduction

Improve service dependence

$ vim service/user/cmd/api/internal/svc/servicecontext.go
type ServiceContext struct {
    Config    config.Config
    UserModel model.UserModel

func NewServiceContext(c config.Config) *ServiceContext {
    return &ServiceContext{
        Config: c,
        UserModel: model.NewUserModel(conn,c.CacheRedis),

Fill in the login logic

$ vim service/user/cmd/api/internal/logic/loginlogic.go
func (l *LoginLogic) Login(req types.LoginReq) (*types.LoginReply, error) {
    if len(strings.TrimSpace(req.Username)) == 0 || len(strings.TrimSpace(req.Password)) == 0 {
        return nil, errors.New("Invalid parameter")

    userInfo, err := l.svcCtx.UserModel.FindOneByNumber(req.Username)
    switch err {
    case nil:
    case model.ErrNotFound:
        return nil, errors.New("Username does not exist")
        return nil, err

    if userInfo.Password != req.Password {
        return nil, errors.New("User password is incorrect")

    // ---start---
    now := time.Now().Unix()
    accessExpire := l.svcCtx.Config.Auth.AccessExpire
    jwtToken, err := l.getJwtToken(l.svcCtx.Config.Auth.AccessSecret, now, l.svcCtx.Config.Auth.AccessExpire, userInfo.Id)
    if err != nil {
        return nil, err
    // ---end---

    return &types.LoginReply{
        Id:           userInfo.Id,
        Name:         userInfo.Name,
        Gender:       userInfo.Gender,
        AccessToken:  jwtToken,
        AccessExpire: now + accessExpire,
        RefreshAfter: now + accessExpire/2,
    }, nil

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

Guess you wants

Copyright © 2019-2021 go-zero all right reserved,powered by GitbookLast UpdateTime: 2021-12-05 09:48:50

results matching ""

    No results matching ""