Skip to content

Commit

Permalink
enhance build + add instances vue.js page
Browse files Browse the repository at this point in the history
* remove previous useless assets for every build
* reproduce instances data to easily test it on Vite
* add instances page to build
* update instances builder to fit new components : list pairs and form data
* form submit working on instances actions
  • Loading branch information
syrk4web committed Jul 3, 2024
1 parent 0c3b668 commit a3514d2
Show file tree
Hide file tree
Showing 34 changed files with 161 additions and 411 deletions.
14 changes: 12 additions & 2 deletions src/client/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function setBuildTempToUI() {
fs.readdir(resolve("./templates"), (err, files) => {
// Read content
files.forEach((file) => {
const data = fs.readFile(
fs.readFile(
resolve(`./templates/${file}`),
{
encoding: "utf8",
Expand All @@ -109,7 +109,8 @@ async function setBuildTempToUI() {
}
let updateData = data;
// I want to remove the first "/" for href="/css", href="/js", href="/img", href="/favicon", src="/assets" or src="/js"
const regexPaths = /href="\/(css|js|img|favicon)|src="\/(assets|js)/g;
const regexPaths =
/href="\/(css|js|img|favicon|assets|js)|src="\/(assets|js)/g;
updateData = data.replace(regexPaths, (match) => {
return match.replace("/", "");
});
Expand Down Expand Up @@ -163,8 +164,17 @@ async function moveBuildStaticToUI() {
});
}

async function delPrevDirs() {
// Delete prev existing dir
delElRecursive(`${appStaticDir}/css`);
delElRecursive(`${appStaticDir}/assets`);
delElRecursive(`${appStaticDir}/flags`);
delElRecursive(`${appStaticDir}/img`);
}

async function build() {
// Build client and setup
await delPrevDirs();
await buildVite();
await updateClientDir();
await setBuildTempToUI();
Expand Down
8 changes: 7 additions & 1 deletion src/client/tests/home.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
stats = [
import json

home = [
{
"type": "card",
"link": "https://panel.bunkerweb.io/?utm_campaign=self&utm_source=ui#pro",
Expand Down Expand Up @@ -85,3 +87,7 @@
],
},
]

# store on a file
with open("home.json", "w") as f:
json.dump(home, f, indent=4)
1 change: 1 addition & 0 deletions src/client/tests/instances.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type": "card", "containerColumns": {"pc": 6, "tablet": 6, "mobile": 12}, "widgets": [{"type": "Instance", "data": {"pairs": [{"key": "instances_hostname", "value": "bunkerweb"}, {"key": "instances_type", "value": "manual"}, {"key": "instances_status", "value": "instances_active"}], "status": "success", "title": "bunkerweb", "buttons": [{"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"reload\" }"}, "text": "action_reload", "color": "warning"}, {"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"stop\" }"}, "text": "action_stop", "color": "error"}]}}]}, {"type": "card", "containerColumns": {"pc": 6, "tablet": 6, "mobile": 12}, "widgets": [{"type": "Instance", "data": {"pairs": [{"key": "instances_hostname", "value": "bunkerweb"}, {"key": "instances_type", "value": "manual"}, {"key": "instances_status", "value": "instances_active"}], "status": "success", "title": "bunkerweb", "buttons": [{"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"reload\" }"}, "text": "action_reload", "color": "warning"}, {"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"stop\" }"}, "text": "action_stop", "color": "error"}]}}]}]
77 changes: 77 additions & 0 deletions src/client/tests/instances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import json


# Create instance class using keys from the instances list
class Instance:
def __init__(self, _type, health, _id, hostname, name):
self._type = _type
self.health = health
self._id = _id
self.hostname = hostname
self.name = name


instances = [
Instance("manual", True, "bunkerweb", "bunkerweb", "bunkerweb"),
Instance("manual", True, "bunkerweb", "bunkerweb", "bunkerweb"),
]


def instances_builder(instances: list):
"""
It returns the home page in JSON format for the Vue.js builder
"""
builder = []

for instance in instances:
# setup actions buttons
actions = (
["restart", "stop"]
if instance._type == "local" and instance.health
else (
["reload", "stop"]
if not instance._type == "local" and instance.health
else ["start"] if instance._type == "local" and not instance.health else []
)
)
buttons = [
{
"attrs": {
"data-submit-form": f"""{{"INSTANCE_ID" : "{instance._id}", "operation" : "{action}" }}""",
},
"text": f"action_{action}",
"color": "success" if action == "start" else "error" if action == "stop" else "warning",
}
for action in actions
]

component = {
"type": "card",
"containerColumns": {"pc": 6, "tablet": 6, "mobile": 12},
"widgets": [
{
"type": "Instance",
"data": {
"pairs": [
{"key": "instances_hostname", "value": instance.hostname},
{"key": "instances_type", "value": instance._type},
{"key": "instances_status", "value": "instances_active" if instance.health else "instances_inactive"},
],
"status": "success" if instance.health else "error",
"title": instance.name,
"buttons": buttons,
},
}
],
}

builder.append(component)

return builder


builder = instances_builder(instances)

