Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #161 from vancluever/path-proxy-support
Browse files Browse the repository at this point in the history
Reverse proxy support
  • Loading branch information
jippi authored Dec 18, 2016
2 parents aab211c + 2d786a2 commit 8bd19c0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
frontend/node_modules
backend/vendor
backend/backend

.tern-port
.sass-cache
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ both hostname and port.

hashi-ui can be controlled by both ENV or CLI flags as described below

| Environment | CLI (`--flag`) | Default | Description |
|------------------- |----------------------- |------------------------- |------------------------------------------------------------------------------------------------------ |
| `NOMAD_ADDR` | `nomad.address` | `http://127.0.0.1:4646` | Must point to the correct location of your Nomad server. |
| `NOMAD_READ_ONLY` | `nomad.read-only` | `false` | Should hash-ui allowed to modify nomad state (stop/start jobs and so forth) |
| `NOMAD_PORT_http` | `web.listen-address` | `0.0.0.0:3000` | The IP + PORT to listen on |
| `NOMAD_LOG_LEVEL` | `log.level` | `info` | Log level to use while running the hashi-ui server - (`critical`, `error`, `warning`, `notice`, `info`, `debug`) |
| `NEWRELIC_APP_NAME` | `newrelic.app_name` | `hashi-ui` | (optional) NewRelic application name |
| `NEWRELIC_LICENSE` | `newrelic.license` | '' | (optional) NewRelic license key |
| Environment | CLI (`--flag`) | Default | Description |
|-----------------------|-------------------------|---------------------------|--------------------------------------------------------------------------------------------------------|
| `NOMAD_ADDR` | `nomad.address` | `http://127.0.0.1:4646` | Must point to the correct location of your Nomad server. |
| `NOMAD_READ_ONLY` | `nomad.read-only` | `false` | Should hash-ui allowed to modify nomad state (stop/start jobs and so forth) |
| `NOMAD_PORT_http` | `web.listen-address` | `0.0.0.0:3000` | The IP + PORT to listen on |
| `NOMAD_PROXY_ADDRESS` | `web.proxy-address` | `<empty>` | (optional) The base URL of the UI when running behind a reverse proxy (ie: example.com/nomad/) |
| `NOMAD_LOG_LEVEL` | `log.level` | `info` | Log level to use while running the hashi-ui server - (`critical`, `error`, `warning`, `notice`, `info`, `debug`) |
| `NEWRELIC_APP_NAME` | `newrelic.app_name` | `hashi-ui` | (optional) NewRelic application name |
| `NEWRELIC_LICENSE` | `newrelic.license` | '' | (optional) NewRelic license key |

# Try

