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

[WIP] - "improve" things (in my own opinionated way) #4

Open
wants to merge 10 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
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.7.6
67 changes: 67 additions & 0 deletions .serverless/cloudformation-template-create-stack.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "The AWS CloudFormation template for this Serverless application",
"Resources": {
"ServerlessDeploymentBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketEncryption": {
"ServerSideEncryptionConfiguration": [
{
"ServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
}
}
]
}
}
},
"ServerlessDeploymentBucketPolicy": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "ServerlessDeploymentBucket"
},
"PolicyDocument": {
"Statement": [
{
"Action": "s3:*",
"Effect": "Deny",
"Principal": "*",
"Resource": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":s3:::",
{
"Ref": "ServerlessDeploymentBucket"
},
"/*"
]
]
}
],
"Condition": {
"Bool": {
"aws:SecureTransport": false
}
}
}
]
}
}
}
},
"Outputs": {
"ServerlessDeploymentBucketName": {
"Value": {
"Ref": "ServerlessDeploymentBucket"
}
}
}
}
3 changes: 3 additions & 0 deletions .serverless/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ausbills
dnspython
pymongo
2 changes: 1 addition & 1 deletion DBSTRUCTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The document data for this collection shall include:
- id
- vote: yes/no
- Users public key
- Time
- Constituency
- vote hash

## Results Collection
The document data for this collection shall include:
Expand Down
122 changes: 110 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@

### On first time

* install NVM
* install pyenv

```
sudo apt-get install nodejs
git clone https://github.com/KipCrossing/voting-app-api
cd voting-app-api

# if ubuntu
sudo apt-get install curl software-properties-common

curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -

sudo npm install -g serverless
# use NVM instead; curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
nvm use 12
# npm install serverless
npm i

# ensure python 3.7.6 installed in pyenv
pyenv install
python3 -m pip install -r requirements.txt
```

[Install MongoDB](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/#install-mongodb-community-edition)
Expand All @@ -25,6 +34,8 @@ npm install

### On every startup

### Dev (offlline)

Start MongoDB

```
Expand All @@ -36,6 +47,8 @@ Populate/update the DB
```
python3 update_bills_db.py
python3 update_issues_db.py
python3 update_ballotspecs_db.py
python3 update_results_db.py
```

'python3 update_bills_db.py' _may be run on loop every few hours to update DB_
Expand All @@ -45,22 +58,107 @@ we haven't got a good way to update the issues collection yet
Run serverless offline

```
serverless offline
npx sls offline
```

Ctrl+C to stop _serverless offline_

### Deploy

Add mongoDB creds to env vars:

```
MONGO_DB_USER=
MONGO_DB_PASS=
```

Populate DB

```
python3 update_bills_db.py
python3 update_issues_db.py
python3 update_ballotspecs_db.py
python3 update_results_db.py
```

May also need to check the url in `mode.py`.

Add aws creds:

```
aws configure
```

Link MongoDB cluster to aws - todo

Deploy to aws:

```
serverless deploy
```

## Public Contracts

Local Dev:

```
┌────────────────────────────────────────────────────────────────────────────────┐
│ GET | http://localhost:3000/dev/bill/{id} │
│ GET | http://localhost:3000/dev/bill │
│ GET | http://localhost:3000/dev/issue/{id} │
│ GET | http://localhost:3000/dev/issue │
└────────────────────────────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ GET | http://localhost:3000/dev/bill/{id} │
│ GET | http://localhost:3000/dev/bill │
│ GET | http://localhost:3000/dev/issue/{id} │
│ GET | http://localhost:3000/dev/issue │
│ GET | http://localhost:3000/dev/shitchain/{id} │
│ GET | http://localhost:3000/dev/shitchain │
│ POST | http://localhost:3000/dev/shitchain │
│ GET | http://localhost:3000/dev/result/{id} │
│ GET | http://localhost:3000/dev/result │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
```

Where `{id}` is the bill/issue id

To make a vote, `POST` to:

```
http://localhost:3000/dev/shitchain/
```

with example body:

```json
{
"pub_key":"lafksdjfnhc934y8q5pcn98xpc5ny85y410c5mp9xnyv",
"ballot_id": "r6434",
"ballotspec_hash": "86d9935a4fcdd7d517293229527ace224287cb6ba2d07115f4784db16fece5af",
"constituency": "Australia",
"vote": "no"
}
```

## Lambdas

### Bills

Update bills once a day around 5 am by running:

```
python3 update_bills_db.py
```


### Issues

Can update issues collecting via api

- To do

### Results

Count votes every 15 mins and update results collections

```
python3 update_results_db.py
```
23 changes: 16 additions & 7 deletions mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
BALLOTSPECSCOLLECTION = "ballotspecscollection"
URL = 'url'

prod = False
user = os.environ['MONGO_DB_USER']
pwd = os.environ['MONGO_DB_PASS'],


print(user, pwd)
if user is not None and pwd is not None:
cluster = True
else:
cluster = False


ms = {
# MONGODB: os.environ['MONGO_DB_NAME'],
Expand All @@ -29,19 +38,19 @@
URL: "localhost:27017",
}

if prod:
if cluster:
ms = {
USER: os.environ['MONGO_DB_USER'],
PWD: os.environ['MONGO_DB_PASS'],
MONGODB: os.environ['MONGO_DB_NAME'],
USER: user,
PWD: pwd,
MONGODB: "votingdb",
BILLSCOLLECTION: "bills",
ISSUESCOLLECTION: "issues",
RESULTSCOLLECTION: "results",
USERSCOLLECTION: "users",
VOTESCOLLECTION: "votes",
BALLOTSPECSCOLLECTION: "ballotspecs",
URL: "mongodb+srv://" + usr + ":" + pwd + "@" +
os.environ['MONGO_DB_URL'] + "/test?retryWrites=true&w=majority"
URL: "mongodb+srv://" + user + ":" + pwd[0] + "@" +
"cluster0-ctiil.mongodb.net" + "/test?retryWrites=true&w=majority"
}


Expand Down
Loading