本地化部署hoppscotch —— 使用docker compose一鍵部署 + https支援

曾经时光發表於2024-05-03

一、前言

研究了好久,終於完成了hoppscotch本地一鍵部署,並支援https。hoppscotch的介紹這裡就不做過多描述,網路上非常多,這裡先放幾張使用效果

1、api介面

2、管理介面

2、部署效果:

a.使用指令碼,./create_and_start.sh(後面會提供),需要傳入本機ip(能訪問到的),可用的smtp url(建議使用qq郵箱,免費),郵箱名(用於郵件登入時,傳送郵件用的發件者,和smtp url對應上)

b.docker會自動部署 postgres(資料庫),hoppscotch(此api工具),nginx(代理,支援https需要)

二、一鍵部署相關配置

1、hoppscotch配置,hoppscotch.env

a.準備好配置檔案如下,以下為拿官方配置改造出來的

b.本文提供的方式只支援郵箱登入,如果要其它方式,可自行修改hoppscotch.env。具體可參考官方文件。如只需要郵箱登入,可直接使用本文提供的此配置。

 1 #-----------------------Backend Config------------------------------#
 2 # Prisma Config
 3 DATABASE_URL=postgresql://hoppscotch:hoppscotch@postgres:5432/hoppscotch
 4 
 5 # Auth Tokens Config
 6 JWT_SECRET=secret1233
 7 TOKEN_SALT_COMPLEXITY=30
 8 MAGIC_LINK_TOKEN_VALIDITY=30
 9 REFRESH_TOKEN_VALIDITY=604800000
10 ACCESS_TOKEN_VALIDITY=604800000
11 # SESSION_SECRET='add some secret here'
12 SESSION_SECRET=some_secret
13 
14 # Hoppscotch App Domain Config
15 REDIRECT_URL=https://__SELF_HOST_IP__
16 WHITELISTED_ORIGINS=https://__SELF_HOST_IP__,https://__SELF_HOST_IP__/admin,https://__SELF_HOST_IP__/backend
17 VITE_ALLOWED_AUTH_PROVIDERS=EMAIL
18 
19 # Google Auth Config
20 GOOGLE_CLIENT_ID=""
21 GOOGLE_CLIENT_SECRET=""
22 GOOGLE_CALLBACK_URL=""
23 GOOGLE_SCOPE=""
24 
25 # Github Auth Config
26 GITHUB_CLIENT_ID=""
27 GITHUB_CLIENT_SECRET=""
28 GITHUB_CALLBACK_URL=""
29 GITHUB_SCOPE=""
30 
31 # Microsoft Auth Config
32 MICROSOFT_CLIENT_ID=""
33 MICROSOFT_CLIENT_SECRET=""
34 MICROSOFT_CALLBACK_URL=""
35 MICROSOFT_SCOPE=""
36 MICROSOFT_TENANT=""
37 
38 # Mailer config
39 MAILER_SMTP_URL=__MY_SMTP_URL__
40 MAILER_ADDRESS_FROM=__MY_SMTP_USER__
41 
42 # Rate Limit Config
43 RATE_LIMIT_TTL=60 # In seconds
44 RATE_LIMIT_MAX=100 # Max requests per IP
45 
46 
47 #-----------------------Frontend Config------------------------------#
48 
49 
50 # Base URLs
51 VITE_BASE_URL=https://__SELF_HOST_IP__
52 VITE_SHORTCODE_BASE_URL=https://__SELF_HOST_IP__
53 VITE_ADMIN_URL=https://__SELF_HOST_IP__/admin
54 
55 # Backend URLs
56 VITE_BACKEND_GQL_URL=https://__SELF_HOST_IP__/backend/graphql
57 VITE_BACKEND_WS_URL=wss://__SELF_HOST_IP__/backend/graphql
58 VITE_BACKEND_API_URL=https://__SELF_HOST_IP__/backend/v1
59 
60 # Terms Of Service And Privacy Policy Links (Optional)
61 VITE_APP_TOS_LINK=https://docs.hoppscotch.io/support/terms
62 VITE_APP_PRIVACY_POLICY_LINK=https://docs.hoppscotch.io/support/privacy
63 
64 # Set to `true` for subpath based access
65 ENABLE_SUBPATH_BASED_ACCESS=true

2、nginx配置,default.conf

a.支援https,必須使用https,否則登入功能將不正常,這將導致無法使用團隊空間,資料也無法落表

 1 map $http_upgrade $connection_upgrade {
 2     default upgrade;
 3     ''      close;
 4 }
 5 
 6 server {
 7     listen 443 ssl;
 8     server_name __SELF_HOST_IP__;
 9 
10     ssl_certificate /etc/nginx/ssl/certificate.crt;
11     ssl_certificate_key /etc/nginx/ssl/private.key;
12 
13     location / {
14         proxy_set_header Upgrade $http_upgrade;
15         proxy_set_header Connection $connection_upgrade;
16         
17         proxy_pass http://hoppscotch:80;
18         proxy_set_header Host $host;
19         proxy_set_header X-Real-IP $remote_addr;
20         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
21         proxy_set_header X-Forwarded-Proto $scheme;
22         
23         proxy_read_timeout 86400;
24     }
25     location /admin {
26         proxy_set_header Upgrade $http_upgrade;
27         proxy_set_header Connection $connection_upgrade;
28        
29         proxy_pass http://hoppscotch:80/admin;
30         proxy_set_header Host $host;
31         proxy_set_header X-Real-IP $remote_addr;
32         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
33         proxy_set_header X-Forwarded-Proto $scheme;
34                 
35         proxy_read_timeout 86400;
36     }
37     location /backend {
38         proxy_set_header Upgrade $http_upgrade;
39         proxy_set_header Connection $connection_upgrade;
40        
41         proxy_pass http://hoppscotch:80/backend;
42         proxy_set_header Host $host;
43         proxy_set_header X-Real-IP $remote_addr;
44         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
45         proxy_set_header X-Forwarded-Proto $scheme;
46                 
47         proxy_read_timeout 86400;
48     }
49     location /proxy {
50         proxy_pass http://hoppscotch-proxy:9159;
51         proxy_set_header Host $host;
52         proxy_set_header X-Real-IP $remote_addr;
53         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
54         proxy_set_header X-Forwarded-Proto $scheme;
55     }
56 }

