Skip to content

Commit b974b0c

Browse files
authored
Merge pull request #214 from seddonym/review-chapter-10
Review chapter 10
2 parents e74ee1a + 1da39d6 commit b974b0c

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

chapter_10_production_readiness.asciidoc

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Instead, we'll use the popular Gunicorn Python/WSGI server.
4747
In addition, several options in _settings.py_ are currently unacceptable.
4848
`DEBUG=True`, is strongly recommended against for production,
4949
we'll want to set a unique `SECRET_KEY`,
50-
and as we'll see, other things will come up.
50+
and, as we'll see, other things will come up.
5151

5252
Let's go through and see if we can fix things one by one.
5353

@@ -104,6 +104,12 @@ $ *docker build -t superlists . && docker run \
104104
-it superlists*
105105
----
106106

107+
// DAVID: Incidentally I got the following error:
108+
// Bind for 0.0.0.0:8888 failed: port is already allocated.
109+
// Turned out the previous container was still running,
110+
// I just used the docker kill process you taught me about earlier.
111+
// Not sure if it's worth including that here, possibly clutter?
112+
107113

108114
==== The FTs catch a problem with static files
109115

@@ -192,6 +198,12 @@ $ *docker build -t superlists . && docker run \
192198
And if you take another manual look at your site, things should look much healthier.
193199
Let's rerun our FTs to confirm:
194200

201+
// DAVID: Incidentally as per your suggestion Harry I have just been skipping
202+
// the requirements.txt and instead just amending the pip install in the Dockerfile.
203+
// If we do that, however, we'll need to make sure readers rebuild the image each time
204+
// we add a requirement - such as at this point.
205+
206+
195207
// JAN: That doesn't work until you install whitenoise to venv on local machine
196208

197209
[role="small-code"]
@@ -208,6 +220,12 @@ Ran 3 tests in 10.718s
208220
OK
209221
----
210222

223+
// DAVID: I got
224+
// UserWarning: No directory at: /Users/david.seddon/Documents/reviewing/goat-book/src/static/
225+
// mw_instance = middleware(adapted_handler)
226+
// I think we need to move the static folder into src too, in the previous chapter.
227+
228+
211229
Phew. Let's commit that:
212230

213231
[subs="specialcharacters,quotes"]
@@ -463,7 +481,7 @@ TIP: Better to fail hard than allow a typo in an environment variable name to
463481

464482
==== Setting environment variables inside the Dockerfile
465483

466-
Now let's set that environment variable in our Dockerfile using then `ENV` directive:
484+
Now let's set that environment variable in our Dockerfile using the `ENV` directive:
467485

468486
[role="sourcecode"]
469487
.Dockerfile (ch10l007)
@@ -560,7 +578,7 @@ image::images/search-results-400-bad-request.png["Duckduckgo search results with
560578
`ALLOWED_HOSTS` is a security setting
561579
designed to reject requests that are likely to be forged, broken or malicious
562580
because they don't appear to be asking for your site
563-
(HTTP request contain the address they were intended for in a header called "Host").
581+
(HTTP requests contain the address they were intended for in a header called "Host").
564582

565583
By default, when DEBUG=True, `ALLOWED_HOSTS` effectively allows _localhost_,
566584
our own machine, so that's why it was working OK until now.
@@ -641,6 +659,13 @@ CMD gunicorn --bind :8888 superlists.wsgi:application
641659
----
642660
====
643661

662+
// DAVID: It would be nice to explain the difference between RUN and CMD.
663+
664+
// DAVID: Interestingly when I did this I put the RUN directive after the ENV
665+
// directive, which led to a KeyError: 'DJANGO_SECRET_KEY' which foxed me for a bit.
666+
// Might be worth calling out that we're running collectstatic in debug mode.
667+
668+
644669
// TODO: gitignore src/static
645670

646671
Well, it was fiddly, but that should get us to passing tests!
@@ -657,6 +682,9 @@ OK
657682

658683
We're nearly ready to ship to production!
659684

685+
// DAVID: It might be worth pointing out what Whitenoise is actually doing.
686+
// From what I understand, we're still using Django to serve static files.
687+
660688
Let's quickly adjust our gitignore, since the static folder is in a new place:
661689

662690
//0010
@@ -673,7 +701,7 @@ $ *git commit -am "Add collectstatic to dockerfile, and new location to gitignor
673701

674702
=== Switching to a nonroot user
675703

676-
TODO: this is definitely a good idea for security, needs writing up.
704+
TODO: WIP, this is definitely a good idea for security, needs writing up.
677705

678706
Dockerfile should gain some lines a bit like this:
679707

@@ -707,7 +735,6 @@ $ *rm src/db.sqlite3*
707735
$ *touch src/db.sqlite3* # otherwise the --mount type=bind will complain
708736
----
709737
710-
711738
Now if you run the tests, you'll see they fail;
712739
713740
[role="small-code"]
@@ -721,6 +748,10 @@ selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
721748
element: [id="id_list_table"]; [...]
722749
----
723750
751+
// DAVID: Got me thinking, I'm not always clear when I need to rebuild the image.
752+
// I would have thought I might need to do it here, but I didn't. Might be worth
753+
// explaining in the previous chapter when we do.
754+
724755
And you might spot in the browser that we just see a minimal error page,
725756
with no debug info (try it manually if you like):
726757
@@ -756,7 +787,7 @@ I mean, yes, separating the concepts of handlers and loggers and filters,
756787
and making it all configurable in a nested hierarchy is all well and good
757788
and covers every possible use case,
758789
but sometimes you just wanna say "just print stuff to stdout pls",
759-
and you wish that configuring the simplest thing was a little easier].
790+
and you wish that configuring the simplest thing was a little easier.].
760791
761792
Here's pretty much the simplest possible logging config
762793
which just prints everything to the console (ie standard out).
@@ -784,7 +815,6 @@ Rebuild and restart our container,
784815
try the FT again (or submitting a new list item manually)
785816
and we now should see a clear error message:
786817
787-
788818
----
789819
Internal Server Error: /lists/new
790820
Traceback (most recent call last):

0 commit comments

Comments
 (0)