ip user info
This commit is contained in:
42
ip_analyzer_app/utils/assets.py
Normal file
42
ip_analyzer_app/utils/assets.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
from flask import request
|
||||
|
||||
|
||||
|
||||
def get_file_hash(filepath: str) -> str:
|
||||
with open(filepath, 'rb') as file_handle:
|
||||
return hashlib.md5(file_handle.read()).hexdigest()[:8]
|
||||
|
||||
|
||||
|
||||
def register_asset_helpers(app):
|
||||
@app.context_processor
|
||||
def inject_static_hash():
|
||||
def static_hash(filename: str) -> str:
|
||||
filepath = os.path.join(app.static_folder, filename)
|
||||
file_hash = get_file_hash(filepath)
|
||||
return f'/static/{filename}?v={file_hash}'
|
||||
return dict(static_hash=static_hash)
|
||||
|
||||
@app.after_request
|
||||
def add_header(response):
|
||||
if request.path.startswith('/static/'):
|
||||
response.cache_control.no_cache = None
|
||||
response.cache_control.no_store = None
|
||||
response.cache_control.max_age = 31536000
|
||||
response.cache_control.public = True
|
||||
response.headers.pop('Content-Disposition', None)
|
||||
else:
|
||||
response.cache_control.no_cache = True
|
||||
response.cache_control.no_store = True
|
||||
return response
|
||||
|
||||
@app.context_processor
|
||||
def inject_config():
|
||||
def get_base_url() -> str:
|
||||
scheme = request.headers.get('X-Forwarded-Proto', request.scheme)
|
||||
host = request.headers.get('X-Forwarded-Host', request.host)
|
||||
return f'{scheme}://{host}'
|
||||
return dict(base_url=get_base_url, request=request)
|
||||
Reference in New Issue
Block a user