Skip to content

go http grpc distributed microservices framework for enterprise in production

License

Notifications You must be signed in to change notification settings

skeleton1231/gotal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

86 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

README for GoTAL Project

Introduction

Welcome to GoTAL, a Go-based enterprise project. This project utilizes a robust architecture suitable for scalable and efficient web services.

Getting Started

Prerequisites

  • Golang environment setup
  • http server + grpc server (microservices)
  • MySQL and Redis services running
  • Necessary environment variables set for MySQL and Redis configurations

Framework Design

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Frontend    β”‚       β”‚   Frontend    β”‚       β”‚   Frontend    β”‚     ...... (Different Roles Of Clients)
β”‚   (Customer)  β”‚       β”‚   (Admin)     β”‚       β”‚   (Robot)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                       β”‚                       β”‚
        β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    BFF        β”‚       β”‚    BFF        β”‚       β”‚    BFF        β”‚
β”‚  (Controller  β”‚       β”‚  (Controller  β”‚       β”‚  (Controller  β”‚
β”‚   + Service)  β”‚       β”‚   + Service)  β”‚       β”‚   + Service)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                       β”‚                       β”‚
        β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Store Layer  β”‚       β”‚  Store Layer  β”‚       β”‚  Store Layer  β”‚
β”‚ (gRPC Client) β”‚       β”‚ (gRPC Client) β”‚       β”‚ (gRPC Client) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                       β”‚                       β”‚
        β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Backend gRPC  β”‚       β”‚ Backend gRPC  β”‚       β”‚ Backend gRPC  β”‚
β”‚   Services    β”‚       β”‚   Services    β”‚       β”‚   Services    β”‚
β”‚(Service +     β”‚       β”‚(Service +     β”‚       β”‚(Service +     β”‚
β”‚    Store)     β”‚       β”‚    Store)     β”‚       β”‚    Store)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                       β”‚                       β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                                β–Ό
                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                           β”‚  Database β”‚
                           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Project Structure

