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

Sofia/fixes second half course #19

Merged
merged 7 commits into from
Jul 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## Add Dockerfile and requirements.txt

### Task
Add the files necessary to run the gateway app as a Docker container.

Expand Down
4 changes: 2 additions & 2 deletions Building the Gateway Application/Define routes/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
2 changes: 0 additions & 2 deletions Building the Gateway Application/Define routes/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## Define Routes

We added the four functions needed in our gateway application for routing requests (in addition to `index()`, which doesn't really do anything).
Each of these four functions will send a request to the other application.
In this task, you will add the appropriate routes to all of them.
Expand Down
4 changes: 2 additions & 2 deletions Building the Gateway Application/Delete Device/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
2 changes: 0 additions & 2 deletions Building the Gateway Application/Delete Device/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## Delete Resource

In this task, we will define the `delete_device` function, which will:
- Forward the delete request to the relevant endpoint in `invsys`.
- Forward the response back to the client by creating a `Response` object from the received `response`.
Expand Down
4 changes: 2 additions & 2 deletions Building the Gateway Application/Get Devices/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
17 changes: 12 additions & 5 deletions Building the Gateway Application/Get Devices/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## Get Devices

In this task, we will define the `get_devices` function, which will:
- Forward the request to the relevant endpoint in `invsys` (depending on the URL: if the item ID is provided,
it needs to be added to the route, which in `invsys` will result in calling the `get` endpoint in the function `device`)
Expand All @@ -15,7 +13,16 @@ import requests
data = ... # from request
response = requests.get("http://0.0.0.0:5000/items/")
```
Let's keep it that way for this task, but let's also keep in mind that
This would be totally okay if you wanted to deploy your applications locally (without Docker). However,
by doing this, our application source code becomes coupled with how and where we deploy `invsys`.
Instead, we can post to `http://invsys:5000/items` and ensure that our network is set up to route `invsys` to whichever IP it is hosted on.
We will come back to this later on.
You can use `0.0.0.0` for local deployment if you like, but for checking this task, we ask
you to replace it with `invsys`, so that your URL looks something like this:
`http://invsys:5000/items` (check out the URL in the `else` clause which is already completed).
We will explain this in more detail in the next lesson. For now, you only need to know that
we use Docker under the hood to check these tasks, so `invsys` will be a designated name of a Docker Compose service and
Docker will take care of names and routing.

<div class="hint">

To create the right URL for getting a specific item, you only need to slightly modify the `get` URL that is already provided by adding the `item_id` to it.
</div>
4 changes: 2 additions & 2 deletions Building the Gateway Application/Introduction/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
2 changes: 0 additions & 2 deletions Building the Gateway Application/Introduction/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Gateway Application

The next step will be creating an API gateway backend, which acts as a router for client requests.
This means our final application will consist of two backend components: the inventory
manager and the API gateway.
Expand Down
4 changes: 2 additions & 2 deletions Building the Gateway Application/Post Device/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
2 changes: 0 additions & 2 deletions Building the Gateway Application/Post Device/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## POST Resource

In this task, we will define the `post_device` function, which will:
- Get the payload from our incoming request.
- Forward the POST request to the relevant endpoint in invsys. Do not forget the payload!
Expand Down
4 changes: 2 additions & 2 deletions Building the Gateway Application/Put Device/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
2 changes: 0 additions & 2 deletions Building the Gateway Application/Put Device/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## Put Resource

In this task we will define the `put_device` function, which will:
- Get the payload from our incoming request.
- Forward the PUT request to the relevant endpoint in `invsys`. Do not forget the payload!
Expand Down
4 changes: 2 additions & 2 deletions Composing the System/Deploying without Docker/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
3 changes: 0 additions & 3 deletions Composing the System/Deploying without Docker/task.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## Deploying locally without Docker


While this course is focused on deploying with Docker, you can also deploy locally without it.

You will need to change the routes in the `gateway`:
Expand Down
4 changes: 2 additions & 2 deletions Composing the System/Docker-compose/invsys/dal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def pull_db():
db_ = getattr(g, '_database', None)
if db_ is None:
db_ = g._database = shelve.open("storage.db")
db_ = g._database = shelve.open("storage")
return db_


Expand Down Expand Up @@ -89,6 +89,6 @@ def delete_device(identifier):
}

# Initialize db with some data already in it
with shelve.open('storage.db') as db:
with shelve.open('storage') as db:
for key, value, in devices.items():
db[key] = value
17 changes: 13 additions & 4 deletions Composing the System/Docker-compose/task.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
## Composing our system
Now we have two Flask apps: Invsys and Gateway.
We want them both to be deployed, and we want Gateway to be able to send
requests to Invsys using `invsys` instead of a specific IP.
Expand Down Expand Up @@ -28,10 +27,20 @@ of the service is what we use to target that service in the network. Since we ar
Invsys service as `invsys`, any requests from our application targeting `invsys` will be routed
to the correct IP for that service. We are using the `build` flag in each case, which instructs the
tool to search for the `gateway` and `invsys` subdirectories and use their Dockerfiles to build
the image. The `ports` flag is the same as the one used in the `docker run` command: it routes the
the image.

The `ports` flag is the same as the one used in the `docker run` command: it routes the
external Docker machine port to the internally exposed port of our containers. You'll notice
that we haven't added a port mapping for `invsys`. This is intentional because we want to ensure that requests
go through the gateway. If you want to allow direct targeting of `invsys` as well, you would
just need to add `ports: - "5000:5000"`.
just need to add `ports: - "5000:5000"` to the `invsys` service description.

<div class="hint" title="Secure port publishing">
By default, when you bind container ports to the host, they become available on all network interfaces.
For experiments, this is not critical, but when deploying real applications,
you should pay careful attention to which ports should be accessible from the outside world and which should not.

You can read more about how to properly publish ports [here](https://docs.docker.com/network/#published-ports).
</div>

Now, add the contents to the `docker-compose.yaml` file and run the application. Test it with Postman like you did before.
Now, add the contents to the `docker-compose.yaml` file and run the application. Test it with HTTPie like you did before.
2 changes: 0 additions & 2 deletions Composing the System/What's next/task.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
## What else to explore

Here are some recommended resources for further reading:

[Flask by example: Part 1](https://realpython.com/flask-by-example-part-1-project-setup/) — a tutorial.
Expand Down