Skip to content

Commit

Permalink
add run.py --url_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
shouldsee committed Sep 28, 2022
1 parent 14ba292 commit 567ea27
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ $ sudo dnf install -y git python3 python3-pip python3-venv

```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 +47,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)
)
21 changes: 11 additions & 10 deletions cesi/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@

db = SQLAlchemy()


def create_app(cesi):
def create_app(cesi, url_prefix):
from api.v2 import register_blueprints

app = Flask(
__name__,
static_folder="ui/build",
static_url_path="",
static_url_path="{}".format(url_prefix),
template_folder="ui/build",
)
app.config["SQLALCHEMY_DATABASE_URI"] = cesi.database
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["APPLICATION_ROOT"] = url_prefix
app.secret_key = os.urandom(24)

db.init_app(app)

app.add_url_rule("/", "index", lambda: render_template("index.html"))
app.add_url_rule("{}/".format(url_prefix), "index", lambda: render_template("index.html"))
app.add_url_rule(
"/api/version",
"version",
"{}/api/version".format(url_prefix),
"{}/version".format(url_prefix),
lambda: jsonify(status="success", version=__version__),
)

Expand All @@ -39,20 +39,20 @@ def create_app(cesi):
def _(error):
return jsonify(status="error", message=error.description), error.code

register_blueprints(app)
register_blueprints(app, url_prefix)

return app


def configure(config_file_path):
def configure(config_file_path, args):
from core import Cesi
from loggers import ActivityLog
from controllers import check_database

cesi = Cesi(config_file_path=config_file_path)
_ = ActivityLog(log_path=cesi.activity_log)

app = create_app(cesi)
app = create_app(cesi, args.url_prefix)

# Check database
with app.app_context():
Expand All @@ -70,6 +70,7 @@ def configure(config_file_path):
"-c", "--config-file", "--config", help="config file", required=True
)
parser.add_argument("--host", help="Host of the cesi", default="0.0.0.0")
parser.add_argument("--url_prefix", help="url_prefix to mount under", default="")
parser.add_argument("-p", "--port", help="Port of the cesi", default="5000")
parser.add_argument(
"--debug", help="Actived debug mode of the cesi", action="store_true"
Expand All @@ -82,7 +83,7 @@ def configure(config_file_path):
parser.add_argument("--version", action="version", version=__version__)

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

app.run(
host=args.host, port=args.port, use_reloader=args.auto_reload, debug=args.debug
Expand Down
2 changes: 1 addition & 1 deletion cesi/ui/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class App extends Component {
return (
<React.Fragment>
{this.state.profile ? (
<HashRouter>
<HashRouter basename='{process.env.PUBLIC_URL}'>
<React.Fragment>
<Header
isAdmin={this.state.profile.type === 0}
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
flask==1.1.2
flask-sqlalchemy==2.4.3
flask-sqlalchemy==2.5.1
psycopg2-binary==2.8.5
pymysql==0.9.3
tomlkit==0.5.11
tomlkit==0.5.11
itsdangerous==2.0.1

0 comments on commit 567ea27

Please sign in to comment.