β”œβ”€β”€ CHANGELOG
β”œβ”€β”€ CONTRIBUTING.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ Makefile
β”œβ”€β”€ README.md
β”œβ”€β”€ api
β”œβ”€β”€ build
β”œβ”€β”€ cert
β”‚   β”œβ”€β”€ apiserver-key.pem
β”‚   β”œβ”€β”€ apiserver.pem
β”‚   β”œβ”€β”€ san.cnf
β”‚   β”œβ”€β”€ server.crt
β”‚   β”œβ”€β”€ server.csr
β”‚   └── server.key
β”œβ”€β”€ cmd
β”‚   β”œβ”€β”€ apiserver
β”‚   β”‚   β”œβ”€β”€  stdout
β”‚   β”‚   β”œβ”€β”€ apiserver
β”‚   β”‚   └── apiserver.go
β”‚   β”œβ”€β”€ authzserver
β”‚   β”‚   β”œβ”€β”€ authzserver
β”‚   β”‚   └── authzserver.go
β”‚   β”œβ”€β”€ redis_test
β”‚   β”‚   β”œβ”€β”€ main
β”‚   β”‚   β”œβ”€β”€ main.go
β”‚   β”‚   └── redis_test
β”‚   β”œβ”€β”€ rpc_test
β”‚   β”‚   └── main.go
β”‚   └── user_service
β”‚       β”œβ”€β”€ user.go
β”‚       └── user_service
β”œβ”€β”€ configs
β”‚   β”œβ”€β”€ apiserver-client.yaml
β”‚   β”œβ”€β”€ authzserver.yaml
β”‚   └── user-service.yaml
β”œβ”€β”€ deployments
β”œβ”€β”€ docs
β”œβ”€β”€ githooks
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ image.png
β”œβ”€β”€ init
β”œβ”€β”€ init.sh
β”œβ”€β”€ internal
β”‚   β”œβ”€β”€ apiserver
β”‚   β”‚   β”œβ”€β”€ app.go
β”‚   β”‚   β”œβ”€β”€ auth.go
β”‚   β”‚   β”œβ”€β”€ config
β”‚   β”‚   β”œβ”€β”€ controller
β”‚   β”‚   β”œβ”€β”€ grpc.go
β”‚   β”‚   β”œβ”€β”€ options
β”‚   β”‚   β”œβ”€β”€ router.go
β”‚   β”‚   β”œβ”€β”€ run.go
β”‚   β”‚   β”œβ”€β”€ server.go
β”‚   β”‚   β”œβ”€β”€ service
β”‚   β”‚   └── store
β”‚   β”œβ”€β”€ authzserver
β”‚   β”‚   β”œβ”€β”€ app.go
β”‚   β”‚   β”œβ”€β”€ config
β”‚   β”‚   β”œβ”€β”€ options
β”‚   β”‚   β”œβ”€β”€ run.go
β”‚   β”‚   └── server.go
β”‚   β”œβ”€β”€ pkg
β”‚   β”‚   β”œβ”€β”€ code
β”‚   β”‚   β”œβ”€β”€ errors
β”‚   β”‚   β”œβ”€β”€ logger
β”‚   β”‚   β”œβ”€β”€ middleware
β”‚   β”‚   β”œβ”€β”€ options
β”‚   β”‚   β”œβ”€β”€ response
β”‚   β”‚   β”œβ”€β”€ server
β”‚   β”‚   β”œβ”€β”€ util
β”‚   β”‚   └── validation
β”‚   β”œβ”€β”€ proto
β”‚   β”‚   β”œβ”€β”€ options
β”‚   β”‚   └── user
β”‚   └── user_service
β”‚       β”œβ”€β”€ app.go
β”‚       β”œβ”€β”€ config
β”‚       β”œβ”€β”€ doc.go
β”‚       β”œβ”€β”€ grpc.go
β”‚       β”œβ”€β”€ options
β”‚       β”œβ”€β”€ router.go
β”‚       β”œβ”€β”€ run.go
β”‚       β”œβ”€β”€ server.go
β”‚       β”œβ”€β”€ service
β”‚       └── store
β”œβ”€β”€ logs
β”‚   β”œβ”€β”€ apiserver.error.log
β”‚   β”œβ”€β”€ apiserver.log
β”‚   └── user_service
β”‚       β”œβ”€β”€ user.error.log
β”‚       └── user.log
β”œβ”€β”€ pkg
β”‚   β”œβ”€β”€ app
β”‚   β”‚   β”œβ”€β”€ app.go
β”‚   β”‚   β”œβ”€β”€ cmd.go
β”‚   β”‚   β”œβ”€β”€ config.go
β”‚   β”‚   β”œβ”€β”€ help.go
β”‚   β”‚   └── option.go
β”‚   β”œβ”€β”€ cache
β”‚   β”‚   β”œβ”€β”€ cache.go
β”‚   β”‚   └── redis.go
β”‚   β”œβ”€β”€ cli
β”‚   β”œβ”€β”€ db
β”‚   β”‚   β”œβ”€β”€ mysql.go
β”‚   β”‚   └── plugin.go
β”‚   β”œβ”€β”€ log
β”‚   β”‚   β”œβ”€β”€ context.go
β”‚   β”‚   β”œβ”€β”€ encoder.go
β”‚   β”‚   β”œβ”€β”€ log.go
β”‚   β”‚   β”œβ”€β”€ options.go
β”‚   β”‚   └── types.go
β”‚   β”œβ”€β”€ shutdown
β”‚   β”‚   β”œβ”€β”€ managers
β”‚   β”‚   └── shutdown.go
β”‚   β”œβ”€β”€ util
β”‚   β”‚   β”œβ”€β”€ common
β”‚   β”‚   β”œβ”€β”€ flag
β”‚   β”‚   └── term
β”‚   └── validator
β”œβ”€β”€ test
β”‚   └── ratelimiter-test.sh
β”œβ”€β”€ third_party
└── tools

61 directories, 64 files

Setup and Running Http REST Server

  1. Building the API server: Navigate to the API server directory:

    cd /Users/huanghaitao/gotal/cmd/apiserver
    

    Build the API server:

    go build
    
  2. Starting the Server: Run the API server with the specified configuration:

    ./apiserver --config ../../configs/apiserver.yaml
    

    This will initialize the server with various configurations as shown in your provided start-up log.

Full Configuration for the GoTAL API Server

After the section on starting the server, the following detailed configurations are applied:

RESTful Service Configuration