Expand Down
25 changes: 23 additions & 2 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Config struct {
ReadOnly bool
Address string
ListenAddress string
ProxyAddress string
LogLevel string
NewRelicAppName string
NewRelicLicense string
Expand Down Expand Up @@ -85,6 +86,9 @@ var (
flagListenAddress = flag.String("web.listen-address", "",
"The address on which to expose the web interface. "+flagDefault(defaultConfig.ListenAddress))

flagProxyAddress = flag.String("web.proxy-address", "",
"The address used on an external proxy (exmaple: example.com/nomad) "+flagDefault(defaultConfig.ProxyAddress))

flagLogLevel = flag.String("log.level", "",
"The log level for hashi-ui to run under. "+flagDefault(defaultConfig.LogLevel))

Expand Down Expand Up @@ -115,6 +119,11 @@ func (c *Config) Parse() {
c.ListenAddress = fmt.Sprintf("0.0.0.0:%s", listenPort)
}

proxyAddress, ok := syscall.Getenv("NOMAD_PROXY_ADDRESS")
if ok {
c.ProxyAddress = proxyAddress
}

logLevel, ok := syscall.Getenv("NOMAD_LOG_LEVEL")
if ok {
c.LogLevel = logLevel
Expand Down Expand Up @@ -144,6 +153,10 @@ func (c *Config) Parse() {
c.ListenAddress = *flagListenAddress
}

if *flagProxyAddress != "" {
c.ProxyAddress = *flagProxyAddress
}

if *flagLogLevel != "" {
c.LogLevel = *flagLogLevel
}
Expand Down Expand Up @@ -188,6 +201,7 @@ func main() {

logger.Infof("| nomad.address : %-50s |", cfg.Address)
logger.Infof("| web.listen-address : http://%-43s |", cfg.ListenAddress)
logger.Infof("| web.proxy-address : %-50s |", cfg.ProxyAddress)
logger.Infof("| log.level : %-50s |", cfg.LogLevel)

if cfg.NewRelicAppName != "" && cfg.NewRelicLicense != "" {
Expand Down Expand Up @@ -231,17 +245,24 @@ func main() {
router := mux.NewRouter()
router.HandleFunc(newrelic.WrapHandleFunc(app, "/ws", hub.Handler))
router.HandleFunc(newrelic.WrapHandleFunc(app, "/download/{path:.*}", nomad.downloadFile))
router.PathPrefix("/static").Handler(http.FileServer(myAssetFS))
router.HandleFunc(newrelic.WrapHandleFunc(app, "/config.js", func(w http.ResponseWriter, r *http.Request) {
response := make([]string, 0)
response = append(response, fmt.Sprintf("window.NOMAD_READ_ONLY=%s", strconv.FormatBool(cfg.ReadOnly)))
response = append(response, fmt.Sprintf("window.NOMAD_ADDR=\"%s\"", cfg.Address))
response = append(response, fmt.Sprintf("window.NOMAD_LOG_LEVEL=\"%s\"", cfg.LogLevel))

var endpointURL string
if cfg.ProxyAddress != "" {
endpointURL = cfg.ProxyAddress
} else {
endpointURL = cfg.ListenAddress
}
response = append(response, fmt.Sprintf("window.NOMAD_ENDPOINT=\"%s\"", strings.TrimSuffix(endpointURL, "/")))

w.Header().Set("Content-Type", "application/javascript")
w.Write([]byte(strings.Join(response, "\n")))
}))

router.PathPrefix("/static").Handler(http.FileServer(myAssetFS))
router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if bs, err := myAssetFS.Open("/index.html"); err != nil {
logger.Infof("%s", err)
Expand Down
16 changes: 5 additions & 11 deletions frontend/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,11 @@
</script>
<% } %>

<script type="text/javascript">
<% if (htmlWebpackPlugin.options.production) { %>
window['NOMAD_ENDPOINT'] = window.location.host;
<% } else { %>
window['NOMAD_ENDPOINT'] = window.location.hostname + ':' + (NOMAD_ENDPOINT_PORT || 3000);
<% } %>
var configScript = document.createElement('script');
configScript.setAttribute('src', '//' + NOMAD_ENDPOINT + '/config.js');
document.head.appendChild(configScript);
</script>
<% if (htmlWebpackPlugin.options.window) { %>
<script src="http://<%= htmlWebpackPlugin.options.window['NOMAD_ENDPOINT']%>:<%= htmlWebpackPlugin.options.window['NOMAD_ENDPOINT_PORT']%>/config.js"></script>
<% } else { %>
<script type="text/javascript" src="config.js"></script>
<% } %>
</head>
<body>
<div id="app"></div>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { browserHistory } from 'react-router'
import { createHistory, useBasename } from 'history';
const browserHistory = useBasename(createHistory)({
basename: window['NOMAD_ENDPOINT'].replace(/^[.a-zA-Z0-9]+:?[0-9]*/, '')
});

import { Provider } from 'react-redux'
import injectTapEventPlugin from 'react-tap-event-plugin'

Expand Down
2 changes: 1 addition & 1 deletion frontend/webpack-base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const autoprefixer = require('autoprefixer');
module.exports = {
output: {
path: path.join(__dirname, 'build/'),
publicPath: '/'
publicPath: ''
},
resolve: {
extensions: ['', '.js', '.jsx', '.css', '.scss']
Expand Down
1 change: 1 addition & 0 deletions frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ webpackConfig = merge(webpackConfig, {
favicon: './assets/img/favicon.png',
appMountId: 'app',
window: {
NOMAD_ENDPOINT: process.env.GO_HOST || '127.0.0.1',
NOMAD_ENDPOINT_PORT: process.env.GO_PORT || 3000
}
}),
Expand Down

0 comments on commit 8bd19c0

Please sign in to comment.