Skip to content

Commit 995a302

Browse files
authored
Merge pull request #88 from jocxfin/85-environment-variable-for-domain-subpaths
85 environment variable for domain subpaths
2 parents a2944cd + c9af2e2 commit 995a302

File tree

7 files changed

+41
-44
lines changed

7 files changed

+41
-44
lines changed

app.py

+17-22
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
import config
66
import string
77
import secrets
8-
from utils.password_utils import (calculate_entropy, check_password_pwned,generate_passphrase, get_random_separator, filter_homoglyphs)
8+
from utils.password_utils import (calculate_entropy, check_password_pwned, generate_passphrase, get_random_separator, filter_homoglyphs)
99
from handlers.request_handler import handle_generate_password_request
1010

11-
app = Flask(__name__)
11+
app = Flask(__name__, static_url_path=config.BASE_PATH + 'static')
1212
cache = Cache(app, config={'CACHE_TYPE': config.CACHE_TYPE})
1313

14-
@app.route('/')
14+
@app.route(config.BASE_PATH + '/')
1515
def index():
1616
no_api_check = config.NO_API_CHECK
1717
multi_gen = config.MULTI_GEN
1818
no_lang = config.PP_HIDE_LANG
1919
generate_pp = config.GENERATE_PP
2020
google_site_verification = config.GOOGLE_SITE_VERIFICATION
21-
21+
2222
pw_settings = {
2323
'length': config.PW_LENGTH,
2424
'include_uppercase': config.PW_INCLUDE_UPPERCASE,
2525
'include_digits': config.PW_INCLUDE_DIGITS,
2626
'include_special': config.PW_INCLUDE_SPECIAL,
2727
'exclude_homoglyphs': config.PW_EXCLUDE_HOMOGLYPHS
2828
}
29-
29+
3030
pp_settings = {
3131
'word_count': config.PP_WORD_COUNT,
3232
'capitalize': config.PP_CAPITALIZE,
@@ -38,10 +38,12 @@ def index():
3838
'language': config.PP_LANGUAGE,
3939
'languageCustom': config.PP_LANGUAGE_CUSTOM
4040
}
41-
42-
return render_template('index.html', no_api_check=no_api_check, pw_settings=pw_settings, pp_settings=pp_settings, multi_gen=multi_gen, no_lang=no_lang, generate_pp=generate_pp, google_site_verification=google_site_verification)
4341

44-
@app.route('/generate-password', methods=['POST'])
42+
return render_template('index.html', no_api_check=no_api_check, pw_settings=pw_settings, pp_settings=pp_settings,
43+
multi_gen=multi_gen, no_lang=no_lang, generate_pp=generate_pp,
44+
google_site_verification=google_site_verification, base_path=config.BASE_PATH)
45+
46+
@app.route(config.BASE_PATH + '/generate-password', methods=['POST'])
4547
async def generate_password_route():
4648
if config.MULTI_GEN:
4749
passwords = [await handle_generate_password_request(request.form) for _ in range(5)]
@@ -50,7 +52,7 @@ async def generate_password_route():
5052
response_data = await handle_generate_password_request(request.form)
5153
return jsonify(response_data)
5254

53-
@app.route('/robots.txt')
55+
@app.route(config.BASE_PATH + '/robots.txt')
5456
def robots():
5557
if not config.ROBOTS_ALLOW:
5658
content = """
@@ -64,19 +66,12 @@ def robots():
6466
"""
6567
return Response(content, mimetype='text/plain')
6668

67-
@app.route('/manifest.json')
68-
async def serve_manifest():
69-
cache_key = 'manifest.json'
70-
cached_content = cache.get(cache_key)
71-
if cached_content is None:
72-
with open('manifest.json', 'r') as file:
73-
content = file.read()
74-
cache.set(cache_key, content, timeout=3600)
75-
cached_content = content
76-
return Response(cached_content, mimetype='application/manifest+json')
69+
@app.route(config.BASE_PATH + '/manifest.json')
70+
def serve_manifest():
71+
return send_file('manifest.json', mimetype='application/manifest+json')
7772

78-
@app.route('/service-worker.js')
79-
async def serve_sw():
73+
@app.route(config.BASE_PATH + '/service-worker.js')
74+
def serve_sw():
8075
return send_file('service-worker.js', mimetype='application/javascript')
8176

82-
app_asgi = WsgiToAsgi(app)
77+
app_asgi = WsgiToAsgi(app)

config.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
PP_HIDE_LANG = os.getenv('PP_HIDE_LANG', 'false').lower() == 'true'
2323
GENERATE_PP = os.getenv('GENERATE_PP', 'false').lower() == 'true'
2424
DISABLE_URL_CHECK = os.getenv('DISABLE_URL_CHECK', 'false').lower() == 'true'
25+
BASE_PATH = os.getenv('BASE_PATH', '/')
2526

2627
special_characters = "!£$%^&*(){},./;:#*-+"
2728

@@ -35,4 +36,4 @@
3536
with open('wordlist_fi.txt', 'r') as file:
3637
word_list_fi = [line.strip() for line in file.readlines() if len(line.strip()) <= 12]
3738