3、docker-compose.yaml

a.注意確認各service配置中,與本地路徑有關的配置是否都與自己的一致,也可直接使用此配置,並根據下文提到的路徑要求上傳檔案

 1 services:
 2   postgres:
 3     image: postgres:latest
 4     volumes:
 5       - ./data/postgres_data:/data/hoppscotch/postgresql/data
 6     environment:
 7       POSTGRES_DB: hoppscotch
 8       POSTGRES_USER: hoppscotch
 9       POSTGRES_PASSWORD: hoppscotch
10     networks:
11       - hoppscotch-network
12 
13   hoppscotch-migrate:
14     image: hoppscotch/hoppscotch:latest
15     env_file:
16       - ./data/hoppscotch/hoppscotch.env
17     entrypoint: sh
18     depends_on:
19       - postgres
20     networks:
21       - hoppscotch-network
22     volumes: 
23       - ./data/share_dir:/share_dir
24     restart: no
25     command: -c "pnpx prisma migrate deploy"
26 
27   hoppscotch:
28     image: hoppscotch/hoppscotch:latest
29     ports:
30       - "8080:80"
31     env_file:
32       - ./data/hoppscotch/hoppscotch.env
33     depends_on:
34       hoppscotch-migrate:
35         condition: service_completed_successfully
36     networks:
37       - hoppscotch-network
38     restart: unless-stopped
39   
40   nginx:
41     image: nginx:latest
42     ports:
43       - "443:443"
44     volumes:
45       - ./data/nginx/conf:/etc/nginx/conf.d
46       - ./data/nginx/ssl:/etc/nginx/ssl
47     depends_on:
48       - hoppscotch
49     networks:
50       - hoppscotch-network
51 
52   hoppscotch-proxy:
53     image: hoppscotch/proxyscotch
54     ports:
55       - "9159:9159"
56     depends_on:
57       - hoppscotch
58     networks:
59       - hoppscotch-network
60 
61 networks:
62   hoppscotch-network:
63     driver: bridge
64     name: hoppscotch-network

三、一鍵部署指令碼

1、安裝指令碼 create_and_start.sh

a.注意本文提供的方式為使用本機ip,需要外部傳入smtp相關資訊

 1 #!/bin/bash
 2 
 3 if [ $# != 3 ]
 4 then
 5     echo "$0 hostname(ip) smtp_url smtp_user"
 6     exit -1
 7 fi
 8 
 9 self_host_ip=$1
10 smtp_url=$2
11 smtp_user=$3
12 
13 mkdir -p data/nginx/conf data/nginx/ssl data/hoppscotch/conf
14 sed "s/__SELF_HOST_IP__/${self_host_ip}/g" conf/nginx/default.conf > data/nginx/conf/default.conf
15 ./gen_key.sh ${self_host_ip} "data/nginx/ssl"
16 
17 sed "s/__SELF_HOST_IP__/${self_host_ip}/g" conf/hoppscotch/hoppscotch.env | sed "s#__MY_SMTP_URL__#${smtp_url}#g" | sed "s#__MY_SMTP_USER__#${smtp_user}#g" > data/hoppscotch/hoppscotch.env
18 
19 sudo docker compose up -d

2、解除安裝指令碼 delete.sh

1 #!/bin/bash
2 
3 sudo docker compose down
4 
5 if [ -d data ]
6 then
7     sudo rm data -r
8 fi

3、ssl證書生成指令碼 ./gen_key.sh

 1 #!/bin/bash
 2 
 3 if [ $# != 2 ]
 4 then
 5     echo "$0 self_host_ip target_dir"
 6     exit -1
 7 fi
 8 
 9 self_host_ip=$1
10 target_dir=$2
11 
12 openssl genrsa -out ${target_dir}/private.key 2048
13 openssl req -new -key ${target_dir}/private.key -out ${target_dir}/private.csr -subj "/CN=${self_host_ip}"
14 openssl x509 -req -days 365 -in ${target_dir}/private.csr -signkey ${target_dir}/private.key -out ${target_dir}/certificate.crt -extfile <(printf "subjectAltName=IP:${self_host_ip}")

四、整體目錄結構

 1 root_folder
 2 |
 3 |-- conf
 4 |   |-- hoppscotch
 5 |   |   |
 6 |   |   \--hoppscotch.env
 7 |   |  
 8 |   \-- nginx
 9 |       |
10 |       \--default.conf
11 |  
12 |--gen_key.sh
13 |--docker-compose.yaml
14 |--create_and_start.sh
15 \--delete.sh

五、安裝

1、呼叫 create_and_start.sh

2、修改頁面配置

六、使用

1、api介面url: https://${IP}

2、管理介面url:https://${IP}/admin

七、資料檢視

在機器中,可使用以下命令進入postgres資料庫

docker exec -it docker_compose-postgres-1 psql -U hoppscotch

相關文章