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

Add main source code files #1

Merged
merged 2 commits into from
May 15, 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
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG PYTHON_VERSION=3.12.2
FROM python:${PYTHON_VERSION}-slim as base

WORKDIR /app

COPY . .

RUN python3 -m pip install -r requirements.txt

EXPOSE 8080


CMD gunicorn 'counter-service:app' --bind=0.0.0.0:8080
61 changes: 61 additions & 0 deletions counter-service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from flask import Flask, request, jsonify
import os

app = Flask(__name__)

# define path to file to store the counter
COUNTER_FILE = "./data/counter.txt"

def read_counter():
"""
reads the counter file for the counter value. If the file doesnot
exist it returns 0.

Returns:
int: The current value of the counter
"""

if os.path.exists(COUNTER_FILE):
with open(COUNTER_FILE, "r") as f:
return int(f.read().strip())
else:
return 0

def update_counter(counter):
"""
updates the counter file with the passed counter argument

Args:
counter (int): The new counter value to be stored in the file

"""
with open(COUNTER_FILE,"w") as f:
f.write(str(counter))

@app.route('/', methods=['GET','POST'])
def handle_request():

counter = read_counter()

if request.method == 'POST':
counter+=1
update_counter(counter)
return f"updated the counter value. new value is ${counter}"
else:
return f"current posts request count is ${counter}"

@app.route('/health', methods=['GET'])
def health_check():

try:
read_counter()
return jsonify({"status": "healthy"}), 200
except Exception as e:
return jsonify({"status": "unhealthy", "reson": str(e)}), 500

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False)




1 change: 1 addition & 0 deletions data/counter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
blinker==1.8.1
click==8.1.7
Flask==3.0.3
gunicorn==22.0.0
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==2.1.5
packaging==24.0
Werkzeug==3.0.3
Loading