first commit

This commit is contained in:
2022-03-17 15:59:24 +08:00
commit 2b0debb847
592 changed files with 73946 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
#!/bin/bash
file_path=$(
cd "$(dirname "$0")" || exit
pwd
)/../..
# shellcheck source=./util.sh
source "${file_path}"/script/build/util.sh
env_type="$1"
initOS ${env_type}
os=$(initOS ${env_type})
api_version=""
project_name="dtalk_service"
now_time=$(date "+%Y_%m_%d")
pkg_name="${project_name}_${now_time}_${os}"
echo ${pkg_name}
mkPkg "${file_path}" "${pkg_name}"
case "${api_version}" in
*)
buildGateway "${file_path}" "${pkg_name}" "gateway" "v1"
buildService "${file_path}" "${pkg_name}" "backend" "${api_version}"
buildService "${file_path}" "${pkg_name}" "backup" "${api_version}"
buildService "${file_path}" "${pkg_name}" "call" "${api_version}"
buildService "${file_path}" "${pkg_name}" "discovery" "${api_version}"
buildService "${file_path}" "${pkg_name}" "generator" "${api_version}"
buildService "${file_path}" "${pkg_name}" "group" "${api_version}"
buildService "${file_path}" "${pkg_name}" "offline-push" "${api_version}"
buildService "${file_path}" "${pkg_name}" "oss" "${api_version}"
buildService "${file_path}" "${pkg_name}" "answer" "${api_version}" "record/"
buildService "${file_path}" "${pkg_name}" "pusher" "${api_version}" "record/"
buildService "${file_path}" "${pkg_name}" "store" "${api_version}" "record/"
# tarPkg "${file_path}" "${pkg_name}" "multi-service"
;;
esac

View File

@@ -0,0 +1,67 @@
#!/bin/bash
file_path=$(
cd "$(dirname "$0")" || exit
pwd
)/../..
# shellcheck source=./util.sh
source "${file_path}"/script/build/util.sh
quickBuildService() {
file_path="$1"
pkg_path="$2"
service_name="$3"
sub_file_path="$4"
flags=$(getFlags)
cd "${file_path}/service/${sub_file_path}${service_name}/cmd" || exit
echo "┌ start building ${service_name} service"
go build -ldflags "${flags}" -o "${file_path}/${pkg_path}/${service_name}" || exit
echo "└ building ${service_name} service success"
}
quickBuildGateway() {
file_path="$1"
pkg_name="$2"
service_name="$3"
sub_file_path="$4"
flags=$(getFlags)
cd "${file_path}/gateway/api/${sub_file_path}" || exit
echo "┌ start building ${service_name} service"
go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/${service_name}" || exit
echo "└ building ${service_name} service success"
}
mkDir() {
file_path="$1"
pkg_path="$2"
rm -rf ${file_path}/${pkg_path}
mkdir -pv "${file_path}/${pkg_path}"
}
env_type="$1"
initOS ${env_type}
os=$(initOS ${env_type})
api_version=""
project_name="dtalk"
now_time=$(date "+%Y_%m_%d")
pkg_path="${project_name}_bin_${now_time}_${os}"
mkDir "${file_path}" "${pkg_path}"
quickBuildGateway "${file_path}" "${pkg_path}" "gateway" "v1"
quickBuildService "${file_path}" "${pkg_path}" "backend"
quickBuildService "${file_path}" "${pkg_path}" "backup"
quickBuildService "${file_path}" "${pkg_path}" "call"
quickBuildService "${file_path}" "${pkg_path}" "discovery"
quickBuildService "${file_path}" "${pkg_path}" "generator"
quickBuildService "${file_path}" "${pkg_path}" "group"
quickBuildService "${file_path}" "${pkg_path}" "offline-push"
quickBuildService "${file_path}" "${pkg_path}" "oss"
quickBuildService "${file_path}" "${pkg_path}" "answer" "record/"
quickBuildService "${file_path}" "${pkg_path}" "pusher" "record/"
quickBuildService "${file_path}" "${pkg_path}" "store" "record/"

