nsidnev / fastapi-realworld-example-app

Backend logic implementation for https://github.com/gothinkster/realworld with awesome FastAPI
MIT License
2.79k stars 629 forks source link

Steps for running on windows #175

Closed stealthrabbi closed 3 years ago

stealthrabbi commented 3 years ago

The readme assumes that you're on Linux (can't use EXPORT in Windows)

stealthrabbi commented 3 years ago

Also, does postgres need to be installed on the local system? That's unclear until you go to run createdb. Ideally that would be in the readme.

nsidnev commented 3 years ago

Sorry, but I'm not developing on windows, so I'm not sure about the exact steps to run this project there. So further, only my thoughts, which I didn't check:

  1. skip parts with exporting variables
  2. create .env file in your text editor and fill it with your own DB_CONNECTION and SECRET_KEY variables. I'm not sure about this, but maybe just using postgres://postgres:postgres@localhost:5432/postgres for DB_CONNECTION will work on windows since ports are exported in docker-compose.yml
  3. run docker-compose up to start the app and postgres in docker. Alternatively, you can run docker-compose up -d db to start only postgres in docker and run alembic and uvicorn commands from cmd to start the server directly on the system.

In fact, if you have postgres installed locally on your system and it's running, then you can simply skip docker and connect this app directly to your postgres (just pass correct postgres DSN to DB_CONNECTION).

stealthrabbi commented 3 years ago

So I'd like to just use the postgres docker container and not have it installed locally. I assume I have to do this within a poetry shell, but I ran this and it looks like the app was able to connect to the DB, but when i browse to http://localhost:8000, I don't get anything.

PS C:\Projects\FastApiTests\fastapi-realworld-example-app> alembic upgrade head
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
PS C:\Projects\FastApiTests\fastapi-realworld-example-app> uvicorn app.main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [15104] using statreload
INFO:     Started server process [7216]
2021-06-26 15:55:13.999 | INFO     | uvicorn.server:serve:65 - Started server process [7216]
INFO:     Waiting for application startup.
2021-06-26 15:55:14.016 | INFO     | uvicorn.lifespan.on:startup:45 - Waiting for application startup.
2021-06-26 15:55:14.017 | INFO     | app.db.events:connect_to_db:9 - Connecting to DatabaseURL('postgresql://postgres:********@localhost:5432/postgres')
2021-06-26 15:55:14.201 | INFO     | app.db.events:connect_to_db:17 - Connection established
INFO:     Application startup complete.
2021-06-26 15:55:14.205 | INFO     | uvicorn.lifespan.on:startup:59 - Application startup complete.
2021-06-26 15:55:20.017 | INFO     | uvicorn.protocols.http.h11_impl:send:440 - 127.0.0.1:13733 - "GET / HTTP/1.1" 404

browser:

{"errors":["Not Found"]}

stealthrabbi commented 3 years ago

I can get to the swagger page at http://127.0.0.1:8000/docs however

stealthrabbi commented 3 years ago

I'm also not sure what I'm supposed to do be authorized. I didn't see where in the code or in the readme it specifies about creating a user.

nsidnev commented 3 years ago

{"errors":["Not Found"]}

It's correct. If you open swagger, you will see that all endpoints are placed under /api/ path. This application is backend only, so trying to access non-existent route will result in 404 error.

I'm also not sure what I'm supposed to do be authorized. I didn't see where in the code or in the readme it specifies about creating a user.

Check out the original specification that this project implements. It describes all routes for this application.

stealthrabbi commented 3 years ago

Thanks. That makes sense, I forgot on another toy FastAPI proj I was making, I had set up a route at / that gave a response.

OK, so I look at the spec and noticed that POST /api/users/ was available, so I created a user successfully and got an auth token. But when I click the Authorize button in swagger and provide that token, I still get 403 forbidden on the other API endpoints. I am able to exercise /api/users/login to log in with email / pass successfully.

The curl command that Swagger creates includes the Authorization token in the header, so I'm not sure what the issue is:

curl -X 'GET' \
  'http://127.0.0.1:8000/api/articles/feed?limit=20&offset=0' \
  -H 'accept: application/json' \
  -H 'Authorization: eyJ0eXAi[redacted]'
nsidnev commented 3 years ago

The authorization header should include not only the token itself, but also a special string in front of it. Typically the "Bearer" string is used in the authorization header along with the token, but this project uses "Token" because it is a specification requirement.

Therefore, if you want to login with swagger, you should pass Token {access_token} there.

stealthrabbi commented 3 years ago

Great. That did it. And I also see the JWT_TOKEN_PREFIX in config.py that Thank you very much for your patience and responsiveness.