Skip to content

Commit 42f5a14

Browse files
committed
feat: auto update swagger documentation host and port
1 parent 613d429 commit 42f5a14

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ graph:
5353

5454
# delete the templates code start
5555
.PHONY: docs
56-
# Generate swagger docs, only for ⓵ Web services created based on sql
56+
# Generate swagger docs, only for ⓵ Create web server based on sql
5757
docs:
58-
@bash scripts/swag-docs.sh $(HOST)
58+
@bash scripts/swag-docs.sh
5959
# delete the templates code end
6060

6161
.PHONY: proto

Makefile-for-http

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ graph:
3636

3737

3838
.PHONY: docs
39-
# Generate swagger docs, only for ⓵ Web services created based on sql
39+
# Generate swagger docs, only for ⓵ Create web server based on sql
4040
docs:
41-
@bash scripts/swag-docs.sh $(HOST)
41+
@bash scripts/swag-docs.sh
4242

4343

4444
.PHONY: build

scripts/parseYaml.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
# Usage: ./yamlParser.sh <yaml_file> <key_path>
3+
# Example: ./yamlParser.sh config.yaml '.http.tls.keyFile'
4+
5+
yaml_file="$1"
6+
key_path="$2"
7+
8+
if [[ -z "$yaml_file" || -z "$key_path" ]]; then
9+
echo "Usage: $0 <yaml_file> <key_path>"
10+
exit 1
11+
fi
12+
13+
if [[ ! -f "$yaml_file" ]]; then
14+
echo "File not found: $yaml_file"
15+
exit 1
16+
fi
17+
18+
key_path="${key_path#.}"
19+
keys_for_awk=$(echo "$key_path" | tr '.' ' ')
20+
21+
awk -v keys="$keys_for_awk" '
22+
BEGIN {
23+
# Split the input key path string into an array
24+
split(keys, path, " ")
25+
path_len = length(path)
26+
current_level = 1
27+
# indent_levels array stores the indentation length for each level
28+
# Set indent_levels[0] to -1 to prevent errors in the first level check
29+
indent_levels[0] = -1
30+
}
31+
32+
# Skip empty lines or lines that are purely comments
33+
/^[[:space:]]*#|^[[:space:]]*$/ { next }
34+
35+
{
36+
# Match indentation at the start of the line and the key
37+
# m[1] is the indentation, m[2] is the key
38+
if (match($0, /^([[:space:]]*)([^:]+):/, m)) {
39+
indent = length(m[1])
40+
key = m[2]
41+
42+
# Adjust current level by comparing current indentation with the previous level
43+
# If current indent is less than or equal to previous level, step back in hierarchy
44+
while (indent <= indent_levels[current_level - 1]) {
45+
current_level--
46+
}
47+
48+
# If the key on the current line matches the key in the path we are looking for
49+
if (key == path[current_level]) {
50+
# Record the indentation of the current level
51+
indent_levels[current_level] = indent
52+
53+
# If we have matched the last level of the path, extract and process its value
54+
if (current_level == path_len) {
55+
# Extract everything after the colon as the initial value
56+
value = $0
57+
sub(/^[^:]+:[[:space:]]*/, "", value)
58+
59+
# --- Clean the value ---
60+
# 1. Remove comments at the end of the line
61+
sub(/[[:space:]]*#.*$/, "", value)
62+
# 2. Remove all leading/trailing whitespace
63+
gsub(/^[[:space:]]+|[[:space:]]+$/, "", value)
64+
# 3. Remove double quotes surrounding the value
65+
gsub(/^"|"$/, "", value)
66+
67+
print value
68+
exit
69+
}
70+
71+
# If not the last level, increment level and continue matching downwards
72+
current_level++
73+
}
74+
}
75+
}
76+
' "$yaml_file"

scripts/swag-docs.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/bash
22

3-
HOST_ADDR=$1
4-
53
function checkResult() {
64
result=$1
75
if [ ${result} -ne 0 ]; then
@@ -14,12 +12,26 @@ go mod tidy
1412
checkResult $?
1513
gofmt -s -w .
1614

17-
# change host addr
18-
if [ "X${HOST_ADDR}" = "X" ];then
19-
HOST_ADDR=$(cat cmd/serverNameExample_mixExample/main.go | grep "@host" | awk '{print $3}')
20-
HOST_ADDR=$(echo ${HOST_ADDR} | cut -d ':' -f 1)
21-
else
22-
sed -i "s/@host .*:8080/@host ${HOST_ADDR}:8080/g" cmd/serverNameExample_mixExample/main.go
15+
# get config address from serverNameExample_mixExample.yml
16+
configHost=$(bash scripts/parseYaml.sh configs/serverNameExample_mixExample.yml '.app.host')
17+
if [ "${configHost}" = "127.0.0.1" ]; then
18+
configHost="localhost"
19+
fi
20+
configPort=$(bash scripts/parseYaml.sh configs/serverNameExample_mixExample.yml '.http.port')
21+
configAddr="${configHost}:${configPort}"
22+
enableMode=$(bash scripts/parseYaml.sh configs/serverNameExample_mixExample.yml '.http.tls.enableMode')
23+
schemes="http"
24+
if [ "$enableMode" != "" ]; then
25+
schemes="https"
26+
fi
27+
28+
# get swagger address from main.go
29+
swaggerAddr=$(grep -E '^[[:space:]]*//[[:space:]]*@host' "cmd/serverNameExample_mixExample/main.go" | awk '{print $3}')
30+
if [[ -z "${swaggerAddr}" ]]; then
31+
swaggerAddr="localhost:8080"
32+
fi
33+
if [ "${configAddr}" != "${swaggerAddr}" ];then
34+
sed -i "s/${swaggerAddr}/${configAddr}/g" cmd/serverNameExample_mixExample/main.go
2335
fi
2436

2537
# generate api docs
@@ -38,7 +50,7 @@ highBright='\033[1m'
3850
markEnd='\033[0m'
3951

4052
echo ""
41-
echo -e "${highBright}Tip:${markEnd} execute the command ${colorCyan}make run${markEnd} and then visit ${colorCyan}http://${HOST_ADDR}:8080/swagger/index.html${markEnd} in your browser."
53+
echo -e "${highBright}Tip:${markEnd} start the service with ${colorCyan}make run${markEnd}, and open ${colorCyan}${schemes}://${configAddr}/swagger/index.html${markEnd} to explore the Swagger API docs."
4254
echo ""
4355
echo -e "${colorGreen}generated api docs done.${markEnd}"
4456
echo ""

0 commit comments

Comments
 (0)