# store on a file
with open("instances.json", "w") as f:
json.dump(builder, f)
2 changes: 1 addition & 1 deletion src/client/vite/src/pages/instances/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
data-server-flash='[{"type" : "success", "title" : "title", "message" : "Success feedback"}, {"type" : "error", "title" : "title", "message" : "Error feedback"}, {"type" : "warning", "title" : "title", "message" : "Warning feedback"}, {"type" : "info", "title" : "title", "message" : "Info feedback"}]'>
</div>
<div class="hidden"
data-server-builder='[{"type":"card","containerColumns":{"pc":6,"tablet":6,"mobile":12},"widgets":[{"type":"Instance","data":{"pairs":[{"key":"instances_hostname","value":"bunkerweb"},{"key":"instances_type","value":"manual"},{"key":"instances_status","value":"instances_active"}],"status":"success","title":"bunkerweb","buttons":[{"attrs":{"data-submit-form": {"operation" : "reload", "INSTANCE_ID" : "bunkerweb"} },"text":"action_reload","color":"warning","size":"normal"},{"attrs":{"data-submit-form": { "operation" : "stop", "INSTANCE_ID" : "bunkerweb"} },"text":"action_stop","color":"error","size":"normal"}]}}]}]'>
data-server-builder='[{"type": "card", "containerColumns": {"pc": 6, "tablet": 6, "mobile": 12}, "widgets": [{"type": "Instance", "data": {"pairs": [{"key": "instances_hostname", "value": "bunkerweb"}, {"key": "instances_type", "value": "manual"}, {"key": "instances_status", "value": "instances_active"}], "status": "success", "title": "bunkerweb", "buttons": [{"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"reload\" }"}, "text": "action_reload", "color": "warning"}, {"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"stop\" }"}, "text": "action_stop", "color": "error"}]}}]}, {"type": "card", "containerColumns": {"pc": 6, "tablet": 6, "mobile": 12}, "widgets": [{"type": "Instance", "data": {"pairs": [{"key": "instances_hostname", "value": "bunkerweb"}, {"key": "instances_type", "value": "manual"}, {"key": "instances_status", "value": "instances_active"}], "status": "success", "title": "bunkerweb", "buttons": [{"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"reload\" }"}, "text": "action_reload", "color": "warning"}, {"attrs": {"data-submit-form": "{\"INSTANCE_ID\" : \"bunkerweb\", \"operation\" : \"stop\" }"}, "text": "action_stop", "color": "error"}]}}]}]'>
</div>
<div id="app"></div>
<script type="module" src="instances.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/client/vite/src/utils/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function useSubmitForm(data) {
input.value = data[key];
form.appendChild(input);
}
// Append the form to the body and submit it
// Append to be able to submit
document.querySelector("body").appendChild(form);
form.submit();
}
Expand Down
1 change: 1 addition & 0 deletions src/client/vite/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default defineConfig({
rollupOptions: {
input: {
home: resolve(__dirname, "./src/pages/home/index.html"),
instances: resolve(__dirname, "./src/pages/instances/index.html"),
},
},
},
Expand Down
10 changes: 6 additions & 4 deletions src/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,10 +1043,11 @@ def instances_builder(instances: list):
)
buttons = [
{
"attrs": {"data-form-INSTANCE_ID": instance._id, "data-form-operation": action, "data-submit-form": "true"},
"attrs": {
"data-submit-form": f"""{{"INSTANCE_ID" : "{instance._id}", "operation" : "{action}" }}""",
},
"text": f"action_{action}",
"color": "success" if action == "start" else "error" if action == "stop" else "warning",
"size": "normal",
}
for action in actions
]
Expand All @@ -1058,7 +1059,7 @@ def instances_builder(instances: list):
{
"type": "Instance",
"data": {
"details": [
"pairs": [
{"key": "instances_hostname", "value": instance.hostname},
{"key": "instances_type", "value": instance._type},
{"key": "instances_status", "value": "instances_active" if instance.health else "instances_inactive"},
Expand Down Expand Up @@ -1116,8 +1117,9 @@ def instances():

# Display instances
instances = app.config["INSTANCES"].get_instances()

data_server_builder = instances_builder(instances)
return render_template("instances.html", title="Instances", data_server_builder=data_server_builder, instances=instances, username=current_user.get_id())
return render_template("instances.html", title="Instances", data_server_builder=json.dumps(data_server_builder))


@app.route("/services", methods=["GET", "POST"])
Expand Down
25 changes: 25 additions & 0 deletions src/ui/static/assets/Title-9ae7a316.js

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-409d04c4.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-4dedb9c1.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-55d62ea9.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-7b366ea2.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-9060b7b9.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-a85457e7.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-b002ec3a.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-bc58f8dd.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-c051e220.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-cb430cab.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-d04bd3e4.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-e3d69c5d.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/ui/static/assets/home-ee122e19.js

This file was deleted.

1 change: 1 addition & 0 deletions src/ui/static/assets/home-ef55bffd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/ui/static/assets/instances-67da6d8b.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ui/static/css/style.css

Large diffs are not rendered by default.

Binary file removed src/ui/static/img/BUNKERWEB-print-hd-blanc.png
Binary file not shown.
Binary file removed src/ui/static/img/BUNKERWEB-print-hd.png
Binary file not shown.
Binary file removed src/ui/static/img/logo-bw.png
Binary file not shown.
Binary file removed src/ui/static/img/logo-menu-2.png
Binary file not shown.
Binary file removed src/ui/static/img/logo-menu.png
Binary file not shown.
Binary file removed src/ui/static/img/no_internet.png
Binary file not shown.
3 changes: 2 additions & 1 deletion src/ui/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<link rel="stylesheet" href="css/flag-icons.min.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BunkerWeb | DASHBOARD</title>
<script nonce="{{ script_nonce }}" type="module" crossorigin src="assets/home-7b366ea2.js"></script>
<script nonce="{{ script_nonce }}" type="module" crossorigin src="assets/home-ef55bffd.js"></script>
<link rel="modulepreload" crossorigin href="assets/Title-9ae7a316.js">
</head>
<body>
<div class="hidden" data-csrf-token="{{ csrf_token() }}"></div>
Expand Down
Loading

0 comments on commit a3514d2

Please sign in to comment.