gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 578 forks source link

Setting HOST environment variable doesn't reflect in assetsPath or rootPath #1889

Closed sureshprasanna70 closed 4 years ago

sureshprasanna70 commented 4 years ago

Description

I have a new buffalo application that is currently deployed behind a proxy. The proxy config looks similar to

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass /app http://0.0.0.0:3000/
    ProxyPassReverse /app http://0.0.0.0:3000/

    ServerName example.com
</VirtualHost>

I have initialized my buffalo app in app.go as

    app = buffalo.New(buffalo.Options{
            Env:         ENV,
            SessionName: "_coke_session",
                    Host: "example.com/app"
        })

Expected Behaviour:

Any path generated by the go application to be prefixed with value of the HOST variable.

Actual Behaviour:

None of the paths are prefixed with the HOST variable. This causes 404 for all assets and routes of the application.

Info

-> Go: Checking installation
✓ The `go` executable was found on your system at: /usr/local/go/bin/go

-> Go: Checking minimum version requirements
✓ Your version of Go, 1.13.4, meets the minimum requirements.

-> Go: Checking GOPATH
✘ You do not appear to be operating inside of your GOPATH.
Things to check:

* Capitalization - make sure that your capitalization of ~/work matches that of ~/work/coke
* `src` - make sure that you are working under ~/work/src

GOPATH: ~/work
PWD: ~/work/coke
Build Sources: ~/work/src

For help setting up your Go environment please follow the instructions for you platform at:

https://www.gopherguides.com/courses/preparing-your-environment-for-go-development

-> Go: Checking Package Management
⚠ You do not appear to be using a package management system.

It is strongly suggested that you use one of the following package management systems:

* Go Modules (Recommended) - https://gobuffalo.io/en/docs/gomods
* Dep - https://github.com/golang/dep

For help setting up your Go environment please follow the instructions for you platform at:

https://www.gopherguides.com/courses/preparing-your-environment-for-go-development

-> Go: Checking PATH
✓ Your PATH contains ~/work/bin.

-> Node: Checking installation
✓ The `node` executable was found on your system at: /usr/bin/node

-> Node: Checking minimum version requirements
✓ Your version of Node, v10.18.1, meets the minimum requirements.

-> NPM: Checking installation
✓ The `npm` executable was found on your system at: /usr/bin/npm

-> NPM: Checking minimum version requirements
✓ Your version of NPM, 6.13.4, meets the minimum requirements.

-> Yarn: Checking installation
✓ The `yarnpkg` executable was found on your system at: /usr/bin/yarnpkg

-> Yarn: Checking minimum version requirements
✓ Your version of Yarn, 1.21.1, meets the minimum requirements.

-> PostgreSQL: Checking installation
✘ The `postgres` executable could not be found on your system.
For help setting up your Postgres environment please follow the instructions for you platform at:

https://www.postgresql.org/download/

-> MySQL: Checking installation
✓ The `mysql` executable was found on your system at: /usr/bin/mysql

-> MySQL: Checking minimum version requirements
✓ Your version of MySQL, 14.14, meets the minimum requirements.

-> SQLite3: Checking installation
✓ The `sqlite3` executable was found on your system at: /usr/bin/sqlite3

-> SQLite3: Checking minimum version requirements
✓ Your version of SQLite3, 3.22.0, meets the minimum requirements.

-> Cockroach: Checking installation
✘ The `cockroach` executable could not be found on your system.
For help setting up your Cockroach environment please follow the instructions for you platform at:

https://www.cockroachlabs.com/docs/stable/

-> Buffalo (CLI): Checking installation
✓ The `buffalo` executable was found on your system at: /usr/local/bin/buffalo

-> Buffalo (CLI): Checking minimum version requirements
✓ Your version of Buffalo (CLI), v0.15.3, meets the minimum requirements.

-> Buffalo: Application Details
Pwd         ~/work/coke
Root        ~/work/coke
GoPath      ~/work
PackagePkg  ../coke
ActionsPkg  ../coke/actions
ModelsPkg   ../coke/models
GriftsPkg   ../coke/grifts
WithModules false
Name        coke
Bin         bin/coke
VCS         git
WithPop     true
WithSQLite  false
WithDep     false
WithWebpack true
WithNodeJs  true
WithYarn    true
WithDocker  true
WithGrifts  true
AsWeb       true
AsAPI       false
InApp       true
PackageJSON {map[build:webpack -p --progress dev:webpack --watch]}

-> Buffalo: config/buffalo-app.toml
name = "coke"
bin = "bin/coke"
vcs = "git"
with_pop = true
with_sqlite = false
with_dep = false
with_webpack = true
with_nodejs = true
with_yarn = true
with_docker = true
with_grifts = true
as_web = true
as_api = false

-> Buffalo: config/buffalo-plugins.toml
[[plugin]]
  binary = "buffalo-heroku"
  go_get = "github.com/gobuffalo/buffalo-heroku"

[[plugin]]
  binary = "buffalo-pop"
  go_get = "github.com/gobuffalo/buffalo-pop"

-> Buffalo: go.mod
module coke

go 1.13

require (
    github.com/dgrijalva/jwt-go v3.2.0+incompatible
    github.com/gobuffalo/buffalo v0.15.3
    github.com/gobuffalo/buffalo-pop v1.23.1
    github.com/gobuffalo/envy v1.8.1
    github.com/gobuffalo/genny v0.6.0 // indirect
    github.com/gobuffalo/mw-csrf v0.0.0-20190129204204-25460a055517
    github.com/gobuffalo/mw-forcessl v0.0.0-20180802152810-73921ae7a130
    github.com/gobuffalo/mw-i18n v0.0.0-20190129204410-552713a3ebb4
    github.com/gobuffalo/mw-paramlogger v0.0.0-20190129202837-395da1998525
    github.com/gobuffalo/packr v1.30.1 // indirect
    github.com/gobuffalo/packr/v2 v2.7.1
    github.com/gobuffalo/pop v4.13.0+incompatible
    github.com/gobuffalo/suite v2.8.2+incompatible
    github.com/gobuffalo/validate v2.0.3+incompatible
    github.com/karrick/godirwalk v1.13.4 // indirect
    github.com/markbates/grift v1.5.0
    github.com/pkg/errors v0.8.1
    github.com/rogpeppe/go-internal v1.5.1 // indirect
    github.com/unrolled/secure v0.0.0-20190103195806-76e6d4e9b90c
)
markbates commented 4 years ago

The path helper functions, don't include any host information, by design. They are absolute paths. If you want full URLs, I would recommend writing a helper function.

sureshprasanna70 commented 4 years ago

Is there any other workaround to handle the case where the reverse proxy isn't a domain alone(example.com) but a domain with a path(example.com/admin)?

markbates commented 4 years ago

https://godoc.org/github.com/gobuffalo/buffalo#App

You want to set the prefix option.

sureshprasanna70 commented 4 years ago

Thanks but I already tried that. Setting the prefix option, renames every path. Say I have a loginPath and use a prefix called app, the path becomes loginAppPath or appLoginPath. Again this breaks the application when moving between environments.