Skip to content

Commit

Permalink
[Hosting] prepare for web service hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaoliao committed Aug 30, 2021
1 parent 603e9ba commit 77358d8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
**/__pycache__
**/*.pyc

/build
/build

/ictrl.conf
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# iCtrl

**SSH Remote Desktop Client / Web Service**

## Config the Flask Application with Apache2

### Install and Enable the Apache 2 module 'mod_wsgi'

```
sudo apt update
sudo apt install libapache2-mod-wsgi-py3 -y
sudo a2enmod mod-wsgi
```

### Apache2 Configuration Example

To add a config:

```
sudo nano /etc/apache2/sites-available/ictrl.conf
```

ictrl.conf Content:

```
<VirtualHost *:80>
ServerName ictrl.ca
DocumentRoot /var/www/ictrl/client/build
WSGIDaemonProcess ictrl_srv user=ictrl threads=20
WSGIScriptAlias /api /var/www/ictrl/ictrl_srv.wsgi
<Directory /var/www/ictrl>
WSGIProcessGroup ictrl_srv
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/ictrl_error.log
CustomLog ${APACHE_LOG_DIR}/ictrl_custom.log combined
</VirtualHost>
```

### To Enable the Site Config

```
sudo a2ensite ictrl
```
12 changes: 1 addition & 11 deletions application/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys

from flask import Flask, send_from_directory
from flask import Flask
from werkzeug.serving import WSGIRequestHandler

# enable persistent HTTP connections (keep-alive)
Expand Down Expand Up @@ -32,13 +32,3 @@

from .routes import *

if not app.debug:
# Reference: https://stackoverflow.com/questions/44209978/serving-a-front-end-created-with-create-react-app-with
# -flask
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')
17 changes: 17 additions & 0 deletions ictrl_be.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
if __name__ == '__main__':
from application import app, APP_HOST, APP_PORT

if not app.debug:
import os

from flask import send_from_directory


# Reference: https://stackoverflow.com/questions/44209978/serving-a-front-end-created-with-create-react-app-with
# -flask
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve(path):
if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')

app.run(host=APP_HOST, port=APP_PORT)
25 changes: 25 additions & 0 deletions ictrl_srv.wsgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# activate the virtual environment
activate_this = '/var/www/ictrl/venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))

import os
import sys

# insert the project path so that module 'application' can be found
sys.path.insert(0, '/var/www/ictrl/')

# read the config file
# as we don't want to hard-code those sensitive data and commit them into Git
with open('ictrl.conf', 'r') as config_file:
for line in config_file:
if line.startswith('#'):
continue

try:
name, value = line.strip().split('=')
os.environ[name] = value
except ValueError:
pass

# now import the application
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
wheel
bcrypt
flask
Flask-SQLAlchemy
paramiko
Pillow
psycopg2-binary
Expand Down

0 comments on commit 77358d8

Please sign in to comment.