38-
haveibeenpwnedapi = 'https://api.pwnedpasswords.com/range/'
39+
haveibeenpwnedapi = 'https://api.pwnedpasswords.com/range/'

handlers/request_handler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ async def handle_generate_password_request(request_form):
6161
attempt += 1
6262

6363
entropy = calculate_entropy(password)
64-
return {"password": password, "entropy": entropy}
64+
return {"password": password, "entropy": entropy}

manifest.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
"name": "Secure Password Generator",
44
"icons": [
55
{
6-
"src": "/static/favicon.png",
6+
"src": "./static/favicon.png",
77
"sizes": "64x64 32x32 24x24 16x16",
88
"type": "image/png"
99
},
1010
{
11-
"src": "/static/favicon.png",
11+
"src": "./static/favicon.png",
1212
"type": "image/png",
1313
"sizes": "192x192"
1414
},
1515
{
16-
"src": "/static/favicon.png",
16+
"src": "./static/favicon.png",
1717
"type": "image/png",
1818
"sizes": "512x512"
1919
}
2020
],
21-
"start_url": "/",
22-
"scope": "/",
21+
"start_url": "./",
22+
"scope": "./",
2323
"display": "standalone",
2424
"theme_color": "#41436A",
2525
"background_color": "#2A3850"
26-
}
26+
}

service-worker.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const CACHE_NAME = 'pwgen-cache-v1';
22
const urlsToCache = [
3-
'/',
4-
'/static/styles.css',
5-
'/static/favicon.png',
6-
'/manifest.json'
3+
'./',
4+
'./static/styles.css',
5+
'./static/favicon.png',
6+
'./manifest.json'
77
];
88

99
self.addEventListener('install', (event) => {

static/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const includeSpecialChars = document.getElementById('includeSpecialChars');
1616
const excludeHomoglyphs = document.getElementById('excludeHomoglyphs');
1717
const refreshpw = document.getElementById('refreshpw');
1818
const languageSelect = document.getElementById('languageSelect');
19+
const BASE_PATH = window.BASE_PATH || '';
1920

2021
separator.onchange = () => customSeparator.style.display = separator.value === 'custom' ? 'block' : 'none';
2122

@@ -34,7 +35,6 @@ document.querySelectorAll('input, select').forEach(element => {
3435
}
3536
});
3637

37-
3838
async function generatePassword() {
3939
const formData = new FormData();
4040
formData.append('length', lengthSlider.value);
@@ -57,7 +57,7 @@ async function generatePassword() {
5757
formData.append('languageCustom', customLanguage.value);
5858
}
5959

60-
fetch('/generate-password', {
60+
fetch(`${BASE_PATH}generate-password`, {
6161
method: 'POST',
6262
body: formData
6363
})
@@ -82,7 +82,6 @@ async function generatePassword() {
8282
});
8383
}
8484

85-
8685
function scrambleAnimation(finalPassword) {
8786
let scrambled = Array.from({ length: finalPassword.length }, () => getRandomCharacter());
8887
passwordInput.value = scrambled.join('');
@@ -116,6 +115,7 @@ wordCountSlider.oninput = function () {
116115
lengthSlider.oninput = function () {
117116
lengthValue.innerText = this.value;
118117
}
118+
119119
function copyPassword(index) {
120120
let password;
121121
if (index === 100) {
@@ -177,4 +177,4 @@ function copyPassword(index) {
177177

178178
document.body.removeChild(textArea);
179179
}
180-
}
180+
}

templates/index.html

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
{% if google_site_verification %}
1212
<meta name="google-site-verification" content="{{ google_site_verification }}" />
1313
{% endif %}
14-
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
15-
<link rel="icon" href="{{ url_for('static', filename='favicon.png') }}" type="image/png">
16-
<link rel="manifest" href="/manifest.json">
14+
<link rel="stylesheet" href="{{ base_path }}static/styles.css">
15+
<link rel="icon" href="{{ base_path }}static/favicon.png" type="image/png">
16+
<link rel="manifest" href="{{ base_path }}manifest.json">
1717
<meta name="theme-color" content="#2A3850">
1818
<script>
19+
window.BASE_PATH = "{{ base_path }}";
1920
if ('serviceWorker' in navigator) {
2021
window.addEventListener('load', () => {
21-
navigator.serviceWorker.register('/service-worker.js')
22+
navigator.serviceWorker.register(`${window.BASE_PATH}service-worker.js`)
2223
.then(reg => console.log('Service Worker registered', reg))
2324
.catch(err => console.log('Service Worker registration failed', err));
2425
});
@@ -223,7 +224,7 @@ <h1 id="header1">Generate a Secure Password</h1>
223224

224225
});
225226
</script>
226-
<script src="{{ url_for('static', filename='index.js') }}"></script>
227+
<script src="{{ base_path }}static/index.js"></script>
227228
</body>
228229

229-
</html>
230+
</html>

0 commit comments

Comments
 (0)