Categorygithub.com/frame-go/framego
modulepackage
0.3.0
Repository: https://github.com/frame-go/framego.git
Documentation: pkg.go.dev

# README

Framego Manual

framego is a versatile and easy-to-use Go library designed for modern backend development. It simplifies the development process by providing a framework and essential libraries for building robust and scalable backend systems, from gRPC APIs, HTTP APIs to job processing.

Key Features

  • Dual Protocol Support: Seamlessly implement gRPC and HTTP APIs. You can easily implement gRPC services and expose corresponding HTTP APIs automatically. Provide a variety of middlewares for gRPC and HTTP services.
  • Job Processing Capabilities: Support background tasks and job processing.
  • Rich Libraries: Come with essential libraries, including configuration management, database/cache/queue clients, in-memory caching solutions, advanced error handling and logging utilities.
  • Ease of Use: Intuitive setup and use, allowing for quick starting your projects.

Repositories

Getting Started

Prerequisites

Ensure you have installed and properly configured these tools in Shell before initializing project:

  • curl
  • git
  • go
  • cookiecutter
  • buf
  • air (optional)

Git

Ensure you have installed and properly setup Git configuration.

Check whether you can directly clone repository from Git remote without input authentication information.

Go

Ensure you have installed and properly setup Golang development environment.

Check whether you can go get modules from private repository in shell.

Cookiecutter

Cookiecutter is a command-line utility that creates projects from project templates.

Cookiecutter is built by Python. You can use pip to install.

pip install cookiecutter

If you haven't installed pip, follow this guide to install.

Buf

Buf is a building tool to make Protobuf reliable and easy to use.

You can follow this guide to install.

Air

Air is a live-reloading command line utility for Go applications in development. It is optional for this framework.

You can follow this guide to install.


curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh

Create Project

Run this command to create a project folder in the current working directory:

cookiecutter https://github.com/frame-go/cookiecutter-framego

You will need to provide the following template variables:

Variable NameDescriptionExample
project_titleProject name, in CapitalizedCase format.
This name will be used in project description and documents.
Suggest to be full name of project.
Identity and Access Management System
project_nameProject name, in kebab-case format.
This name will be used as project root folder name.
iam-server
app_nameApp name, in snake_case format.
This name will be used as an app name.
security_core
app_package_nameApp package name, in flatcase.
This name will be used as project root cmd package name and binary name.
Suggest aligning with app name, using a single word or abbreviation.
securitycore
service_nameService name, in snake_case format.
This name will be used as service name, DB name.
security_core
service_package_nameSedervice package name, in flatcase.
This name will be used as the api package name.
Suggest aligning with service name, using a single word or abbreviation.
securitycore
go_moduleFull go module path.
Suggest to end with project name.
github.com/example/demo-server
go_versionGolang version, will be used in makefile and CI.1.16

Initialize Project

Run this command inside the project folder to initialize and install Golang tools:

make init

Build Project

Generate codes and compile the project:

make

Setup DB

Ensure you have MySQL/MariaDB installed and running locally.

Run this command inside the project folder to create a sample DB and tables and grant permission to the test user:

