This guide will mainly introduce the project's building and startup processes, as well as related scripts.
Subsequent <target> refers to a certain service, such as api. You can specifically obtain the list of buildable services through make help.
- Use the command
make env-upto start the environment (MySQL, etcd, Redis, etc.). make <target>compiles and runs the specific service.- The service retrieves
config.yamlfrom etcd. - Reads the configuration in
config.yamland maps the Env to the corresponding struct. - Obtains the available address from
config.yaml. - Initializes the service and registers the service in
etcd. - Starts the service.
sequenceDiagram
participant User as User
participant Makefile as Makefile
participant Services as Service
participant Env as Environment (MySQL, etcd, Redis)
User->>Makefile: Run `make env-up`
Makefile->>Env: Start MySQL, Redis, and etcd
User->>Makefile: Run `make <target>`
Makefile->>Services: Compile and start the specified service
Services->>Env: Get `config.yaml` from etcd
Env-->>Services: Return `config.yaml`
Services->>Services: Read `config.yaml` and map it to the struct, and obtain the available IP address
Services->>Env: Register the service in etcd
Services->>Services: Initialization completed, start the service
The key directories of the project are as follows:
cmd/: Contains the startup entry points of each service module.output/: The output directory for build products.
Here we explain the specific workflow when we type make <target>. We omit the content related to the tmux environment.
The building process is mainly completed through the build.sh script, which is used to compile the binary files of the specified service module or conduct system tests:
- Enter the corresponding service folder in
cmd. - Execute
go buildto compile the binary file of this service and store it in theoutputfolder.
flowchart TD
A[Start script] --> B{Check if a parameter, i.e., target, is input}
B --> |Empty| C[Output an error message and exit]
B --> |Not empty| D[Set ROOT_DIR as the current working directory]
D --> E[Enter the specified module directory ./cmd/RUN_NAME]
E --> F[Create the folder output/RUN_NAME and set permissions]
F --> G{Determine if it is a test environment}
G --> |It is a test environment| H[Execute the test build]
G --> |Not a test environment| I[Execute the build]
H --> J[Generate the test binary file output/RUN_NAME/fzuhelper-RUN_NAME]
I --> K[Generate the build binary file output/RUN_NAME/fzuhelper-RUN_NAME]
output
└── target
└── binary
When we type make <target> without setting the build-only flag (BUILD_ONLY), it will start automatically. Here we introduce the process of local debugging startup.
The startup process of Docker containers is similar, except that it is moved into the container.
The startup process is mainly completed through the entrypoint.sh script.
- Use
exportto set the environment variable of the etcd address, so that the subsequent program can obtain the etcd address during runtime and getconfig.yaml. cdto theoutputdirectory generated during the build stage and execute the binary file of the corresponding service.
flowchart TD
A[Start entrypoint.sh] --> B{Check if ETCD_ADDR is set}
B --> |Not set| C[Set the default ETCD_ADDR=localhost:2379]
B --> |Set| D[Keep the existing ETCD_ADDR]
C --> E[Export the ETCD_ADDR environment variable]
D --> E
E --> F[Start the service]
Both scripts are managed by the commands in Makefile and can be called through the following commands:
make <target> [option] # option = BUILD_ONLYThe following is a rough flowchart of make <target>:
flowchart TD
A[Start the make <target> command] --> B{Check if the BUILD_ONLY setting is passed in}
B -- Not set --> C[Build and run]
B -- Set --> D[Only build]
D --> E[Create the output directory]
E --> F[Run the build.sh script for compilation]
C --> G[Create the output directory]
G --> H[Run the build.sh script for compilation]
H --> I[Run the entrypoint.sh to start the service]
I --> J[The service is successfully started]