-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create proxy to serve lazy loaded assets
- Loading branch information
1 parent
c717beb
commit c983ac4
Showing
8 changed files
with
49 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class FrontendConfig(AppConfig): | ||
name = 'frontend' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from django.urls import path, re_path | ||
from django.urls import re_path | ||
from . import views | ||
|
||
|
||
urlpatterns = [ | ||
re_path(r'^biography$|^blog$|^$', views.index, name='frontend'), | ||
re_path(r'^blog/(?P<string>.+)$|^$', views.blog_post, name='frontend') | ||
re_path(r'^blog$|^$', views.index, name='frontend'), | ||
re_path(r'^blog/(?P<string>.+)$|^$', views.blog_post, name='frontend'), | ||
re_path(r'^.*\.js$', views.js_files_handler, name='frontend'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,44 @@ | ||
import requests | ||
from django.shortcuts import render | ||
from core.settings.base import PRODUCTION_MODE | ||
from django.shortcuts import redirect | ||
from django.http import StreamingHttpResponse | ||
from wsgiref.util import is_hop_by_hop | ||
|
||
class ProxyHttpResponse(StreamingHttpResponse): | ||
def __init__(self, url, headers=None, **kwargs): | ||
upstream = requests.get(url, stream=True, headers=headers) | ||
|
||
kwargs.setdefault('content_type', upstream.headers.get('content-type')) | ||
kwargs.setdefault('status', upstream.status_code) | ||
kwargs.setdefault('reason', upstream.reason) | ||
|
||
super().__init__(upstream.raw, **kwargs) | ||
|
||
for name, value in upstream.headers.items(): | ||
if not is_hop_by_hop(name): | ||
self[name] = value | ||
|
||
|
||
def index(request): | ||
endpoint = request.META.get('PATH_INFO', None) | ||
pathname = request.META.get('PATH_INFO', None) | ||
|
||
if (endpoint == '/admin'): | ||
if (pathname == '/admin'): | ||
return redirect('/admin/') | ||
|
||
return render(request, 'frontend/index.html') | ||
|
||
|
||
def blog_post(request, string): | ||
return render(request, 'frontend/index.html') | ||
return render(request, 'frontend/index.html') | ||
|
||
|
||
def js_files_handler(request): | ||
pathname = request.META.get('PATH_INFO', None) | ||
url = request.build_absolute_uri() | ||
|
||
backend_relative_pathname = pathname.replace('/', '/static/frontend/') | ||
url = url.replace(pathname, backend_relative_pathname) | ||
|
||
print(url) | ||
|
||
return ProxyHttpResponse(url, headers=request.headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters