Skip to content

Commit 6707c49

Browse files
committed
Add port and host as variables
1 parent 9b05bd2 commit 6707c49

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ Enjoy and please do feel free to feedback experiences and issues.
1515

1616
## Securing the API
1717

18-
By default, the API is exposed so your interface can interact directly. In other words, anyone can go to `http://your-device:9090/v1/connect` to send commands to your device. If you would prefer to only allow access from your backend, change the following line in run.py:
18+
By default, the API is exposed so your interface can interact directly. In other words, anyone can go to `http://your-device:9090/v1/connect` to send commands to your device.
1919

20-
```diff
21-
- app.run(port=9090, host='0.0.0.0')
22-
+ app.run(port=9090, host='127.0.0.1')
23-
```
24-
25-
Then ensure your backend container is connected to the host network so it matches the API docker-compose.yml file in this repo:
20+
If you would prefer to only allow access from your backend, change the `host` environment variable to `127.0.0.1`. Then ensure your backend container is connected to the host network so it matches the API docker-compose.yml file in this repo:
2621

2722
`network_mode: "host"`
2823

2924
Users will then be unable to access the API `http://your-device:9090/v1/connect`. Your backend container on the device, however, can reach the API using `http://127.0.0.1:9090/v1/connect`. This is useful if your interface has a login process, and you only want users to be able to interact with Wi-Fi after logging in.
3025

26+
Alternatively, if you would rather have your backend use specified ports instead of the host network, you can change the `host` environment variable to `172.17.0.1` and access the API from `http://172.17.0.1:9090/v1/connect`.
3127

3228

3329
## Endpoints

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
version: '2.1'
22

33
services:
4-
balena-py-wifi-connect:
4+
py-wifi-connect:
55
environment:
66
HOTSPOT_SSID: "Py Wi-Fi Connect"
77
#HOTSPOT_PASSWORD: "my-hotspot-password" # Hotspot passwords must be 8 characters or more.
8+
HOST: "0.0.0.0"
9+
PORT: 9090
810
DBUS_SYSTEM_BUS_ADDRESS: "unix:path=/host/run/dbus/system_bus_socket"
911
build:
1012
context: .

src/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
else:
1313
hotspot_password = None
1414

15+
# Set default host.
16+
if "HOST" in os.environ:
17+
host = os.environ['HOST']
18+
else:
19+
host = '0.0.0.0'
20+
21+
# Set default port.
22+
if "PORT" in os.environ:
23+
port = os.environ['PORT']
24+
else:
25+
port = 9090
26+
1527
# Default access point name. No need to change these under usual operation as
1628
# they are for use inside the app only. PWC is acronym for 'Py Wi-Fi Connect'.
1729
ap_name = 'PWC'

src/run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from common.wifi import check_wifi_status
99
from common.wifi import connect
1010
from common.wifi import refresh_networks
11+
from config import host
12+
from config import port
1113
from resources.system_routes import system_health_check
1214
from resources.wifi_routes import wifi_connect
1315
from resources.wifi_routes import wifi_connection_status
@@ -67,4 +69,4 @@ def handle_sigterm(*args):
6769
# Initialise and start
6870
api.init_app(app)
6971

70-
app.run(port=9090, host='0.0.0.0')
72+
app.run(port=port, host=host)

0 commit comments

Comments
 (0)