Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing User Management for Personal Ollama-Chat Application

Tech May 16 1

User Management Implementation

The project structure follows a modular design with distinct directories for services, common utilities, and database models.

|-- Dockerfile
|-- LICENSE
|-- common
|   |-- callmodel
|   |   |-- gemma.go
|   |   `-- models.go
|   |-- consts
|   |   |-- code.go
|   |   |-- common.go
|   |   |-- config.go
|   |   `-- consts.go
|   |-- cryptx
|   |   `-- crypt.go
|   |-- curlhttp
|   |   `-- curl.go
|   |-- database
|   |   |-- common.go
|   |   |-- connect.go
|   |   |-- dao.go
|   |   |-- ormLogx.go
|   |   |-- redisClient.go
|   |   `-- redisDao.go
|   |-- go.mod
|   |-- go.sum
|   |-- jwtx
|   |   `-- jwt.go
|   |-- middleware
|   |   `-- static.go
|   |-- model
|   |   |-- chat.sql
|   |   |-- chatmodel.go
|   |   |-- chatmodel_client.go
|   |   |-- prompt.sql
|   |   |-- promptmodel.go
|   |   |-- promptmodel_client.go
|   |   |-- readMe.md
|   |   |-- user.sql
|   |   |-- usermodel.go
|   |   `-- usermodel_client.go
|   `-- utils
|       `-- utils.go
|-- docker-compose.yaml
|-- nginx
|   `-- conf.d
|       `-- default.conf
|-- readme.md
`-- service
    |-- chat
    |   |-- api
    |   `-- rpc
    `-- user
        |-- api
        `-- rpc

4.1 Database Model Generation

A SQL schema defines the user entity within the openui database:

CREATE TABLE `user` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '' COMMENT 'User name',
  `email` varchar(255) NOT NULL DEFAULT '' COMMENT 'User email',
  `password` varchar(255) NOT NULL DEFAULT '' COMMENT 'User password',
  `role` varchar(264) NOT NULL DEFAULT '' COMMENT 'User role',
  `profile_image_url` varchar(255) NOT NULL DEFAULT '' COMMENT 'Profile image URL',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Use the following command too generate Go models from the DDL:

goctl model mysql ddl -src ./model/user.sql -dir ./model -c

4.2 API Service Creation

Define the API contract using the .api file format:

 type (
  // Login request
  LoginRequest {
    Email    string `json:"email"`
    Password string `json:"password"`
  }
  LoginResponse {
    Id              int64  `json:"id"`
    Name            string `json:"name"`
    Role            string `json:"role"`
    ProfileImageUrl string `json:"profile_image_url"`
    Token           string `json:"token"`
  }

  // Registration request
  RegisterRequest {
    Name            string `json:"name"`
    Email           string `json:"email"`
    Password        string `json:"password"`
    ProfileImageUrl string `json:"profile_image_url"`
  }
  RegisterResponse {
    Id    int64  `json:"id"`
    Name  string `json:"name"`
    Token string `json:"token"`
  }

  // User info response
  UserInfoResponse {
    Id              int64  `json:"id"`
    Name            string `json:"name"`
    Email           string `json:"email"`
    Role            string `json:"role"`
    ProfileImageUrl string `json:"profile_image_url"`
  }
)

@server (
  prefix: /api/v1
)
service User {
  @handler Login
  post /auths/signin (LoginRequest) returns (LoginResponse)

  @handler Register
  post /auths/signup (RegisterRequest) returns (RegisterResponse)
}

@server (
  jwt:    Auth
  prefix: /api/v1
)
service User {
  @handler UserInfo
  get /auths returns (UserInfoResponse)
}

Generate the Go API implementation with:

goctl api go -api ./api/user.api -dir ./api

4.3 RPC Service Definition

Create a Protocol Buffers definition for gRPC communication:

syntax = "proto3";

package userclient;

option go_package = "./user";

message LoginRequest {
  string Email = 1;
  string Password = 2;
}

message LoginResponse {
  int64 Id = 1;
  string Name = 2;
  string Token = 3;
  string Role = 4;
  string ProfileImageUrl = 5;
}

message RegisterRequest {
  string Name = 1;
  string Email = 2;
  string Password = 3;
  string ProfileImageUrl = 4;
}

message RegisterResponse {
  int64 Id = 1;
  string Name = 2;
  string Token = 3;
}

message UserInfoRequest {
  int64 Id = 1;
}

message UserInfoResponse {
  int64 Id = 1;
  string Name = 2;
  string Email = 3;
  string Role = 4;
  string ProfileImageUrl = 5;
}

service User {
  rpc Login(LoginRequest) returns(LoginResponse);
  rpc Register(RegisterRequest) returns(RegisterResponse);
  rpc UserInfo(UserInfoRequest) returns(UserInfoResponse);
}

Generate the Go stubs with:

goctl rpc protoc ./rpc/user.proto --go_out=./rpc/types --go-grpc_out=./rpc/types --zrpc_out=./rpc

4.4 Configuration Files

RPC Configuration

Name: user.rpc
ListenOn: 0.0.0.0:9001

Etcd:
  Hosts:
    - xxxxxxxxxxxxxxxxxxxxxxxxx:2379
  Key: user.rpc

Timeout: 0

Mysql:
  Host: xxxxxxxxxxxxxx
  Port: 3306
  DbName: openui
  User: xxxxxx
  Password: "xxxxxxxxxxxxxxxxxxxxxxxxx"
  DBZone: "TS"
  Charset: utf8mb4
  MaxIdle: 10
  MaxOpen: 100
  LogMode: true
  Loc: Asia/Shanghai
  Debug: true
  TablePrefix: "v1_"
  MaxLifetime: 300

CacheRedis:
  Name: "openui"
  Nettype: "tcp"
  Address: "redis:6379"
  Auth: ""
  DB: 0

Salt: ********************

LogConf:
  ServiceName: user.rpc
  Mode: file
  TimeFormat: 2006-01-02 15:04:05.000
  Path: logs
  Level: info
  Compress: true
  Stat: false
  KeepDays: 10
  MaxBackups: 2

API Configuration

Name: user.rpc
ListenOn: 0.0.0.0:9001

Etcd:
  Hosts:
    - xxxxxxxxxxxxxxxxxxxxxxxxx:2379
  Key: user.rpc

Timeout: 0

Mysql:
  Host: xxxxxxxxxxxxxx
  Port: 3306
  DbName: openui
  User: xxxxxx
  Password: "xxxxxxxxxxxxxxxxxxxxxxxxx"
  DBZone: "TS"
  Charset: utf8mb4
  MaxIdle: 10
  MaxOpen: 100
  LogMode: true
  Loc: Asia/Shanghai
  Debug: true
  TablePrefix: "v1_"
  MaxLifetime: 300

CacheRedis:
  Name: "openui"
  Nettype: "tcp"
  Address: "redis:6379"
  Auth: ""
  DB: 0

Salt: *****************

LogConf:
  ServiceName: user.rpc
  Mode: file
  TimeFormat: 2006-01-02 15:04:05.000
  Path: logs
  Level: info
  Compress: true
  Stat: false
  KeepDays: 10
  MaxBackups: 2

4.5 Business Logic Implementation

Business logic integration involves handling authentication flows, session management, and data persistence through generated models and services.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.