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

Market app #4

Merged
merged 7 commits into from
Oct 9, 2024
Merged
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
24 changes: 24 additions & 0 deletions .github/workflows/semgrep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Semgrep
on:
workflow_dispatch: {}
pull_request: {}
push:
branches:
- main
paths:
- .github/workflows/semgrep.yml
schedule:
# random HH:MM to avoid a load spike on GitHub Actions at 00:00
- cron: '44 16 * * *'
jobs:
semgrep:
name: semgrep/ci
runs-on: ubuntu-20.04
env:
SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }}
container:
image: semgrep/semgrep
if: (github.actor != 'dependabot[bot]')
steps:
- uses: actions/checkout@v4
- run: semgrep ci
3 changes: 3 additions & 0 deletions bca-market-services/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/venv
*/__pycache__
src/bca_market_services
Empty file.
11 changes: 11 additions & 0 deletions bca-market-services/idl/Base.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace py bca_market_services

exception InvalidOperation {
1: i32 noSuchOp,
2: string reason
}

exception NotFound {
1: i32 noSuchId,
2: string reason
}
14 changes: 14 additions & 0 deletions bca-market-services/idl/Owner.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

include "Base.thrift"

namespace py bca_market_services

struct OwnerData {
1: required i32 id,
2: string name,
3: string email,
}

service Owner {
OwnerData find_owner(1:i32 id) throws (1: Base.NotFound e)
}
60 changes: 60 additions & 0 deletions bca-market-services/idl/Provider.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
include "Base.thrift"
include "Owner.thrift"

namespace py bca_market_services

struct ControllerData {
1: required i32 id,
2: i32 owner,
3: list<i32> serviceids,
4: string description,
}

struct ServiceData {
1: required i32 id,
2: string name,
3: i32 pricingid,
4: string description,
}

typedef i16 hours
struct PricingData {
1: required i32 id,
2: string description,
3: hours period,
4: i32 setupFee,
5: i32 tickFee,
}

struct InstanceData {
1: required i32 id,
2: i32 serviceid,
3: i32 userid,
4: i32 credentialid,
}

enum Protocol { TCP = 1, UDP = 2 }
struct CredentialData {
1: required i32 id,
2: string address,
3: i8 port,
4: Protocol protocol
5: string username
6: string password
7: string pubkey

}

service ServiceController {
list<i32> list_services()
ServiceData get_service(1:i32 id) throws (1: Base.NotFound e)
list<i32> list_pricings()
PricingData get_pricing(1:i32 id) throws (1: Base.NotFound e)
list<i32> list_instances(1:i32 userid)
InstanceData get_instance(1:i32 id) throws (1: Base.NotFound e)
}

service Provider extends Owner.Owner {
list<i32> list_controllers()
ControllerData get_controller(1:i32 id) throws (1: Base.NotFound e)
}
7 changes: 7 additions & 0 deletions bca-market-services/idl/all.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace py bca_market_services

include "Base.thrift"

include "Owner.thrift"

include "Provider.thrift"
45 changes: 45 additions & 0 deletions bca-market-services/src/.mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[mypy]

explicit_package_bases = True

# Start off with these
warn_unused_configs = True
warn_redundant_casts = True
warn_unused_ignores = True

# Getting these passing should be easy
strict_equality = True
strict_concatenate = True

# Strongly recommend enabling this one as soon as you can
check_untyped_defs = True

# These shouldn't be too much additional work, but may be tricky to
# get passing if you use a lot of untyped libraries
disallow_subclassing_any = True
disallow_untyped_decorators = True
disallow_any_generics = True

# These next few are various gradations of forcing use of type annotations
disallow_untyped_calls = True
disallow_incomplete_defs = True
disallow_untyped_defs = True

# This one isn't too hard to get passing, but return on investment is lower
no_implicit_reexport = True

# This one can be tricky to get passing if you use a lot of untyped libraries
warn_return_any = True

[thrift]
ignore_missing_imports = True

[thrift.*]
ignore_missing_imports = True

[mypy-thriftpy2]
ignore_missing_imports = True
# ignore_errors = True

[mypy-thriftpy2.*]
ignore_missing_imports = True
Empty file.
Empty file.
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions bca-market-services/src/DbConnection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from os import getenv, environ
import psycopg2

def db_connection():
try:
dbhost: string = getenv('PGHOST','localhost')
dbport: string = int(getenv('PGPORT','5432'))
dbname: string = environ['PGDATABASE']
dbuser: string = environ['PGUSER']
dbpassword: string = environ['PGPASSWORD']
with psycopg2.connect(host=dbhost, port=dbport, dbname=dbname, user=dbuser, password=dbpassword) as conn:
print('Connected to the PostgreSQL server.')
return conn
except (psycopg2.DatabaseError, Exception) as error:
print(f"failed to connect to database: {error}")
25 changes: 25 additions & 0 deletions bca-market-services/src/SProvider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List

import thriftpy2
provider_thrift = thriftpy2.load("../idl/Provider.thrift", module_name="provider_thrift")

# print(provider_thrift.Provider.__dict__)

from DbConnection import db_connection

from thriftpy2.protocol import TCyBinaryProtocolFactory
from thriftpy2.transport import TCyBufferedTransportFactory
from thriftpy2.rpc import make_server

class SProvider(object):
def list_controllers(self) -> List[int]:
return [0, 1, 3]

def get_controller(self, id:int) -> provider_thrift.ControllerData:
return provider_thrift.ControllerData(12, 44, [1,2,3])


db_connection()

server = make_server(provider_thrift.Provider, SProvider(), '127.0.0.1', 9001)
server.serve()
Empty file.
9 changes: 9 additions & 0 deletions bca-market-services/test/CProvider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import thriftpy2

provider_thrift = thriftpy2.load("../idl/Provider.thrift", module_name="provider_thrift")

from thriftpy2.rpc import make_client

client = make_client(provider_thrift.Provider, '127.0.0.1', 9001)
print(client.list_controllers())
print(client.get_controller(0))
Loading