Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt flask and react for reverse_proxy by adding --url_prefix #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __pycache__/

# Distribution / packaging
.Python
build/
#build/
develop-eggs/
dist/
downloads/
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
URL_PREFIX = ''
SHELL := /bin/bash
cwd := $(shell pwd)
ui_path =${cwd}/cesi/ui

run: build-ui
cd $cwd; python3 cesi/run.py --config-file defaults/cesi.conf.toml --url_prefix $(URL_PREFIX)

release: build-ui clean
@echo 'Releasing...'
tar -czvf ${cwd}/../cesi-extended.tar.gz .

build-ui: install-ui
@echo 'Building UI'
export PUBLIC_URL=$(URL_PREFIX); \
pushd ${ui_path}; \
yarn build; \
tar -czvf ${cwd}/../build-ui.tar.gz build; \
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@ $ # On Fedora 28
$ sudo dnf install -y git python3 python3-pip python3-venv
```

**Using Makefile**

```bash
## serve under /cesi
make run URL_PREFIX=/cesi

## or with more control
URL_PREFIX=/cesi
make install-ui URL_PREFIX=$URL_PREFIX
python3 cesi/run.py --config-file defaults/cesi.conf.toml --url_prefix $URL_PREFIX
```

**Install Cesi**

```bash
$ export CESI_SETUP_PATH=~/cesi
$ export PUBLIC_URL='' ## this is the url_prefix to be served under. e.g. /cesi
$ mkdir ${CESI_SETUP_PATH}
$ cd ${CESI_SETUP_PATH}

Expand All @@ -46,7 +59,7 @@ $ source venv/bin/activate
(venv) $ pip3 install -r requirements.txt

