Skip to content

Commit

Permalink
update: modify docker relevant files
Browse files Browse the repository at this point in the history
  • Loading branch information
dbbDylan committed Nov 28, 2024
1 parent c514c78 commit 7ea4de6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUN CGO_ENABLED=0 go build -o oauth2playground .


# 阶段2:运行
FROM alpine:3.18
FROM alpine:latest

# 设置工作目录并复制二进制文件
WORKDIR /app
Expand Down
8 changes: 4 additions & 4 deletions cfg-docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
},
"endpoints": {
"authorization": "http://localhost:8081/oauth2/authorize",
"device_authorization": "http://oauth-server-lite:8081/oauth2/device/authorize",
"token": "http://oauth-server-lite:8081/oauth2/token",
"userinfo": "http://oauth-server-lite:8081/oauth2/userinfo"
"device_authorization": "http://localhost:8081/oauth2/device/authorize",
"token": "http://localhost:8081/oauth2/token",
"userinfo": "http://localhost:8081/oauth2/userinfo"
},
"iplimit": {
"enable": false,
Expand All @@ -20,7 +20,7 @@
"cors": ["localhost", "127.0.0.1"],
"listen": "0.0.0.0:80"
},
"trust_domain": ["localhost", "oauth-server-lite:8081"],
"trust_domain": ["localhost", "localhost:8081"],
"default_scope": "Basic",
"timeout": 10
}
2 changes: 1 addition & 1 deletion cfg.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"cors": ["localhost", "127.0.0.1"],
"listen": "0.0.0.0:80"
},
"trust_domain": ["oauth.example.org", "localhost:8081"],
"trust_domain": ["localhost", "oauth.example.org"],
"default_scope": "Basic",
"timeout": 10
}
17 changes: 14 additions & 3 deletions docker-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# 构建方式: 本地构建 or 多平台构建打包上传镜像
BUILD_MODE="local" # local / remote

# DockerHub 用户名、镜像名称和版本号
USERNAME="ecnunic"
IMAGE_NAME="open-oauth2playground"
Expand All @@ -13,8 +16,16 @@ FULL_TAG="${USERNAME}/${IMAGE_NAME}:${VERSION}"

echo "Building ${FULL_TAG} for platforms ${PLATFORMS}..."

# 推送到远程镜像仓库
docker buildx build \
if [ "${BUILD_MODE}" == "remote" ]; then
# 推送到远程镜像仓库
docker buildx build \
--platform "${PLATFORMS}" \
-t "${FULL_TAG}" \
--push .
--push .
elif [ "${BUILD_MODE}" == "local" ]; then
# 本地构建
docker build --no-cache --load -t "${FULL_TAG}" .
else
# Unknown $BUILD_MODE
echo "BUILD_MODE must be \`local\` or \`remote\`, but got \`${BUILD_MODE}\`"
fi
58 changes: 32 additions & 26 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
# docker-compose -p oauth-server-lite up -d
# 启动命令: docker-compose -p open-oauth2playground up -d
# -p [service name] 其中 server_name 可以替换为任意的服务名称

services:
# container model: 容器模式。由 open-oauth2playground 容器网络统一暴露端口
# container 模式下的容器无法暴露自身端口

redis:
image: redis:alpine
container_name: oauth-redis
ports:
- "6379:6379"
networks:
- oauth-server-lite
services:

open-oauth2playground:
image: ecnunic/open-oauth2playground:v0.2.0
container_name: open-oauth2playground
environment:
- PLAYGROUND_PORT=80
- OAUTH_SERVER_PORT=8081
- OAUTH_SERVER_HOST=oauth-server-lite
- OAUTH_SERVER_URL=http://oauth-server-lite:8081
restart: always
- PLAYGROUND_PORT=80 # oauth2playground 服务端口号
- PLAYGROUND_HOST=localhost # oauth2playground 服务地址/域名 (用于配置域名解析)
- CAS_SERVER_HOST=localhost # apereo-cas 服务地址/域名 (用于配置域名解析)
- OAUTH_SERVER_PORT=8081 # oauth-server-lite 服务端口号
- OAUTH_SERVER_HOST=localhost # oauth-server-lite 服务地址/域名 (用于服务通信 && 配置域名解析)
- OAUTH_SERVER_URL=http://localhost:8081 # oauth-server-lite 服务 URL
ports:
- "80:80"
- "80:80" # open-oauth2playground 端口
# - "6379:6379" # redis 端口
- "8080:8080" # apereo-cas 服务占用 1
- "8081:8081" # oauth-server-lite 前端服务
- "8444:8444" # apereo-cas 服务占用 2
networks:
- oauth-server-lite
- open-oauth2playground
# volumes:
# - ./oauth-server-lite/oauth2playground/cfg.json:/app/cfg.json
# - ./oauth-server-lite/oauth2playground/logs:/app/logs
restart: always

redis:
image: redis:alpine
container_name: oauth-redis
depends_on:
- open-oauth2playground
network_mode: container:open-oauth2playground
restart: always

oauth-server-lite:
image: ecnunic/oauth-server-lite:v0.3.0
Expand All @@ -35,30 +45,26 @@ services:
- OAUTH_CLIENT_SECRET=123456 # OAuth2 Client Secret
- CAS_USERNAME=cas # CAS 用户名
- CAS_PASSWORD=123456 # CAS 用户密码
- OAUTH_SERVER_PORT=8081 # oauth-server-lite 后端服务端口号
- OAUTH_SERVER_PORT=8081 # oauth-server-lite 服务端口号
- CAS_SERVER_PORT=8444 # apereo-cas 服务端口号
- CAS_SERVER_HOST=localhost # apereo-cas 服务地址/域名
- CAS_SERVER_URL=http://localhost:8444 # apereo-cas 服务 URL
- OAUTH_REDIS_DSN=redis:6379 # redis 服务域名
- OAUTH_REDIS_DSN=localhost:6379 # redis 服务地址/域名
- OAUTH_REDIS_PASSWORD= # redis 服务连接密码
- PLAYGROUND_HOST=localhost # oauth2playground 服务地址/域名
depends_on:
- open-oauth2playground
- redis
restart: always
ports:
- "8080:8080" # apereo-cas 服务占用 1
- "8081:8081" # oauth-server-lite 前端服务
- "8444:8444" # apereo-cas 服务占用 2
networks:
- oauth-server-lite
network_mode: container:open-oauth2playground
# volumes:
# - ./oauth-server-lite/apereo-cas/cas.db:/app/apereo-cas/cas.db # apereo-cas 用户信息 sqlite 数据库
# - ./oauth-server-lite/apereo-cas/config:/etc/cas/config # apereo-cas 配置信息
# - ./oauth-server-lite/apereo-cas/services:/etc/cas/services # apereo-cas 服务配置
# - ./oauth-server-lite/oauth-server-lite/sqlite.db:/app/oauth-server-lite/sqlite.db # oauth-server-lite 认证信息 sqlite 数据库
# - ./oauth-server-lite/oauth-server-lite/cfg.json:/app/oauth-server-lite/cfg.json # oauth-server-lite 配置信息
# - ./oauth-server-lite/oauth-server-lite/logs:/app/oauth-server-lite/logs # oauth-server-lite 日志
restart: always

networks:
oauth-server-lite:
open-oauth2playground:
driver: bridge
40 changes: 39 additions & 1 deletion start-services.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ PLAYGROUND_CONFIG_FILE="${PATH_ROOT}/cfg.json"

# 可对外暴露的环境变量
PLAYGROUND_PORT=${PLAYGROUND_PORT:-"80"} # oauth2playground 服务端口号
PLAYGROUND_HOST=${PLAYGROUND_HOST:-"localhost"} # oauth2playground 服务地址/域名
CAS_SERVER_HOST=${CAS_SERVER_HOST:-"localhost"} # apereo-cas 服务地址/域名
OAUTH_SERVER_PORT=${OAUTH_SERVER_PORT:-"8081"} # oauth-server-lite 服务端口号
OAUTH_SERVER_HOST=${OAUTH_SERVER_HOST:-"oauth-server-lite"} # oauth-server-lite 服务域名
OAUTH_SERVER_HOST=${OAUTH_SERVER_HOST:-"localhost"} # oauth-server-lite 服务地址/域名
OAUTH_SERVER_URL=${OAUTH_SERVER_URL:-"http://${OAUTH_SERVER_HOST}:${OAUTH_SERVER_PORT}"} # oauth-server-lite 服务 URL

# ========================
Expand All @@ -41,9 +43,44 @@ configure_oauth2_playground() {
.http.listen = "0.0.0.0:\($port)"
' "$PLAYGROUND_CONFIG_FILE" > "$PLAYGROUND_CONFIG_FILE.tmp" && mv "$PLAYGROUND_CONFIG_FILE.tmp" "$PLAYGROUND_CONFIG_FILE"

# 仅在 trust_domain 中不存在时追加新值
jq --arg new_domain "${OAUTH_SERVER_URL}" '
if .trust_domain | index($new_domain) == null then
.trust_domain += [$new_domain]
else
.
end
' "$PLAYGROUND_CONFIG_FILE" > "$PLAYGROUND_CONFIG_FILE.tmp" && mv "$PLAYGROUND_CONFIG_FILE.tmp" "$PLAYGROUND_CONFIG_FILE"

echo "OAuth2 Playground configured successfully!"
}

configure_domain_parser() {
echo "Configuring domain parser..."

# 检查并添加 PLAYGROUND_DOMAIN 的解析
if [ "${PLAYGROUND_HOST}" != "localhost" ] && [ "${PLAYGROUND_HOST}" != "127.0.0.1" ]; then
if ! grep -q "${PLAYGROUND_HOST}" /etc/hosts; then
echo "127.0.0.1 ${PLAYGROUND_HOST}" >> /etc/hosts
echo "Added DNS resolution for PLAYGROUND_HOST: ${PLAYGROUND_HOST}"
else
echo "DNS resolution for PLAYGROUND_HOST already exists: ${PLAYGROUND_HOST}"
fi
fi

# 检查并添加 OAUTH_SERVER_DOMAIN 的解析
if [ "${OAUTH_SERVER_HOST}" != "localhost" ] && [ "${OAUTH_SERVER_HOST}" != "127.0.0.1" ]; then
if ! grep -q "${OAUTH_SERVER_HOST}" /etc/hosts; then
echo "127.0.0.1 ${OAUTH_SERVER_HOST}" >> /etc/hosts
echo "Added DNS resolution for OAUTH_SERVER_HOST: ${OAUTH_SERVER_HOST}"
else
echo "DNS resolution for OAUTH_SERVER_HOST already exists: ${OAUTH_SERVER_HOST}"
fi
fi

echo "Domain parser configuration completed!"
}

# 启动 OAuth2 Playground 服务
start_oauth2_playground() {
echo "Starting OAuth2 Playground..."
Expand All @@ -55,6 +92,7 @@ start_oauth2_playground() {
# 主执行流程
# ========================
configure_oauth2_playground
configure_domain_parser
start_oauth2_playground

# 保持脚本运行
Expand Down

0 comments on commit 7ea4de6

Please sign in to comment.