Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecaruso committed May 9, 2020
0 parents commit f04925c
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=3000
145 changes: 145 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# Other
.idea/
*.iml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Davide Caruso

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<p align="center">
<img src="https://github.com/davidecaruso/wazowski/raw/master/logo.jpg" />
</p>

> *Python+GraphQL* microservice to check **ports availability** in a host.
## Install
```shell script
git clone [email protected]:davidecaruso/wazowski.git && cd wazowski
cp .env.example .env && vi .env
pip3 install --no-cache-dir -r requirements.txt
python3 main.py
```

## Getting started
Many of the following *GraphQL* queries accept these parameters:
- `start`: is the starting port value of the range. Default value is **1024**
- `end`: is the ending port value of the range. Default value is **65535**
- `host`: is the host where the search will be performed. Default value is **"127.0.0.1"**

#### Check if a port is free
> **POST** /graphql
```graphql
query Query {
check(port: 3000, host: "127.0.0.1")
}
```
> **200**
```json
{
"data": {
"check": true
}
}
```

#### Get the list of free ports in a range
> **POST** /graphql
```graphql
query Query {
list(start: 3000, end: 3010, host: "127.0.0.1")
}
```
> **200**
```json
{
"data": {
"list": [
3000,
3002,
3010
]
}
}
```

#### Get next free port to a given one in a range
> **POST** /graphql
```graphql
query Query {
next(port: 3007, start: 3000, end: 3010, host: "127.0.0.1")
}
```
> **200**
```json
{
"data": {
"next": 3008
}
}
```

#### Get the previous free port to a given one in a range
> **POST** /graphql
```graphql
query Query {
previous(port: 3007, start: 3000, end: 3010, host: "127.0.0.1")
}
```
> **200**
```json
{
"data": {
"next": 3006
}
}
```

#### Get the previous free port to a given one in a range
> **POST** /graphql
```graphql
query Query {
previous(port: 3007, start: 3000, end: 3010, host: "127.0.0.1")
}
```
> **200**
```json
{
"data": {
"previous": 3006
}
}
```

#### Get a random free port in a range
> **POST** /graphql
```graphql
query Query {
random(start: 3000, end: 3010, host: "127.0.0.1")
}
```
> **200**
```json
{
"data": {
"random": 3265
}
}
```

## Author
[Davide Caruso](https://about.me/davidecaruso)

## License
Licensed under [MIT](LICENSE).
Binary file added logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

from aiohttp import web
from dotenv import load_dotenv, find_dotenv

from src.controller import resolve
from src.wazowski import Wazowski

load_dotenv(find_dotenv())

app = web.Application()
app.add_routes([web.post('/graphql', resolve)])

if __name__ == '__main__':
port = int(os.getenv('PORT'))
web.run_app(app, port=port if Wazowski.is_port_free(port) else Wazowski.next_free_port(port))
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aiohttp
graphene
python-dotenv
16 changes: 16 additions & 0 deletions src/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from aiohttp import web

from src.graphql import schema


async def resolve(request):
body = await request.json()
try:
result = schema.execute(str(body['query']))
return web.json_response({
'data': dict(result.data.items()) if result.data else None
}, status=200)
except Exception as e:
return web.json_response({
'error': e
}, status=500)
59 changes: 59 additions & 0 deletions src/graphql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from graphene import ObjectType, String, Int, Boolean, List, Schema

from src.wazowski import Wazowski


class Query(ObjectType):
"""
GraphQL queries and resolvers
"""

list = List(
of_type=Int,
start=Int(default_value=Wazowski.FIRST_PORT),
end=Int(default_value=Wazowski.LAST_PORT),
host=String(default_value=Wazowski.HOST)
)

check = Boolean(
port=Int(required=True),
host=String(default_value=Wazowski.HOST)
)

next = Int(
port=Int(required=True),
start=Int(default_value=Wazowski.FIRST_PORT),
end=Int(default_value=Wazowski.LAST_PORT),
host=String(default_value=Wazowski.HOST)
)

previous = Int(
port=Int(required=True),
start=Int(default_value=Wazowski.FIRST_PORT),
end=Int(default_value=Wazowski.LAST_PORT),
host=String(default_value=Wazowski.HOST)
)

random = Int(
start=Int(default_value=Wazowski.FIRST_PORT),
end=Int(default_value=Wazowski.LAST_PORT),
host=String(default_value=Wazowski.HOST)
)

def resolve_list(self, info, start, end, host):
return Wazowski.free_ports(start, end, host)

def resolve_check(self, info, port, host):
return Wazowski.is_port_free(port, host)

def resolve_next(self, info, port, start, end, host):
return Wazowski.next_free_port(port, start, end, host)

def resolve_previous(self, info, port, start, end, host):
return Wazowski.previous_free_port(port, start, end, host)

def resolve_random(self, info, start, end, host):
return Wazowski.random_free_port(start, end, host)


schema = Schema(query=Query)
Loading

0 comments on commit f04925c

Please sign in to comment.