$ # Run with command line
(venv) $ python3 ${CESI_SETUP_PATH}/cesi/run.py --config-file ${CESI_SETUP_PATH}/defaults/cesi.conf.toml
(venv) $ python3 ${CESI_SETUP_PATH}/cesi/run.py --config-file ${CESI_SETUP_PATH}/defaults/cesi.conf.toml --url_prefix $PUBLIC_URL
```

**Install Cesi as a service**
Expand Down
5 changes: 3 additions & 2 deletions cesi/api/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
API_PREFIX = "/api/{}".format(API_VERSION)


def register_blueprints(app):
def register_blueprints(app,prefix=''):
# Import and register blueprint modules dynamically
# from blueprints.nodes.routes import nodes
# app.register_blueprint(nodes, url_prefix="/{}/nodes".format(API_VERSION))
Expand All @@ -25,5 +25,6 @@ def register_blueprints(app):
)
blueprint = getattr(module, blueprint_name)
app.register_blueprint(
blueprint, url_prefix="{}/{}".format(API_PREFIX, blueprint_name)
blueprint,
url_prefix="{}/{}/{}".format(prefix, API_PREFIX, blueprint_name)
)
26 changes: 26 additions & 0 deletions cesi/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,28 @@ def configure(config_file_path):

signal.signal(signal.SIGHUP, lambda signum, frame: cesi.reload())



return app, cesi

class PrefixMiddleware(object):
'''
source: https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes/36033627#36033627
'''
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ['PATH_INFO'].startswith(self.prefix):
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):]
environ['SCRIPT_NAME'] = self.prefix
return self.app(environ, start_response)
else:
start_response('404', [('Content-Type', 'text/plain')])
return ["This url does not belong to the app.".encode()]




if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Cesi web server")
Expand All @@ -80,10 +100,16 @@ def configure(config_file_path):
action="store_true",
)
parser.add_argument("--version", action="version", version=__version__)
parser.add_argument(
"--url_prefix",
default = '',
)

args = parser.parse_args()
app, cesi = configure(args.config_file)

app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=args.url_prefix)

app.run(
host=args.host, port=args.port, use_reloader=args.auto_reload, debug=args.debug
)
25 changes: 25 additions & 0 deletions cesi/ui/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"files": {
"main.css": "/cesi/static/css/main.1584120f.chunk.css",
"main.js": "/cesi/static/js/main.3c897545.chunk.js",
"main.js.map": "/cesi/static/js/main.3c897545.chunk.js.map",
"runtime-main.js": "/cesi/static/js/runtime-main.22c0eb5e.js",
"runtime-main.js.map": "/cesi/static/js/runtime-main.22c0eb5e.js.map",
"static/css/2.0e1a4c73.chunk.css": "/cesi/static/css/2.0e1a4c73.chunk.css",
"static/js/2.51644260.chunk.js": "/cesi/static/js/2.51644260.chunk.js",
"static/js/2.51644260.chunk.js.map": "/cesi/static/js/2.51644260.chunk.js.map",
"index.html": "/cesi/index.html",
"precache-manifest.5b705781bda7298d2a42c550d289cce9.js": "/cesi/precache-manifest.5b705781bda7298d2a42c550d289cce9.js",
"service-worker.js": "/cesi/service-worker.js",
"static/css/2.0e1a4c73.chunk.css.map": "/cesi/static/css/2.0e1a4c73.chunk.css.map",
"static/css/main.1584120f.chunk.css.map": "/cesi/static/css/main.1584120f.chunk.css.map",
"static/js/2.51644260.chunk.js.LICENSE.txt": "/cesi/static/js/2.51644260.chunk.js.LICENSE.txt"
},
"entrypoints": [
"static/js/runtime-main.22c0eb5e.js",
"static/css/2.0e1a4c73.chunk.css",
"static/js/2.51644260.chunk.js",
"static/css/main.1584120f.chunk.css",
"static/js/main.3c897545.chunk.js"
]
}
Binary file added cesi/ui/build/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions cesi/ui/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="manifest" href="/cesi/manifest.json"><link rel="shortcut icon" href="/cesi/favicon.ico"><title>Cesi</title><link href="/cesi/static/css/2.0e1a4c73.chunk.css" rel="stylesheet"><link href="/cesi/static/css/main.1584120f.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,i,l=r[0],c=r[1],f=r[2],p=0,s=[];p<l.length;p++)i=l[p],Object.prototype.hasOwnProperty.call(o,i)&&o[i]&&s.push(o[i][0]),o[i]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(a&&a(r);s.length;)s.shift()();return u.push.apply(u,f||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,l=1;l<t.length;l++){var c=t[l];0!==o[c]&&(n=!1)}n&&(u.splice(r--,1),e=i(i.s=t[0]))}return e}var n={},o={1:0},u=[];function i(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,i),t.l=!0,t.exports}i.m=e,i.c=n,i.d=function(e,r,t){i.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,r){if(1&r&&(e=i(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(i.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)i.d(t,n,function(r){return e[r]}.bind(null,n));return t},i.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(r,"a",r),r},i.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},i.p="/cesi/";var l=this["webpackJsonpcesi-web-ui"]=this["webpackJsonpcesi-web-ui"]||[],c=l.push.bind(l);l.push=r,l=l.slice();for(var f=0;f<l.length;f++)r(l[f]);var a=c;t()}([])</script><script src="/cesi/static/js/2.51644260.chunk.js"></script><script src="/cesi/static/js/main.3c897545.chunk.js"></script></body></html>
15 changes: 15 additions & 0 deletions cesi/ui/build/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
self.__precacheManifest = (self.__precacheManifest || []).concat([
{
"revision": "9d01370781135b2da91e62fc91a738fc",
"url": "/cesi/index.html"
},
{
"revision": "765100a4b9a1b26ac6fa",
"url": "/cesi/static/css/2.0e1a4c73.chunk.css"
},
{
"revision": "d97e7f0121cc257061b7",
"url": "/cesi/static/css/main.1584120f.chunk.css"
},
{
"revision": "765100a4b9a1b26ac6fa",
"url": "/cesi/static/js/2.51644260.chunk.js"
},
{
"revision": "d2966845b94a3318bf32eecc7af8015d",
"url": "/cesi/static/js/2.51644260.chunk.js.LICENSE.txt"
},
{
"revision": "d97e7f0121cc257061b7",
"url": "/cesi/static/js/main.3c897545.chunk.js"
},
{
"revision": "3f94683db87da86e641a",
"url": "/cesi/static/js/runtime-main.22c0eb5e.js"
}
]);
39 changes: 39 additions & 0 deletions cesi/ui/build/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
* See https://goo.gl/nhQhGp
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
* See https://goo.gl/2aRDsh
*/

importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js");

importScripts(
"/cesi/precache-manifest.5b705781bda7298d2a42c550d289cce9.js"
);

self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});

workbox.core.clientsClaim();

/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
* See https://goo.gl/S9QRab
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/cesi/index.html"), {

blacklist: [/^\/_/,/\/[^/?]+\.[^/]+$/],
});
7 changes: 7 additions & 0 deletions cesi/ui/build/static/css/2.0e1a4c73.chunk.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cesi/ui/build/static/css/2.0e1a4c73.chunk.css.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cesi/ui/build/static/css/main.1584120f.chunk.css

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

1 change: 1 addition & 0 deletions cesi/ui/build/static/css/main.1584120f.chunk.css.map

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

3 changes: 3 additions & 0 deletions cesi/ui/build/static/js/2.51644260.chunk.js

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions cesi/ui/build/static/js/2.51644260.chunk.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
object-assign
(c) Sindre Sorhus
@license MIT
*/

/*!
Copyright (c) 2017 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/

/** @license React v0.19.1
* scheduler.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/** @license React v16.13.1
* react-dom.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/** @license React v16.13.1
* react-is.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/** @license React v16.13.1
* react.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**!
* @fileOverview Kickass library to create and place poppers near their reference elements.
* @version 1.16.1
* @license
* Copyright (c) 2016 Federico Zivolo and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
1 change: 1 addition & 0 deletions cesi/ui/build/static/js/2.51644260.chunk.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cesi/ui/build/static/js/main.3c897545.chunk.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cesi/ui/build/static/js/main.3c897545.chunk.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions cesi/ui/build/static/js/runtime-main.22c0eb5e.js

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

Loading