Port Assignment: Go Web Server
sentinel1909
PROOP

2 years ago

Hello,

I realize this is a stupid question, but it's something I reallly struggle with on this service. I'm learning Go, and am building out a simple web server that I'd like to deploy on Railway. I've been using the Railway CLI to setup and deploy.

It's not clear to me when to set the port to listen on. I thought it would be automatic on deployment (port is set within the app) but it's not. When I add an environment variable later for the port, it has no effect and doesn't work.

I've gone through the docs a few times, but it's not clear to me how to do it.

How do I set the port value that a Go web server will use during deployment?

Thanks,

~Jeff Mitchell

6 Replies

2 years ago

Hi,

It looks like I'm not seeing any active projects right now in your account. Can you describe the error you were hitting in more detail?

And can I make this thread public? I think this is something a lot of people struggle with and its an important topic.


2 years ago

Railway makes this very easy, for most use cases you don't have to set anything since Railway injects a PORT variable that your app is expected to listen on, here's a simple code example -

// handler code and whatnot

port := cmp.Or(os.Getenv("PORT"), "3000")

http.ListenAndServe(":" + port, mux)

sentinel1909
PROOP

2 years ago

I've started a project in my "team" environment called "captivating-grace". It's running a very simple web server with one /health_check endpoint. After creating a blank project, I used the Railway CLI to deploy, via railway login, then railway link, and finally railway up. I created a domain and attached it to the project. The port in the web server is 8080. I think the mistake I've been making is not grabbing the environment variable as Brody hints at.

Totally ok with making this thread public!

Thanks,

~Jeff


2 years ago

Let me know if my example helps!


sentinel1909
PROOP

2 years ago

Alright! Got it, thanks @brody, your suggestion definitely did the trick!


2 years ago

Awsome!


Loading...