Skip to content

Commit

Permalink
Add app root options for heroku can found template file
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Aug 12, 2015
1 parent 6f8319c commit 36dab22
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: go-plurk-robot serve
web: go-plurk-robot serve -r /app
worker: go-plurk-robot robot
4 changes: 3 additions & 1 deletion cmd/go-plurk-robot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func setupCommandFlags() {

// Add Server
cmdServe.Flags().StringVarP(&Port, "port", "p", "5000", "Specify web server port")
cmdServe.Flags().StringP("root", "r", "", "Specify app root if cannot found tempalte file")
}

// A shortcut for Add Plurk, can use with cronjob
Expand Down Expand Up @@ -86,7 +87,8 @@ var cmdServe = &cobra.Command{
Short: "Start a Web UI for robot",
Long: `Start a Web UI with analytics and some useful tool for robot and plurk`,
Run: func(cmd *cobra.Command, args []string) {
server.Serve(Port)
appRoot, _ := cmd.Flags().GetString("root")
server.Serve(Port, appRoot)
},
}

Expand Down
27 changes: 20 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

var (
cookie *sessions.CookieStore
cookie *sessions.CookieStore
appRoot string
)

type Renderer struct {
Expand All @@ -32,13 +33,15 @@ func getDatabase() (mdb *mgo.Database, err error) {
return
}

func Serve(Port string) {
func Serve(Port string, AppRoot string) {

// Fallback port to 5000 for local test
if len(Port) <= 0 {
Port = "5000"
}

appRoot = AppRoot

cookie = NewCookieStore()

server := echo.New()
Expand All @@ -64,9 +67,14 @@ func Serve(Port string) {
}

func getRenderer() *Renderer {
// Get correctly path to template file
_, filename, _, _ := runtime.Caller(1)
tmplPath := filepath.Dir(filename)
var tmplPath string
if len(appRoot) <= 0 {
// Get correctly path to template file
_, filename, _, _ := runtime.Caller(1)
tmplPath = filepath.Dir(filename)
} else {
tmplPath = appRoot + "/server"
}
templates := template.Must(template.ParseGlob(tmplPath + "/template/*.tmpl"))
templates.ParseGlob(tmplPath + "/template/*/*.tmpl")
return &Renderer{
Expand All @@ -75,8 +83,13 @@ func getRenderer() *Renderer {
}

func setupStatic(server *echo.Echo) {
_, filename, _, _ := runtime.Caller(1)
packagePath := filepath.Dir(filename)
var packagePath string
if len(appRoot) <= 0 {
_, filename, _, _ := runtime.Caller(1)
packagePath = filepath.Dir(filename)
} else {
packagePath = appRoot + "/server"
}
server.Static("/js", packagePath+"/static/js")
server.Static("/css", packagePath+"/static/css")
server.Static("/vendor", packagePath+"/static/vendor")
Expand Down

0 comments on commit 36dab22

Please sign in to comment.