Skip to content

Commit

Permalink
feat: adds news service
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Sep 16, 2023
1 parent 1ef9df9 commit 80531dd
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 0 deletions.
34 changes: 34 additions & 0 deletions services/news/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# 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
9 changes: 9 additions & 0 deletions services/news/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.Phony: run
run:
@echo "Running..."
@gunicorn -b 0.0.0.0:4444 'app:server'

.Phony: run-dev
run-dev:
@echo "Running..."
@python -m app
8 changes: 8 additions & 0 deletions services/news/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from app.server import server
from os import environ

def main():
print("Running server")
port = int(environ.get("PORT", 4444))

server.run(debug=True, port=port)
4 changes: 4 additions & 0 deletions services/news/app/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from app import main

if __name__ == "__main__":
main()
9 changes: 9 additions & 0 deletions services/news/app/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import requests

def fetch_top_news():
response = requests.get('https://hacker-news.firebaseio.com/v0/topstories.json')
return response.json()

def fetch_details_for_story(story_id):
response = requests.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')
return response.json()
19 changes: 19 additions & 0 deletions services/news/app/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask import Flask, render_template
from app.api import fetch_top_news, fetch_details_for_story

server = Flask(__name__)

@server.route("/")
def root():
news = fetch_top_news()
return render_template("index.html", posts=news[:10])

@server.route("/story/<item_id>/link")
def story_link(item_id):
details = fetch_details_for_story(item_id)
return render_template("_story_link.html", details=details)

@server.route("/story/<item_id>/details")
def story_details(item_id):
details = fetch_details_for_story(item_id)
return render_template("_story_details.html", details=details)
5 changes: 5 additions & 0 deletions services/news/app/templates/_story_details.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>Story Details</h2>
<div>
<div>{{details.title}}</div>
<div>{{details.text}}</div>
</div>
8 changes: 8 additions & 0 deletions services/news/app/templates/_story_link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<li class="p-2">
<a href="{{ details.url }}">{{ details.title }}</a>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-2 rounded"
hx-get="/story/{{ details.id }}/details" hx-target="#story-details" hx-swap="innerHTML" hx-trigger="click"
hx-indicator="#loading">
Show preview
</button>
</li>
45 changes: 45 additions & 0 deletions services/news/app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />

<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/htmx.org/dist/ext/ws.js"></script>
<script src="https://cdn.tailwindcss.com"></script>

<style type="text/css">
.htmx-indicator {
opacity: 0;
transition: opacity 500ms ease-in;
}

.htmx-request .htmx-indicator {
opacity: 1
}
</style>

<title>Latest Tech News</title>
</head>

<body>
<div class="w-2/3 mx-auto p-4">
<h1 class="uppercase text-blue-400 font-bold">
Latest Stories
</h1>

<ul>
{% for post in posts %}
<li hx-get="/story/{{ post }}/link" hx-target="this" hx-swap="outerHTML" hx-trigger="load">loading...</li>
{% endfor %}
</ul>
</div>

<section class="w-1/2 mx-auto p-4">
<div id="story-details"></div>
<span id="loading" class="htmx-indicator">Loading details...</span>
</section>
</body>

</html>
3 changes: 3 additions & 0 deletions services/news/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==2.3.3
requests==2.31.0
gunicorn==21.2.0

0 comments on commit 80531dd

Please sign in to comment.