英文:
Heroku Go app is no longer working after a few minutes
问题
我在GitHub页面上运行React应用程序时遇到了问题,所以我选择尝试在我的Heroku上的Go后端上提供文件。起初,我通过主Go后端提供React应用程序,成功地提供了React应用程序,但在此之后,我的Go应用程序中的其他路由都无法工作,这些路由是我React应用程序运行所需的。
因此,我选择创建一个新的Heroku Go应用程序,并将后端和前端分离到不同的Heroku Go应用程序中。前端Go应用程序正常运行,但后端会间歇性地工作。我知道Heroku的免费应用程序会在一段不活动时间后进入休眠状态,但我指的是应用程序在线几分钟后,突然切换回默认模式,显示"no such app"。
前端Heroku Go应用程序:
// Route: Delete Graph from database
func RouteDefault(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.ServeFile(w, r, "static/index.html")
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "9000" // 如果未指定端口,则使用默认端口
}
// HTTPRouter 设置和路由
router := httprouter.New()
router.GET("/", RouteDefault)
// 如果找不到,查找静态文件
static := httprouter.New()
static.ServeFiles("/*filepath", http.Dir("static"))
router.NotFound = static
handler := cors.AllowAll().Handler(router)
fmt.Println(http.ListenAndServe(":"+port, handler))
}
后端Heroku Go应用程序:
func main() {
// BasicAuth 用户名和密码
user := ""
pass := ""
port := os.Getenv("PORT")
if port == "" {
port = "9000" // 如果未指定端口,则使用默认端口
}
DefaultUser()
// HTTPRouter 设置和路由
router := httprouter.New()
router.POST("/login/", BasicAuth(RouteLogin, user, pass))
router.POST("/upload/", JWTAuth(RouteUpload))
router.POST("/graph/", JWTAuth(RouteGetGraph))
router.GET("/autologin/", JWTAuth(RouteAutoLogin))
handler := cors.AllowAll().Handler(router)
fmt.Println(http.ListenAndServe(":"+port, handler))
}
前端:https://grafulatordemo.herokuapp.com/
后端:https://grafulator.herokuapp.com/
英文:
I had trouble getting my React app to run on GitHub pages, so I chose to try and serve the files on my Go backend which is on Heroku. At first I was serving the React app through the main Go backend, which did serve the React app successfully but none of my other routes would work after that in the Go app, the routes needed for my React app to operate.
So I chose to create a new Heroku Go app and separate the backend and frontend on different Heroku Go apps. The frontend Go app is running fine, but the backend will intermittently work. I understand Heroku free apps go into a sleep state with a period of inactivity, but I am literally talking about the app being online for a few minutes, and then all of sudden just switching back to the default mode of saying "no such app"
Frontend Heroku Go app:
// Route: Delete Graph from database
func RouteDefault(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.ServeFile(w, r, "static/index.html")
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
// HTTPRouter Settings and Routes
router := httprouter.New()
router.GET("/", RouteDefault)
// if not found look for a static file
static := httprouter.New()
static.ServeFiles("/*filepath", http.Dir("static"))
router.NotFound = static
handler := cors.AllowAll().Handler(router)
fmt.Println(http.ListenAndServe(":"+port, handler))
}
Backend Heroku Go app:
func main() {
// BasicAuth username and password
user := ""
pass := ""
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
DefaultUser()
// HTTPRouter Settings and Routes
router := httprouter.New()
router.POST("/login/", BasicAuth(RouteLogin, user, pass))
router.POST("/upload/", JWTAuth(RouteUpload))
router.POST("/graph/", JWTAuth(RouteGetGraph))
router.GET("/autologin/", JWTAuth(RouteAutoLogin))
handler := cors.AllowAll().Handler(router)
fmt.Println(http.ListenAndServe(":"+port, handler))
}
Frontend: https://grafulatordemo.herokuapp.com/
Backend: https://grafulator.herokuapp.com/
答案1
得分: 0
运行以下命令:heroku apps -A
,并分享相关的输出,以便进行故障排除。
英文:
run the command: heroku apps -A
and share the relevant output so it can be troubleshooted
答案2
得分: 0
问题出在Heroku的一方,联系他们的支持团队解决了这个问题。
英文:
The problem was on heroku's end, contacting their support team resolved it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论