Skip to content

Commit

Permalink
feat: adds news to the main app
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Sep 16, 2023
1 parent 80531dd commit e12fd97
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .watch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
run: 'docker-compose restart chat-app'
change: 'services/chat/**/*'
ignore: [ 'services/chat/target/**/*' ]

- name: restart news-app
run: 'docker-compose up news-app --build'
change: 'services/news/**/*'
13 changes: 9 additions & 4 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@
}
</style>

<title>Micro frontend</title>
<title>Distributex</title>
</head>

<body>
<div class="w-full p-4">
<h1 class="uppercase text-blue-400 font-bold" hx-target="find">HTMX rox</h1>
<div class="w-2/3 mx-auto">
<h1 class="uppercase text-blue-400 font-bold" hx-target="find">Distributex - HTMX & NGINX ❤️</h1>
<p>
This is a POC of micro frontend architecture using HTMX and TailwindCSS and multiple backends.
</p>
</div>

<div class="display-4 flex">
<div class="display-4 flex justify-center">
<div class="m-4 p-4 border-1 al">
<div hx-get="/todos" hx-trigger="load" hx-target="this" hx-swap="innerHTML"></div>
</div>

<div class="m-4 p-4 border-1 al">
<div hx-get="/chat" hx-trigger="load" hx-target="this" hx-swap="innerHTML"></div>
</div>
</div>

<div class="w-full mx-auto p-4 border-1">
<div hx-get="/news" hx-trigger="load" hx-target="this" hx-swap="innerHTML"></div>
</div>
</body>

</html>
11 changes: 9 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ services:
todos-app:
build: services/todos
ports:
- "4001:4001" # Web Server
- "4001:4001"
volumes:
- ./services/todos:/app

chat-app:
build: services/chat
ports:
- "4002:4002" # Web Server
- "4002:4002"
volumes:
- ./services/chat/src:/app/src

news-app:
build: services/news
ports:
- "4003:4003"
volumes:
- ./services/news:/app
4 changes: 4 additions & 0 deletions nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ server {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /news {
proxy_pass http://news-app:4003/news;
}
}
13 changes: 13 additions & 0 deletions services/news/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.9-alpine

RUN mkdir -p /application
WORKDIR /application

COPY requirements.txt /application
RUN pip install -r requirements.txt

ADD . /application

EXPOSE 4003

CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:4003", "app:server"]
2 changes: 1 addition & 1 deletion services/news/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.Phony: run
run:
@echo "Running..."
@gunicorn -b 0.0.0.0:4444 'app:server'
@gunicorn -b 0.0.0.0:4003 'app:server'

.Phony: run-dev
run-dev:
Expand Down
2 changes: 1 addition & 1 deletion services/news/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

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

server.run(debug=True, port=port)
2 changes: 2 additions & 0 deletions services/news/app/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import requests
import functools

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

@functools.cache
def fetch_details_for_story(story_id):
response = requests.get(f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json')
return response.json()
7 changes: 4 additions & 3 deletions services/news/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

server = Flask(__name__)

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

@server.route("/story/<item_id>/link")
@server.route("/news/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")
@server.route("/news/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: 2 additions & 3 deletions services/news/app/templates/_story_details.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<h2>Story Details</h2>
<div>
<div>{{details.title}}</div>
<div class="w-1/2 mx-auto">
<div>{{details.text}}</div>
<a href="{{details.url}}">Read more...</a>
</div>
3 changes: 2 additions & 1 deletion services/news/app/templates/_story_link.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<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-get="/news/story/{{ details.id }}/details" hx-target="next #show-details" hx-swap="innerHTML" hx-trigger="click"
hx-indicator="#loading">
Show preview
</button>
<div id="show-details"></div>
</li>
2 changes: 1 addition & 1 deletion services/news/app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h1 class="uppercase text-blue-400 font-bold">

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

0 comments on commit e12fd97

Please sign in to comment.