Browse Source

#212: Introduced BASE_PATH configuration setting

Jeremy Stretch 8 years ago
parent
commit
833499ffe8

+ 12 - 0
docs/configuration/optional-settings.md

@@ -26,6 +26,18 @@ BANNER_BOTTOM = BANNER_TOP
 
 ---
 
+## BASE_PATH
+
+Default: None
+
+The base URL path to use when accessing NetBox. Do not include the scheme or domain name. For example, if installed at http://example.com/netbox/, set:
+
+```
+BASE_PATH = 'netbox/'
+```
+
+---
+
 ## DEBUG
 
 Default: False

+ 4 - 0
netbox/netbox/configuration.docker.py

@@ -52,6 +52,10 @@ EMAIL = {
 # are permitted to access most data in NetBox (excluding secrets) but not make any changes.
 LOGIN_REQUIRED = os.environ.get('LOGIN_REQUIRED', False)
 
+# Base URL path if accessing NetBox within a directory. For example, if installed at http://example.com/netbox/, set:
+# BASE_PATH = 'netbox/'
+BASE_PATH = os.environ.get('BASE_PATH', '')
+
 # Setting this to True will display a "maintenance mode" banner at the top of every page.
 MAINTENANCE_MODE = os.environ.get('MAINTENANCE_MODE', False)
 

+ 4 - 0
netbox/netbox/configuration.example.py

@@ -52,6 +52,10 @@ EMAIL = {
 # are permitted to access most data in NetBox (excluding secrets) but not make any changes.
 LOGIN_REQUIRED = False
 
+# Base URL path if accessing NetBox within a directory. For example, if installed at http://example.com/netbox/, set:
+# BASE_PATH = 'netbox/'
+BASE_PATH = ''
+
 # Setting this to True will display a "maintenance mode" banner at the top of every page.
 MAINTENANCE_MODE = False
 

+ 3 - 0
netbox/netbox/settings.py

@@ -27,6 +27,9 @@ ADMINS = getattr(configuration, 'ADMINS', [])
 DEBUG = getattr(configuration, 'DEBUG', False)
 EMAIL = getattr(configuration, 'EMAIL', {})
 LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)
+BASE_PATH = getattr(configuration, 'BASE_PATH', '')
+if BASE_PATH:
+    BASE_PATH = BASE_PATH.strip('/') + '/'  # Enforce trailing slash only
 MAINTENANCE_MODE = getattr(configuration, 'MAINTENANCE_MODE', False)
 PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
 NETBOX_USERNAME = getattr(configuration, 'NETBOX_USERNAME', '')

+ 35 - 30
netbox/netbox/urls.py

@@ -1,3 +1,4 @@
+from django.conf import settings
 from django.conf.urls import include, url
 from django.contrib import admin
 from django.views.defaults import page_not_found
@@ -10,35 +11,39 @@ handler500 = handle_500
 
 urlpatterns = [
 
-    # Default page
-    url(r'^$', home, name='home'),
-
-    # Login/logout
-    url(r'^login/$', login, name='login'),
-    url(r'^logout/$', logout, name='logout'),
-
-    # Apps
-    url(r'^circuits/', include('circuits.urls', namespace='circuits')),
-    url(r'^dcim/', include('dcim.urls', namespace='dcim')),
-    url(r'^ipam/', include('ipam.urls', namespace='ipam')),
-    url(r'^secrets/', include('secrets.urls', namespace='secrets')),
-    url(r'^tenancy/', include('tenancy.urls', namespace='tenancy')),
-    url(r'^profile/', include('users.urls', namespace='users')),
-
-    # API
-    url(r'^api/circuits/', include('circuits.api.urls', namespace='circuits-api')),
-    url(r'^api/dcim/', include('dcim.api.urls', namespace='dcim-api')),
-    url(r'^api/ipam/', include('ipam.api.urls', namespace='ipam-api')),
-    url(r'^api/secrets/', include('secrets.api.urls', namespace='secrets-api')),
-    url(r'^api/tenancy/', include('tenancy.api.urls', namespace='tenancy-api')),
-    url(r'^api/docs/', include('rest_framework_swagger.urls')),
-    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
-
-    # Error testing
-    url(r'^404/$', page_not_found),
-    url(r'^500/$', trigger_500),
-
-    # Admin
-    url(r'^admin/', include(admin.site.urls)),
+    url(r'^{}'.format(settings.BASE_PATH), include([
+
+        # Default page
+        url(r'^$', home, name='home'),
+
+        # Login/logout
+        url(r'^login/$', login, name='login'),
+        url(r'^logout/$', logout, name='logout'),
+
+        # Apps
+        url(r'^circuits/', include('circuits.urls', namespace='circuits')),
+        url(r'^dcim/', include('dcim.urls', namespace='dcim')),
+        url(r'^ipam/', include('ipam.urls', namespace='ipam')),
+        url(r'^secrets/', include('secrets.urls', namespace='secrets')),
+        url(r'^tenancy/', include('tenancy.urls', namespace='tenancy')),
+        url(r'^profile/', include('users.urls', namespace='users')),
+
+        # API
+        url(r'^api/circuits/', include('circuits.api.urls', namespace='circuits-api')),
+        url(r'^api/dcim/', include('dcim.api.urls', namespace='dcim-api')),
+        url(r'^api/ipam/', include('ipam.api.urls', namespace='ipam-api')),
+        url(r'^api/secrets/', include('secrets.api.urls', namespace='secrets-api')),
+        url(r'^api/tenancy/', include('tenancy.api.urls', namespace='tenancy-api')),
+        url(r'^api/docs/', include('rest_framework_swagger.urls')),
+        url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
+
+        # Error testing
+        url(r'^404/$', page_not_found),
+        url(r'^500/$', trigger_500),
+
+        # Admin
+        url(r'^admin/', include(admin.site.urls)),
+
+    ]))
 
 ]

+ 1 - 1
netbox/templates/500.html

@@ -26,7 +26,7 @@
                     <pre><strong>{{ exception }}</strong><br />
 {{ error }}</pre>
                     <div class="text-right">
-                        <a href="/" class="btn btn-primary">Home Page</a>
+                        <a href="{% url 'home' %}" class="btn btn-primary">Home Page</a>
                     </div>
                 </div>
             </div>

+ 2 - 2
netbox/templates/_base.html

@@ -21,7 +21,7 @@
                     <span class="icon-bar"></span>
                     <span class="icon-bar"></span>
                 </button>
-                <a class="navbar-brand" href="/">
+                <a class="navbar-brand" href="{% url 'home' %}">
                     <img src="{% static 'img/netbox_logo.png' %}" />
                 </a>
             </div>
@@ -289,7 +289,7 @@
                 <div class="col-xs-4 text-right">
                     <p class="text-muted">
                         <i class="fa fa-fw fa-book text-primary"></i> <a href="http://netbox.readthedocs.io/" target="_blank">Docs</a> &middot;
-                        <i class="fa fa-fw fa-cloud text-primary"></i> <a href="/api/docs/">API</a> &middot;
+                        <i class="fa fa-fw fa-cloud text-primary"></i> <a href="{% url 'django.swagger.base.view' %}">API</a> &middot;
                         <i class="fa fa-fw fa-code text-primary"></i> <a href="https://github.com/digitalocean/netbox">Code</a>
                     </p>
                 </div>

+ 3 - 3
netbox/templates/dcim/device.html

@@ -545,13 +545,13 @@ function toggleConnection(elem, api_url) {
     return false;
 }
 $(".consoleport-toggle").click(function() {
-    return toggleConnection($(this), "/api/dcim/console-ports/");
+    return toggleConnection($(this), "/{{ settings.BASE_PATH }}api/dcim/console-ports/");
 });
 $(".powerport-toggle").click(function() {
-    return toggleConnection($(this), "/api/dcim/power-ports/");
+    return toggleConnection($(this), "/{{ settings.BASE_PATH }}api/dcim/power-ports/");
 });
 $(".interface-toggle").click(function() {
-    return toggleConnection($(this), "/api/dcim/interface-connections/");
+    return toggleConnection($(this), "/{{ settings.BASE_PATH }}api/dcim/interface-connections/");
 });
 </script>
 <script src="{% static 'js/graphs.js' %}"></script>