在Heroku上使用Angular和Go

huangapple go评论86阅读模式
英文:

Angular and Go on heroku

问题

我正在尝试将我的应用程序部署到Heroku上。前端使用Angular,后端使用Go。

我按照这个教程进行操作:http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html

然而,当我访问我的Heroku应用程序的域名时,我得到的是应用程序的目录(包括git中的所有内容)。当我导航到/app文件夹(我的Angular应用程序所在的位置)时,它显示了应用程序。

我不希望我的应用程序位于:

foobar.herokuapp.com/app/#/

我希望它位于:

foobar.herokuapp.com

我的应用程序目录的简化版本如下:

foobar
 - /app
 - /server/server.go
 - .godir             // 包含"app"
 - Procfile           // 包含"web: server"

我在/server文件夹内运行了"go get"命令。

以下命令可以正常工作:

$ PORT=5000 demoapp

$ curl -i http://127.0.0.1:5000/

这是我的简单server.go文件:

package main

import (
"github.com/gorilla/handlers"
"log"
"net/http"
"os"
)

func main() {
log.Println("Starting Server")
http.Handle("/", logHandler(http.FileServer(http.Dir("../app/"))))

log.Println("Listening...")
panic(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}

func logHandler(h http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, h)

}

英文:

I am trying to put my app on Heroku. I am using angular on the front-end and Go on the backend.

I followed this tutorial http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html

However, when I go to the domain of my heroku app, I get the directory of my app (everything in the git). When I navigate to the /app folder, (where my angular app lives) it shows the app.

I don't want my app to be at

foobar.herokuapp.com/app/#/

I want it to be at

foobar.herokuapp.com

A simplified version of my app directory is:

foobar
 - /app
 - /server/server.go
 - .godir             // contains "app"
 - Procfile           // contains "web: server"

I ran "go get" from inside my /server folder

These work:

$ PORT=5000 demoapp

$ curl -i http://127.0.0.1:5000/

Here is my simple server.go

package main

import (
"github.com/gorilla/handlers"
"log"
"net/http"
"os"
)

func main() {
log.Println("Starting Server")
http.Handle("/", logHandler(http.FileServer(http.Dir("../app/"))))

log.Println("Listening...")
panic(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}

func logHandler(h http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, h)

}

答案1

得分: 1

将您的FileServer目录从"../app/"更改为"app/"(相对路径)或"/app/app/"(绝对路径)应该解决这个问题。

http.Handle("/", logHandler(http.FileServer(http.Dir("app/"))))

当Heroku执行Procfile命令时,您的项目根目录是工作文件夹。它的绝对路径是/app,这就是为什么../app会将您带回项目根目录。

尽管您的server.go存储在./server子文件夹中,但它仍然会被编译到项目根目录中,使用的是package main

英文:

Changing your FileServer directory from "../app/" to "app/" (relative) or "/app/app/" (absolute) should solve the issue.

http.Handle("/", logHandler(http.FileServer(http.Dir("app/"))))

Your project root is the work folder when Heroku executes the Procfile command. It has the absolute path /app which is why ../app brings you back to your project root.

Although your server.go is stored in the ./server subfolder it is still compiled into the project root with package main.

huangapple
  • 本文由 发表于 2013年12月30日 10:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/20832559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定