Skip to content

Commit

Permalink
polishing the first release 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Jan 17, 2022
1 parent e31dcff commit c52f837
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 67 deletions.
59 changes: 33 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This is aimed at Hash Kennels to announce runs which then can be consumed
by [Harrier Central](https://www.harriercentral.com)

## Requirements
You need to have a running Wordpress site with 'WP Event Manager' Plugin installed.

### Software
* python >= 3.6
* starlette
Expand All @@ -20,22 +22,22 @@ by [Harrier Central](https://www.harriercentral.com)
### WP Event Manager Plugin
* WP Event Manager >= 3.1.21

# Installing
## Installing
* here we assume we install in ```/opt```

## RedHat based OS
### RedHat based OS
* on RedHat/CentOS 7 you need to install python3.6 and pip from EPEL first
* on RHEL/Rocky Linux 8 systems the package name changed to `python3-pip`
```shell
yum install python36-pip
```

## Ubuntu 18.04 & 20.04
### Ubuntu 18.04 & 20.04
```shell
apt-get update && apt-get install python3-venv
```

## Clone repo and install dependencies
### Clone repo and install dependencies
* download and setup of virtual environment
```shell
cd /opt
Expand All @@ -46,7 +48,7 @@ python3 -m venv .venv
pip3 install -r requirements.txt || pip install -r requirements.txt
```

## Install as systemd service
### Install as systemd service
If files have been installed in a different directory then the systemd service file
needs to be edited.

Expand All @@ -66,7 +68,7 @@ systemctl start wordpress-hash-event-api
systemctl enable wordpress-hash-event-api
```

## Install as OpenRC service
### Install as OpenRC service
The [uvicorn.confd](contrib/uvicorn.confd) config file needs to be copied to `/etc/conf.d/`.
Let's assume the API is called `nerd-h3`.
```shell
Expand All @@ -90,7 +92,7 @@ is finished the service can be started.
rc-update add uvicorn.nerd-h3 default
```

## Docker
### Docker

Run the application in docker container

Expand All @@ -99,31 +101,36 @@ Run the application in docker container

```
docker build -t wordpress-hash-event-api .
docker run --rm -it -v $(pwd)/config.ini:/app/settings.ini wordpress-hash-event-api
docker run --rm -it -v $(pwd)/config.ini:/app/config.ini wordpress-hash-event-api
```

### Nginx as reverse proxy
In case you want to use Nginx as a reverse proxy you can add following lines
to your server block configuration. Make sure to adjust your IP and port accordingly.
```nginx
location /api {
return 307 /api/v1;
}
location /api/v1/ {
rewrite /api/v1/(.*) /$1 break;
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# activate to see the actual remote IP and not just your reverse proxy
# attention: in Europe this has implications on your GDPR statements on your page
# as you log IP addresses.
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
}
```

## Setup
Copy the [config-example.ini](config-example.ini) sample settings file to `config.ini`.
All options are described in the example file.


## ToDo
- [x] fix and test db connection timeout
- [ ] fill README with useful information
- [x] describe config
- [ ] function and class doc strings
- [x] add filters for most run attributes
- [x] add API auth
- [x] clean up code / linting
- [x] describe setup and installation
- [x] add docker file
- [x] try to add "auto-install", this should set up the WordPress Event Manager to add all available fields to all events
- [x] requirements.ini
- [x] add OpenRC init script and config to server API via uvicorn
- [ ] add nginx config example
- [ ] add CORS Headers to nginx config


After starting the API the first time it will add additional fields to each event.
Such as a choice for the hosting Kennel or amount of Hash Cash.


## License
Expand Down
2 changes: 1 addition & 1 deletion api/factory/runs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion api/models/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion api/models/run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion api/routers/runs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion api/security.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
14 changes: 5 additions & 9 deletions common/log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand All @@ -8,8 +8,8 @@
# repository or visit: <https://opensource.org/licenses/MIT>.

import logging
import psutil
import os

from config.log import run_by_systemd

# define valid log levels
valid_log_levels = ["DEBUG", "INFO", "WARNING", "ERROR"]
Expand Down Expand Up @@ -43,12 +43,8 @@ def setup_logging(log_level=None):

log_format = '%(asctime)s - %(levelname)s: %(message)s'

# if app is started via systemd then strip time stamp from log format
try:
if psutil.Process(psutil.Process(os.getpid()).ppid()).name() == "systemd":
log_format = '%(levelname)s: %(message)s'
except Exception as e:
print(f"unable to determine parent process name: {e}")
if run_by_systemd is True:
log_format = '%(levelname)s: %(message)s'

if log_level is None or log_level == "":
print("ERROR: log level undefined or empty. Check config please.")
Expand Down
2 changes: 1 addition & 1 deletion common/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
4 changes: 2 additions & 2 deletions config/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand All @@ -15,7 +15,7 @@ class BasicAPISettings(BaseModel):
title = 'Kennel Runs API'
openapi_url = "/openapi.json"
root_path = "/api/v1"
version = '0.1'
version = '1.0'
debug = False

# EOF
36 changes: 24 additions & 12 deletions config/hash.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand All @@ -8,40 +8,52 @@
# repository or visit: <https://opensource.org/licenses/MIT>.

"""
ToDo:
Describe changing attributes here
These are the attributes possible to select for each run.
This way it is very simple to add new attributes.
Each of them gets "sluggyfied“ and used in the API.
i.e.: a 'Men-only Hash‘ will be formatted to ‘men-only-hash’ using
this function 'common/misc.py#L16-L51'
The consuming side needs be made aware of
ony changes happening here.
"""
hash_attributes = [
'Harriette run',
'Men-only Hash',
'Woman-only Hash',
'Accessible by public transport',
'Bag drop available',
'No bag drop available',
'Kids allowed',
'No kids allowed',
'Bring flashlight',
'Water on trail',
'Walker trail',
'Runner trail',
'Long run trail',
'Pub crawl',
'On after',
'Baby jogger friendly',
'Shiggy run',
'Accessible by public transport',
'Bike Hash',
'City run',
'Live hare',
'Dead hare',
'Nighttime run',
'Harriette run',
'Men-only Hash',
'Woman-only Hash',
'No kids allowed',
'Pub crawl',
'Shiggy run',
'Bike Hash',
'Steep hills',
'Charity event',
'Dog friendly',
'Pick-up Hash',
'Catch the Hare',
'Bring cash on trail',
'Bag drop available',
'AGM'
]

"""
The same principals of formatting apply to 'hash_scope' as to
'hash_attributes'. The consuming side needs be made aware of
ony changes happening here.
"""
hash_scope = [
"Unspecified",
"Do not promote",
Expand Down
19 changes: 17 additions & 2 deletions config/log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand All @@ -9,15 +9,30 @@

# taken from here: https://stackoverflow.com/questions/62934384/how-to-add-timestamp-to-each-request-in-uvicorn-logs

import psutil
import os

default_log_level = "INFO"

run_by_systemd = False

# if app is started via systemd then strip time stamp from log format
request_logger_config_format = '%(asctime)s - %(levelname)s: %(client_addr)s - "%(request_line)s" %(status_code)s'
try:
if psutil.Process(psutil.Process(os.getpid()).ppid()).name() == "systemd":
run_by_systemd = True
request_logger_config_format = '%(levelname)s: %(client_addr)s - "%(request_line)s" %(status_code)s'
except Exception as e:
print(f"unable to determine parent process name: {e}")


request_logger_config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": '%(asctime)s - %(levelname)s: %(client_addr)s - "%(request_line)s" %(status_code)s',
"fmt": request_logger_config_format,
"use_colors": True
},
},
Expand Down
2 changes: 1 addition & 1 deletion config/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
4 changes: 2 additions & 2 deletions config/models/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand All @@ -12,7 +12,7 @@

class APIConfigSettings(EnvOverridesBaseSettings):
token: str = None
root_path: str = "/"
root_path: str = "/api/v1"

class Config:
env_prefix = f"{__name__.split('.')[-1]}_"
2 changes: 1 addition & 1 deletion config/models/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion config/models/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion config/models/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion source/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down
2 changes: 1 addition & 1 deletion source/manage_event_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2021 Ricardo Bartels. All rights reserved.
# Copyright (c) 2022 Ricardo Bartels. All rights reserved.
#
# wordpress-hash-event-api
#
Expand Down

0 comments on commit c52f837

Please sign in to comment.