server:
  mode: debug # Modes: release, debug, test. Default is release.
  healthz: true # Enable health check, setting up /healthz route. Default is true.
  middlewares: recovery,logger,secure,nocache,cors,dump # List of gin middlewares.

gRPC Service Configuration

grpc:
  bind-address: 0.0.0.0 # IP address for gRPC. Default is 0.0.0.0.
  bind-port: 8082 # Port for gRPC. Default is 8081.

HTTP Configuration (Insecure)

insecure:
  bind-address: 0.0.0.0 # IP address for insecure binding. Default is 127.0.0.1.
  bind-port: 8884 # Non-secure port. Default is 8080.

HTTPS Configuration (Secure)

secure:
  bind-address: 0.0.0.0 # IP address for HTTPS. Default is 0.0.0.0.
  bind-port: 8445 # Port for HTTPS. Default is 8443.
  tls:
    cert-key:
      cert-file: /Users/huanghaitao/gotal/cert/apiserver.pem # Certificate file path.
      private-key-file: /Users/huanghaitao/gotal/cert/apiserver-key.pem # Private key file path.

MySQL Database Configuration

mysql:
  host: 127.0..0.1 # MySQL server address.
  username: root # MySQL username.
  password:  # MySQL password.
  database: db # Database name.
  max-idle-connections: 100 # Max idle connections.
  max-open-connections: 100 # Max open connections.
  max-connection-life-time: 10s # Connection lifetime.
  log-level: 4 # Log level.

Redis Configuration

redis:
  host: 127.0.0.1 # Redis host.
  port: 6379 # Redis port.
  password:  # Redis password.
  # Additional configuration details can be specified here.

JWT Configuration

jwt:
  realm: JWT # JWT realm identifier.
  key:  # Secret key.
  timeout: 24h # Token expiration time.
  max-refresh: 24h # Token refresh time.

Feature Configuration

feature:
  enable-metrics: true # Enable metrics at /metrics.
  profiling: true # Enable performance analysis at /debug/pprof/.

Rate Limiting Configuration

ratelimit:
  requests-per-second: 1 # Requests per second per user.
  burst-size: 20 # Maximum burst size.
  custom-limits:
    "/test-response":
      requests-per-second: 1.5 # Requests per second for specific endpoint.
      burst-size: 10 # Burst size for specific endpoint.

Logging Configuration

log:
  name: apiserver # Logger name.
  development: true # Development mode.
  level: debug # Log level.
  format: console # Log format.
  enable-color: true # Color output.
  disable-caller: false # Caller information.
  disable-stacktrace: false # Stack trace.
  output-paths: /Users/huanghaitao/gotal/logs/apiserver.log # Output paths.
  error-output-paths: /Users/huanghaitao/gotal/logs/apiserver.error.log # Error log paths.

This detailed configuration will ensure that your GoTAL API server is set up with the specific settings required for its operation. These settings include server modes, service bindings, database connections, logging, and more, ensuring a comprehensive and robust setup for your enterprise-grade application.

Configuration

The server's behavior is controlled by various flags and options. These include but are not limited to:

  • gRPC Configurations: Address, port, message size limits.
  • MySQL and Redis Configurations: Host, port, authentication, pooling settings.
  • JWT Settings: Key, timeout, refresh settings.
  • Server Mode: Including debug, test, and release modes.

Detailed flag descriptions are available in the start-up log section of this README.

Architecture Overview

The project is structured into several layers to promote separation of concerns and maintainability:

  1. Controller Layer: Handles HTTP requests, invoking the appropriate services.
  2. Service Layer: Contains business logic and interacts with the repository layer.
  3. Repository Layer: Responsible for data access and storage management.

Key Components

  • Cobra & Viper: Used for building CLI and managing configuration files.
  • Validator: Ensures that incoming requests meet the defined constraints.
  • Middleware: Includes authentication, logging, CORS, rate limiting, etc.
  • Logging: Structured logging for tracing and monitoring.
  • MySQL & Redis: Used for data storage and caching.

Contribution

Refer to CONTRIBUTING.md for guidelines on how to contribute to this project.

License

This project is licensed under the terms mentioned in LICENSE.

Changelog

For a detailed changelog, see CHANGELOG.


This README provides a basic overview of GoTAL. For detailed documentation, refer to the docs directory.

Releases

No releases published

Packages

No packages published

Languages