Skip to content

Commit 58066ab

Browse files
authored
feat: add embedded apisix dashboard ui (#12276)
1 parent c2ef083 commit 58066ab

File tree

4 files changed

+174
-9
lines changed

4 files changed

+174
-9
lines changed

apisix/cli/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ local _M = {
360360
}
361361
},
362362
enable_admin_cors = true,
363+
enable_admin_ui = true,
363364
allow_admin = { "127.0.0.0/24" },
364365
admin_listen = {
365366
ip = "0.0.0.0",

apisix/cli/ngx_tpl.lua

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,20 +632,35 @@ http {
632632
set $upstream_host $http_host;
633633
set $upstream_uri '';
634634
635-
location /apisix/admin {
636-
{%if allow_admin then%}
637-
{% for _, allow_ip in ipairs(allow_admin) do %}
638-
allow {*allow_ip*};
639-
{% end %}
640-
deny all;
641-
{%else%}
642-
allow all;
643-
{%end%}
635+
{%if allow_admin then%}
636+
{% for _, allow_ip in ipairs(allow_admin) do %}
637+
allow {*allow_ip*};
638+
{% end %}
639+
deny all;
640+
{%else%}
641+
allow all;
642+
{%end%}
644643
644+
location /apisix/admin {
645645
content_by_lua_block {
646646
apisix.http_admin()
647647
}
648648
}
649+
650+
{% if enable_admin_ui then %}
651+
location = /ui {
652+
return 301 /ui/;
653+
}
654+
location ^~ /ui/ {
655+
rewrite ^/ui/(.*)$ /$1 break;
656+
root {* apisix_lua_home *}/ui;
657+
try_files $uri /index.html =404;
658+
gzip on;
659+
gzip_types text/css application/javascript application/json;
660+
expires 7200s;
661+
add_header Cache-Control "private,max-age=7200";
662+
}
663+
{% end %}
649664
}
650665
{% end %}
651666

conf/config.yaml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ deployment: # Deployment configurations
674674
# role: viewer
675675

676676
enable_admin_cors: true # Enable Admin API CORS response header `Access-Control-Allow-Origin`.
677+
enable_admin_ui: true # Enable embedded APISIX Dashboard UI.
677678
allow_admin: # Limit Admin API access by IP addresses.
678679
- 127.0.0.0/24 # If not set, any IP address is allowed.
679680
# - "::/64"

t/cli/test_admin_ui.sh

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership.
7+
# The ASF licenses this file to You under the Apache License, Version 2.0
8+
# (the "License"); you may not use this file except in compliance with
9+
# the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
. ./t/cli/common.sh
21+
22+
# check admin ui enabled
23+
24+
git checkout conf/config.yaml
25+
26+
make init
27+
28+
grep "location ^~ /ui/" conf/nginx.conf > /dev/null
29+
if [ ! $? -eq 0 ]; then
30+
echo "failed: failed to enable embedded admin ui"
31+
exit 1
32+
fi
33+
34+
make run
35+
36+
## check /ui redirects to /ui/
37+
38+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui)
39+
if [ ! $code -eq 301 ]; then
40+
echo "failed: failed to redirect /ui to /ui/"
41+
exit 1
42+
fi
43+
44+
## check /ui/ accessible
45+
46+
mkdir -p ui/assets
47+
echo "test_html" > ui/index.html
48+
echo "test_js" > ui/assets/test.js
49+
echo "test_css" > ui/assets/test.css
50+
51+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui/)
52+
if [ ! $code -eq 200 ]; then
53+
echo "failed: /ui/ not accessible"
54+
exit 1
55+
fi
56+
57+
## check /ui/index.html accessible
58+
59+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui/index.html)
60+
if [ ! $code -eq 200 ]; then
61+
echo "failed: /ui/index.html not accessible"
62+
exit 1
63+
fi
64+
65+
## check /ui/assets/test.js accessible
66+
67+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui/assets/test.js)
68+
if [ ! $code -eq 200 ]; then
69+
echo "failed: /ui/assets/test.js not accessible"
70+
exit 1
71+
fi
72+
73+
## check /ui/assets/test.css accessible
74+
75+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui/assets/test.css)
76+
if [ ! $code -eq 200 ]; then
77+
echo "failed: /ui/assets/test.css not accessible"
78+
exit 1
79+
fi
80+
81+
## check /ui/ single-page-application fallback
82+
83+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui/not_exist)
84+
if [ ! $code -eq 200 ]; then
85+
echo "failed: /ui/not_exist not accessible"
86+
exit 1
87+
fi
88+
89+
make stop
90+
91+
# test ip restriction
92+
93+
git checkout conf/config.yaml
94+
95+
echo "
96+
deployment:
97+
admin:
98+
enable_admin_ui: true
99+
allow_admin:
100+
- 1.1.1.1/32
101+
" > conf/config.yaml
102+
103+
make run
104+
105+
code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9180/ui/)
106+
if [ ! $code -eq 403 ]; then
107+
echo "failed: ip restriction not working, expected 403, got $code"
108+
exit 1
109+
fi
110+
111+
make stop
112+
113+
# test admin ui disabled
114+
115+
git checkout conf/config.yaml
116+
117+
echo "
118+
deployment:
119+
admin:
120+
enable_admin_ui: false
121+
" > conf/config.yaml
122+
123+
make init
124+
125+
#### When grep cannot find the value, it uses 1 as the exit code.
126+
#### Due to the use of set -e, any non-zero exit will terminate the
127+
#### script, so grep is written inside the if statement here.
128+
if grep "location ^~ /ui/" conf/nginx.conf > /dev/null; then
129+
echo "failed: failed to disable embedded admin ui"
130+
exit 1
131+
fi
132+
133+
# test admin UI explicitly enabled
134+
135+
git checkout conf/config.yaml
136+
137+
echo "
138+
deployment:
139+
admin:
140+
enable_admin_ui: true
141+
" > conf/config.yaml
142+
143+
make init
144+
145+
if ! grep "location ^~ /ui/" conf/nginx.conf > /dev/null; then
146+
echo "failed: failed to explicitly enable embedded admin ui"
147+
exit 1
148+
fi

0 commit comments

Comments
 (0)