2 months ago
I'm trying to set up an API in Go, with HTML and JavaScript as the frontend. It works locally, but I'd like support to deploy it to a server.
Pinned Solution
2 months ago
heres who to deploy go + mysql on railway:
quick steps:
add mysql database to your project (click + new → database → mysql)
deploy your go app from github repo (+ new → github repo). railway auto-detects go projects from go.mod
super important , make sure your go app listens on railway's PORT variable:
go
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.ListenAndServe(":"+port, handler)connect to mysql using the env variables railway gives you automatically. in your go app use:
go
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
os.Getenv("MYSQLUSER"),
os.Getenv("MYSQLPASSWORD"),
os.Getenv("MYSQLHOST"),
os.Getenv("MYSQLPORT"),
os.Getenv("MYSQLDATABASE"),
)in your go service settings → variables tab, reference the mysql service variables
generate domain under settings → networking
your html/js frontend just needs to be served by your go app and it'll deploy together. the PORT thing catches most people , railway assigns a random port so you gotta use their variable.
3 Replies
2 months ago
heres who to deploy go + mysql on railway:
quick steps:
add mysql database to your project (click + new → database → mysql)
deploy your go app from github repo (+ new → github repo). railway auto-detects go projects from go.mod
super important , make sure your go app listens on railway's PORT variable:
go
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.ListenAndServe(":"+port, handler)connect to mysql using the env variables railway gives you automatically. in your go app use:
go
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
os.Getenv("MYSQLUSER"),
os.Getenv("MYSQLPASSWORD"),
os.Getenv("MYSQLHOST"),
os.Getenv("MYSQLPORT"),
os.Getenv("MYSQLDATABASE"),
)in your go service settings → variables tab, reference the mysql service variables
generate domain under settings → networking
your html/js frontend just needs to be served by your go app and it'll deploy together. the PORT thing catches most people , railway assigns a random port so you gotta use their variable.
2 months ago
lmk if you hit any snags, happy to help further!
2 months ago
Thank you very much, I managed to solve it.
Status changed to Open brody • 2 months ago
Status changed to Solved brody • 2 months ago