mysql < db/*.sql
mysql -v -e "CREATE USER 'test'@'127.0.0.1' IDENTIFIED BY 'password'; GRANT ALL ON *.* TO 'test'@'127.0.0.1';"

You can change the DB connection config inside configs/debug/config.yaml.

Run Project

You can run the air command inside the project folder to run your project in live reload mode:

air

Or you can simply run the compiled executable inside the bin folder.

Make Commands

The template provides many useful commands by makefile. You can run make commands in the project folder.

CommandDescription
make initInitialize project and install tools.
make / make allGenerate code and compile project.
Equal to make fmt && make generate && make build.
make buildCompile project.
make cleanClear compiled files.
make go-generateRun go generate to generate go code.
make buf-generateRun buf generate to generate code from protobuf files and export dependencies protobuf files to buf_vendor folder.
make generateEqual to make buf-generate && make go-generate .
make fmtRun go fmt and go vet format and check go code.
make go-lintRun golangci-lint analysis go code.
make buf-lintRun buf lint to analysis protobuf code.
make lintRun all static code analysis. Equal to make fmt && make go-lint && make buf-lint .
make check-go-versionCheck whether golang version matches requirement.

Project Structure

Root folder name: {{project_name}}

PathDescription
cmdMain applications.
Each executable should be in a separated package directory.
It's common to have a small main function that imports and invokes the code from other packages and nothing else.
cmd/{{app_package_name}}/main.goApplication default executable.
apiAPI and protocol definitions and generated codes.
The data definition can be used by domain and handlers package.
api/{{service_package_name}}/{{service_package_name}}.protoService protobuf definition.
buf_vendorProtobuf dependencies.
configsLocal configuration files
configs/debug/config.yamlDefault configuration files for debug
dbDatabase creation and migrations sql files.
docsDocuments for the project.
internalPrivate application and library code.
This is the code you don't want others importing in their applications or libraries.
internal/modelsData models definitions for Data Access layer.
The models can be used by db, cache and domain package.
internal/dbDatabase data access layer. CRUD for database.
internal/cacheCache data access layer. CRUD for in memory cache and distributed cache.
internal/domainBusiness logic layer.
This package contains major business logic, invokes code from db and cache packages, and provides functions for handlers.
internal/handlersPresentation layer. API handlers for HTTP and GRPC interfaces.
This package should only contains data validation and conversion, and invokes code from domain package. This package should not invokes code from models, db or cache packages.
toolsExternal tools used by project.
tools.goDummy go file to include external tool dependencies.
go.modGo module dependencies configuration.
MakefileMake command configuration.
.gitignoreGit ignore file configurations.
.golangci.yamlGolangci-lint configurations.
.dockerignoreDocker ignore file configurations for docker build
DockerfileDocker build configurations.
buf.yamlBuf configurations, includes lint rules and dependencies.
buf.gen.yamlBuf generate configurations.
.air.tomlAir configuration

Configuration

Applications built from the framework accept configurations from command line arguments, environment variables, configuration file, and Apollo.

The priority of them is: command line arguments > environment variables > configuration file > Apollo.

Basic Configurations

Below are basic configuration items supported:

Configuration ItemCommand Line ArgumentEnvironment VariableConfiguration KeyDefault Value
Configuration file path-c, --config-pathCONFIG_PATHN/ANone
Whether to enable debug mode-d, --debugDEBUGdebugfalse
Whether to enable human-friendly, colorized log-b, --beautify-logBEAUTIFY_LOGbeautify_logFALSE
Minimal log level: trace, debug, info, warn, error-l, --log-levelLOG_LEVELlog_leveldebug (if debug==true), info (if debug==false)
Run job-j, --jobJOBjobtask_executor
Apollo server endpoint--apollo-serverAPOLLO_SERVER _APOLLO_SERVER_N/ANone
Apollo app ID--apollo-app-idAPOLLO_APP_ID _APOLLO_APP_ID_N/ANone
Apollo app access key secret--apollo-access-keyAPOLLO_ACCESS_KEY _APOLLO_ACCESS_KEY_N/ANone
Apollo cluster--apollo-clusterAPOLLO_CLUSTER _APOLLO_CLUSTER_N/Adefault
Apollo namespace--apollo-namespaceAPOLLO_NAMESPACE _APOLLO_NAMESPACE_N/Aconfig.yaml

App Configuration

To run the application, app configuration must be provided. It can come from either a local config file or remote config (Apollo).

  • For local config file, the file path must be provided by command line argument (--config-path) or environment variable (CONFIG_PATH). The framework will try to load the local config file if the config path exists.
  • For remote config, the Apollo loading arguments must be provided by command line arguments (--apollo-*) or environment variable (_APOLLO_*_). The framework will try to load Apollo config if Apollo server endpoint and app ID exist.

For the same config key, values in the local config file have higher precedence over remote config.

Besides basic configuration items, you can configure items under app for services and clients configurations.

Below is a sample app configuration config.yaml:

debug: true
beautify_log: true
app:
  name: sample
  observable:
    endpoints:
      http: ":8080"
    modules:
      - pprof
      - metrics
      - swagger
      - channelz
      - grpcui
  services:
    - name: sample
      endpoints:
        grpc: ":9000"
        http: ":8000"
      security:
        grpc:
          key: ./keys/service.pem
          cert: ./keys/service.crt
          ca: ./keys/ca.crt
      middlewares:
        - recovery
        - open_tracing
        - metrics
        - context_logger
        - log_request
        - name: access_control
          policy: ./acl.csv
        - request_validation
  jobs:
    - sample_job
  clients:
    grpc:
      middlewares:
        - open_tracing
        - metrics
      servers:
        - name: auth
          endpoint: "127.0.0.1:9000"
          security:
            key: ./keys/service.pem
            cert: ./keys/service.crt
            ca: ./keys/ca.crt
    databases:
      - name: sample
        database: "sample_db"
        user: "root"
        password: ""
        masters: ["127.0.0.1:3306"]
        slaves: []
    caches:
      - name: sample
        type: redis
        address: "127.0.0.1:6379"
    pulsars:
      - name: sample
        url: "pulsar://127.0.0.1:6650"
        token: "zFbeuKF3jqjfxkQFfOoMeQ"
  id_generator:
    service_id: 1
    key: "c2b4706d47bbddfd6729cb72960c1a3d"

A project is an app, it can has many GRPC/HTTP services under an app, and an optionalobservable service for debug, monitoring, etc. Services are listen on different ports.

Below are configuration items under app.

PathDescriptionExample
nameName of appiam
observableBuilt-in observable services for debug, monitoring, etc.
observable.endpoint.httpsHTTP endpoint for observable service.:8080
observable.modulesEnable built-in observable modules.
Details of available modules refer to below.
- pprof
servicesList of application services.
services[].nameName of serviceiam
services[].endpoint.grpcgRPC endpoint for this service.:9000
services[].endpoint.httpHTTP endpoint for this service.:8000
services[].security.grpcgRPC server TLS configuration.
Optional, accept insecure connection if not configured.
services[].security.grpc.keyTLS server key../keys/service.pem
services[].security.grpc.certTLS server certificate chain../keys/service.crt
services[].security.grpc.caTLS CA for verifying clients certificates../key/ca.crt
services[].middlewaresEnable built-in middlewares/interceptors for HTTP/gRPC service.
Details of available middlewares refer to below.
- recovery
jobs[]Jobs enabled in the app.
Jobs should be registered by App.AddJob().
Only enabled jobs will be run.
- txn_executor
clientsClients of dependent service.
clients.gprcgRPC clients of dependent service.
clients.gprc.middlewaresEnable built-in middlewares/interceptors for all gRPC clients.
Details of available middlewares refer to below.
- metrics
clients.grpc.serversServer of gRPC clients.
clients.grpc.servers[].nameName of gRPC server to fetch the client interface.
This name will also be verified for connection TLS CA is configured.
iam
clients.grpc.servers[].endpointEndpoint of gRPC server.127.0.0.1:9000
clients.grpc.servers[].securitygRPC client TLS configuration.
Optional, use insecure connection if not configured.
clients.grpc.servers[].security.keyTLS client key../keys/service.pem
clients.grpc.servers[].security.certTLS client certificate chain../keys/service.crt
clients.grpc.servers[].security.caTLS CA for verifying server certificates../key/ca.crt
databasesDatabases used by app.
databases[].nameName of database to fetch the client interface.iam
databases[].databaseDatabase schema name.iam_db
databases[].userDatabase user name.test_user
databases[].passwordDatabase passwrod.testpass
databases[].mastersDatabase master endpoints for read-write query.["127.0.0.1"]
databases[].slavesDatabase slave endpoints for readonly query.["10.0.0.1:6606", "10.0.0.2:6606"]
cachesCaches used by app.
caches[].nameName of cache to fetch the client interface.default
caches[].typeCache client type.
Choices: redis
redis
caches[].addressCache server address in <host>:<port> format.127.0.0.1:6379
caches[].usernameOptional. Username for authentication.test_user
caches[].passwordOptional. Username for authentication.testpass
caches[].dbDatabase to be selected after connecting to the server.0
pulsarsPulsar clients
pulsars[].nameName of pulsar server to fetch the client interfaceiam
pulsars[].urlURL of pulsar serverpulsar://10.0.0.1:6650
pulsars[].tokenToken for pulsar auth, optionaleyJhbGciOiJU...
id_generatorID generatior configuration, optional.
id_generator.service_idService ID for unique ID generator.1
id_generator.keyEncrypt key for unique ID generator, 16 bytes, hex encoded.c2b4706d47bbddfd6729cb72960c1a3d

Observable Service Modules

Below are built-in observable service modules:

NameEndpointDescription
pprof/pprofGolang standard profiling tool.
channelz/channelzgRPC Channelz UI.
swagger/swaggerSwagger docs UI.
metrics/metricsPrometheus metrics endpoint for gRPC and HTTP services.
grpcui/grpcuigRPC service interactive UI.

Service Middlewares

Below are built-in middlewares for gRPC/HTTP client/service:

NameDescriptiongRPC ServiceHTTP ServicegRPC Client
recoveryAuto recover from panic in handler and record details in log.YYN
open_tracing(Not implemented yet) Fetch or generate trace id, put in context; generate and pass request ID for sub requests.YYY
metricsRecord request metrics.YYY
context_loggerAdd request metadata into logger and put logger in context.
Context logger can be fetched by log.FromContext in handlers.
YYN
log_requestRecord log for each request, includes metadata, error code, latency, etc.YYN
request_validationValidate request data in gRPC client/service by protoc-gen-validate.YNY
corsHTTP CORS handlingNYN
compressHTTP response compressionNYN
access_controlRequest access control based on gRPC TLS certificate and Casbin configurationYNN

Libraries

errors

fromego/errors module provides useful utilities for error handling, and is compatible with errors module in standard library. Refer to errors document for details.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Package utils provides a set of common utilities, used thoughout the project.

# Functions

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author