Skip to content

Commit c6790b1

Browse files
authored
Merge pull request #57 from ByteInternet/nginx_redirects
2 parents 4790fa8 + f3db239 commit c6790b1

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

.github/workflows/deploy.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
- uses: webfactory/ssh-agent@v0.5.4
2323
with:
2424
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
25-
- run: apt update && apt install -y python3 virtualenv
2625
- run: hypernode-deploy build -vvv
2726
- name: archive production artifacts
2827
uses: actions/upload-artifact@v3

bin/generate_nginx_redirects

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python3
2+
from hypernode.redirect.generate_nginx_redirects import main
3+
4+
if __name__ == "__main__":
5+
main()

deploy.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@
2525
task('python:venv:create', static function () {
2626
run('mkdir -p .hypernode');
2727
run('virtualenv -p python3 .venv');
28+
run('echo export PYTHONPATH=$(pwd) >> .venv/bin/activate');
2829
});
2930

3031
# Install the requirements
3132
task('python:venv:requirements', static function () {
3233
run('source .venv/bin/activate && pip install -r requirements/base.txt');
3334
});
3435

36+
task('python:generate_redirects', static function () {
37+
run('mkdir -p etc/nginx');
38+
run('source .venv/bin/activate && bin/generate_nginx_redirects > etc/nginx/server.redirects.conf');
39+
});
40+
3541
# Build the documentation
3642
task('python:build_documentation', static function () {
3743
run('source .venv/bin/activate && bin/build_docs');
@@ -49,17 +55,28 @@
4955
}
5056
});
5157

52-
task("deploy:docs_vhost", static function () {
53-
run("hypernode-manage-vhosts --https --force-https {{hostname}} --no --webroot {{current_path}}/{{public_folder}}");
58+
task('deploy:docs_vhost:acceptance', static function () {
59+
run('hypernode-manage-vhosts --https --force-https {{hostname}} --no --webroot {{current_path}}/{{public_folder}}');
60+
})->select('stage=acceptance');
61+
62+
task('deploy:docs_vhost:production', static function () {
63+
run('hypernode-manage-vhosts --https --force-https docs.hypernode.io --no --webroot {{current_path}}/{{public_folder}}');
64+
})->select('stage=production');
65+
66+
task('deploy:nginx_redirects', static function () {
67+
run('cp {{release_path}}/etc/nginx/server.redirects.conf /data/web/nginx/server.redirects.conf');
5468
});
5569

5670
$configuration = new Configuration();
5771
$configuration->addBuildTask('python:venv:create');
5872
$configuration->addBuildTask('python:venv:requirements');
5973
$configuration->addBuildTask('python:build_documentation');
74+
$configuration->addBuildTask('python:generate_redirects');
6075
$configuration->addDeployTask('deploy:disable_public');
6176
$configuration->addDeployTask('deploy:hmv_docker');
62-
$configuration->addDeployTask('deploy:docs_vhost');
77+
$configuration->addDeployTask('deploy:docs_vhost:acceptance');
78+
$configuration->addDeployTask('deploy:docs_vhost:production');
79+
$configuration->addDeployTask('deploy:nginx_redirects');
6380

6481
# Just some sane defaults to exclude from the deploy
6582
$configuration->setDeployExclude([
@@ -73,12 +90,11 @@
7390
'.idea',
7491
'.gitignore',
7592
'.editorconfig',
76-
'etc/',
77-
'.venv/',
78-
'bin/',
79-
'hypernode/',
80-
'requirements/',
81-
'tests/'
93+
'./.venv',
94+
'./bin',
95+
'./hypernode',
96+
'./requirements',
97+
'./tests',
8298
]);
8399

84100
$productionStage = $configuration->addStage('production', 'docs.hypernode.io');
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os.path
2+
from pathlib import Path
3+
from typing import List
4+
5+
from frontmatter import Frontmatter
6+
7+
from hypernode.common.docs import get_all_docs
8+
from hypernode.common.settings import DOCS_DIR
9+
10+
11+
def get_redirects_from_doc(doc: Path) -> List[str]:
12+
fm = Frontmatter.read_file(doc)
13+
attributes = fm["attributes"] or {}
14+
return attributes.get("redirect_from", [])
15+
16+
17+
def get_path_for_doc(doc: Path) -> str:
18+
relative_path = Path(os.path.relpath(doc, DOCS_DIR))
19+
path = "/{}/{}".format(
20+
relative_path.parent, relative_path.name.replace(".md", ".html")
21+
)
22+
path = path.replace("/./", "/")
23+
path = path.replace("/index.html", "/")
24+
return path
25+
26+
27+
def main():
28+
for doc in get_all_docs():
29+
for redirect in get_redirects_from_doc(doc):
30+
doc_path = get_path_for_doc(doc)
31+
print("rewrite ^{}$ {} permanent;".format(redirect, doc_path))

0 commit comments

Comments
 (0)