R
Railway•12mo ago
lplpqq

guys why do railway return `Application failed to respond` whenever im trying to access my app

here is my code
import aiohttp
from aiohttp import web

async def request_handler(request):
print(f"Received request: {request.method} {request.path}")
return web.Response(text="Hello, world!")

async def on_startup(app):
print("Server started")

async def on_shutdown(app):
print("Server shutting down")

app = web.Application()
app.router.add_get('/', request_handler)

app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)

if __name__ == "__main__":
web.run_app(app, port=8080)
import aiohttp
from aiohttp import web

async def request_handler(request):
print(f"Received request: {request.method} {request.path}")
return web.Response(text="Hello, world!")

async def on_startup(app):
print("Server started")

async def on_shutdown(app):
print("Server shutting down")

app = web.Application()
app.router.add_get('/', request_handler)

app.on_startup.append(on_startup)
app.on_shutdown.append(on_shutdown)

if __name__ == "__main__":
web.run_app(app, port=8080)
No description
55 Replies
Percy
Percy•12mo ago
Project ID: N/A
Fragly
Fragly•12mo ago
make sure you listen on the PORT railway provides take a look at this: https://docs.railway.app/guides/fixing-common-errors
lplpqq
lplpqq•12mo ago
hello, thanks for rapid answerm where i can get the port provided by railway?
Fragly
Fragly•12mo ago
it's an environment variable
lplpqq
lplpqq•12mo ago
its empty 😦
No description
Fragly
Fragly•12mo ago
It's not stated there by default, it's a railway provided variable learn more here: https://docs.railway.app/guides/variables#railway-provided-variables
lplpqq
lplpqq•12mo ago
ok, i guess it is 7981
Brody
Brody•12mo ago
yes fragly that would work, but it's not the proper solution
Fragly
Fragly•12mo ago
it changes between deployments oh ok
Brody
Brody•12mo ago
@lplpqq please use gunicorn in your start command https://docs.railway.app/guides/fixing-common-errors#python--gunicorn
lplpqq
lplpqq•12mo ago
how can i replace webapp.ru with uvicorn here
Brody
Brody•12mo ago
sorry my mistake, I meant gunicorn
lplpqq
lplpqq•12mo ago
actually that doesnt matter they are similar
Brody
Brody•12mo ago
it does matter you'd want a Procfile similar to this https://github.com/railwayapp-templates/flask/blob/main/Procfile
lplpqq
lplpqq•12mo ago
but i dont know how to replace gunicorn in my case just make app variable global and remove main functio?
Brody
Brody•12mo ago
app is already global, you should not need to touch the code you showed
lplpqq
lplpqq•12mo ago
Thanks!!! that worked, port is different on each deploy, right?
Fragly
Fragly•12mo ago
yes
Brody
Brody•12mo ago
fragly 😠
Fragly
Fragly•12mo ago
<:e_guh:1114526512339763341> am I wrong?
Brody
Brody•12mo ago
you helped them run a development server
lplpqq
lplpqq•12mo ago
i didnt get you, its local, isnt that right?
Brody
Brody•12mo ago
from the code you showed me, app is a global variable
Fragly
Fragly•12mo ago
lplpqq
lplpqq•12mo ago
im talking about this code
Fragly
Fragly•12mo ago
wdym brody 😭
Brody
Brody•12mo ago
then yes make app global
lplpqq
lplpqq•12mo ago
aight, just a moment
Brody
Brody•12mo ago
you need to use gunicorn too
lplpqq
lplpqq•12mo ago
now its good?
Brody
Brody•12mo ago
does it work with gunicorn?
lplpqq
lplpqq•12mo ago
wait, but now i need to fill railway.json file, right?
Brody
Brody•12mo ago
yes, or a procfile, up to you
lplpqq
lplpqq•12mo ago
gunicorn __main__:app is this the full startCommand?
Brody
Brody•12mo ago
try it, trying is a good way of learning
lplpqq
lplpqq•12mo ago
hypercorn main:app --bind "[::]:$PORT" this is command from fastapi template
Brody
Brody•12mo ago
are you using fastapi?
lplpqq
lplpqq•12mo ago
no, but it is from fastapi template
Brody
Brody•12mo ago
right but you arent using fastapi
lplpqq
lplpqq•12mo ago
yeah, but i guess commands are the same
Brody
Brody•12mo ago
i would check instead of assuming
lplpqq
lplpqq•12mo ago
i know, i know guys, bot was working the second ago but now log says /bin/bash: line 1: gunicorn: command not found aahhh i need to put it into requirements,txt aight, ive done, its building... deploying Failed to find attribute 'app' in 'main'.
lplpqq
lplpqq•12mo ago
but here is the code
Brody
Brody•12mo ago
i'd strongly recommend you familiarize yourself with aiohttp and gunicorn
lplpqq
lplpqq•12mo ago
ok, thanks, but how am i supposed to fix that
Brody
Brody•12mo ago
familiarize yourself with the technologies you are using, we unfortunately cant offer code help
lplpqq
lplpqq•12mo ago
i fixed this issue, now no errors in log, but my bot doesnt respond
No description
Brody
Brody•12mo ago
my bot doesnt respond
going to need more info than that
lplpqq
lplpqq•12mo ago
guys, it works great if i just replace config.app_port with os.getenv('PORT'), but why this way is discouraged?
Brody
Brody•12mo ago
that starts a development server
lplpqq
lplpqq•12mo ago
also that doesnt require any changes in railway.json file that is what?
Brody
Brody•12mo ago
it uses more resources and thus will cost you more to run on railway
lplpqq
lplpqq•12mo ago
wait, what uses more resources
web.run_app(app, port=app_port, loop=loop,
access_log=None)
web.run_app(app, port=app_port, loop=loop,
access_log=None)
over
guvicorn app:app
guvicorn app:app
?
Brody
Brody•12mo ago
running your app in development mode will use the most resources
lplpqq
lplpqq•12mo ago
development mode is when i start the script using
python
python
command in railway.json file? alright guys, thanks for help, i gotta go right now, but i will reach out again very soon, because i havent managed to start the bot
Want results from more Discord servers?
Add your server