登录
首页 >  Golang >  Go问答

Heroku Go 应用程序在几分钟后停止运行

来源:stackoverflow

时间:2024-02-10 19:30:23 448浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《Heroku Go 应用程序在几分钟后停止运行》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

我无法让我的 react 应用程序在 github 页面上运行,因此我选择尝试在 heroku 上的 go 后端上提供文件。起初,我通过主 go 后端为 react 应用程序提供服务,该后端确实成功地为 react 应用程序提供了服务,但此后我的其他路由都无法在 go 应用程序中工作,而这些路由是我的 react 应用程序运行所需的。

因此,我选择创建一个新的 heroku go 应用程序,并将不同 heroku go 应用程序的后端和前端分开。前端 go 应用程序运行良好,但后端会间歇性工作。我知道 heroku 免费应用程序会进入睡眠状态并在一段时间内不活动,但我实际上是在谈论该应用程序在线几分钟,然后突然切换回默认模式“没有这样的应用程序”

前端 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" // 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))
}

后端 heroku go 应用:

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))
}

前端:https://grafulatordemo.herokuapp.com/ 后端:https://grafulator.herokuapp.com/


正确答案


问题出在 Heroku 端,联系他们的支持团队解决了它。

运行命令:heroku apps -A 并共享相关输出,以便进行故障排除

好了,本文到此结束,带大家了解了《Heroku Go 应用程序在几分钟后停止运行》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>