diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..82bfcb1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.7.4-slim-buster + +# WORKDIR /app + +# RUN useradd -m -r 2kodev && \ +# chown 2kodev /app + +COPY requirements.txt ./ +RUN pip install -r requirements.txt + +ARG GIT_HASH +ENV PORT=8080 +ENV GIT_HASH=${GIT_HASH:-dev} + +EXPOSE ${PORT} + +COPY . . + +# USER 2kodev + +CMD uvicorn api:app --reload --host 0.0.0.0 --port ${PORT} \ No newline at end of file diff --git a/README.md b/README.md index 5fa331c..b62a6cd 100644 --- a/README.md +++ b/README.md @@ -1 +1,24 @@ -# domino-contest-api \ No newline at end of file +# domino-contest-api + +Project template for a Dominoes python player implementing an API required to interact with a Manager like program. + +## How to run + +This player is implemented using [FastApi](https://fastapi.tiangolo.com). To set it up execute from the project root: +```bash +$ uvicorn api:app --reload --port X +``` +where `X` is an open and valid port + +## Docker + +The docker image can be built running: +```bash +$ docker build --build-arg GIT_HASH=${GIT_HASH::7} -t . +``` +replace `` with the desired image tag + +After building the image, run the container with the following command: +```bash +$ docker run --rm +``` \ No newline at end of file diff --git a/api.py b/api.py index b193a23..bc17a09 100644 --- a/api.py +++ b/api.py @@ -1,6 +1,27 @@ +""" +Copyright © 2022 2kodevs + +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. +""" from simple import Random from typing import Tuple -from fastapi import FastAPI +from fastapi import FastAPI, status import args as arguments app = FastAPI() @@ -22,4 +43,8 @@ def log(log: Tuple): @app.post("/step") def step(heads: Tuple[int, int]): return player.step(heads) - \ No newline at end of file + + +@app.get('/healthcheck', status_code=status.HTTP_200_OK) +def perform_healthcheck(): + return {'healthcheck': 'Everything OK!'} \ No newline at end of file diff --git a/args.py b/args.py index 05f05ba..9f776f8 100644 --- a/args.py +++ b/args.py @@ -1,3 +1,24 @@ +""" +Copyright © 2022 2kodevs + +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. +""" from pydantic import BaseModel from typing import List, Tuple from enum import Enum diff --git a/player.py b/player.py index 05d15c0..9b90093 100644 --- a/player.py +++ b/player.py @@ -1,6 +1,26 @@ +""" +Copyright © 2022 2kodevs + +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. +""" import random - class BasePlayer: def __init__(self, name): self.name = name diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6fef6ab --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +fastapi==0.73.0 +pydantic==1.9.0 +uvicorn>=0.15.0,<0.16.0 diff --git a/simple.py b/simple.py index f38347a..fa76ba7 100644 --- a/simple.py +++ b/simple.py @@ -1,3 +1,24 @@ +""" +Copyright © 2022 2kodevs + +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. +""" from player import BasePlayer class BigDrop(BasePlayer):