131
script/build/util.sh Normal file
View File

@@ -0,0 +1,131 @@
#!/bin/bash
# 检查服务名称是否合法
checkValid() {
valid_services=("backend")
#"apply" "department" "enterprise" "staff")
service_name="$1"
is_valid="no"
for valid_service in "${valid_services[@]}"; do
if [ "${valid_service}" == "${service_name}" ]; then
is_valid="yes"
fi
done
if [ "${is_valid}" == "no" ]; then
return 1
else
return 0
fi
}
# 获取编译参数
getFlags() {
main_path="main"
go_version=$(go version | awk '{ print $3 }')
build_time=$(date "+%Y-%m-%d %H:%M:%S %Z")
git_commit=$(git rev-parse --short=10 HEAD)
flags="-X '${main_path}.goVersion=${go_version}' -X '${main_path}.buildTime=${build_time}' -X '${main_path}.gitCommit=${git_commit}' -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"
echo "${flags}"
}
# 创建目标目录
mkPkg() {
file_path="$1"
pkg_name="$2"
rm -rf ${file_path}/${pkg_name}
mkdir -pv "${file_path}/${pkg_name}"/bin
mkdir -pv "${file_path}/${pkg_name}"/etc
}
# 编译服务
buildService() {
file_path="$1"
pkg_name="$2"
service_name="$3"
version="$4"
sub_file_path="$5"
flags=$(getFlags)
suffix=""
if [ -n "${version}" ]; then
suffix="-${version}"
fi
cd "${file_path}"/service/"${sub_file_path}""${service_name}"/cmd || exit
echo "start building ${service_name} service"
# echo ${flags}
# go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/bin/${service_name}${suffix}" || exit
go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/bin/${service_name}${suffix}" || exit
echo "building ${service_name} service success"
cp "${file_path}/service/${sub_file_path}${service_name}/config/${service_name}".toml "${file_path}/${pkg_name}/etc/${service_name}".toml
}
# 编译gateway服务
buildGateway() {
file_path="$1"
pkg_name="$2"
service_name="$3"
version="$4"
flags=$(getFlags)
suffix=""
if [ -n "${version}" ]; then
suffix="-${version}"
fi
cd "${file_path}/gateway/api/${version}" || exit
echo "start building ${service_name} service"
go build -ldflags "${flags}" -o "${file_path}/${pkg_name}/bin/${service_name}" || exit
echo "building ${service_name} service success"
cp "${file_path}/gateway/api/${version}/etc/${service_name}".toml "${file_path}/${pkg_name}/etc/${service_name}".toml
}
# 复制所选文件
cpIfExist() {
file_path="$1"
pkg_name="$2"
files="$3"
for file in "${file_path}"/configs/${files}; do
if [ -f "${file}" ]; then
cp "${file}" "${file_path}/${pkg_name}"/etc
fi
done
}
# 打包目标目录
tarPkg() {
file_path="$1"
pkg_name="$2"
file_name="$3"
is_single_service="$4"
# if [ "${is_single_service}" == "yes" ]; then
# content="$(cat "${file_path}"/configs/Makefile-single-service)"
# echo "${content//single/${file_name}}" > "${file_path}/${pkg_name}"/Makefile
# else
# cp "${file_path}/configs/Makefile-${file_name}" "${file_path}/${pkg_name}"/Makefile
# fi
# cp "${file_path}"/README.md "${file_path}/${pkg_name}"
tar -zcvPf "${file_path}/${pkg_name}".tar.gz -C "${file_path}" "${pkg_name}"/etc "${pkg_name}"/bin
# "${pkg_name}"/Makefile
# rm -r "${file_path}/${pkg_name}"
}
# 设置目标打包环境
# 默认 linux amd64
initOS() {
env_type="amd64"
if [ -n "$1" ]; then
env_type="$1"
fi
export GOOS=linux
export GOARCH=${env_type}
export GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn
echo "linux_${env_type}"
}