jumpserver/apps/jumpserver/urls.py

104 lines
4.0 KiB
Python
Raw Normal View History

# ~*~ coding: utf-8 ~*~
from __future__ import unicode_literals
2018-07-12 16:31:50 +00:00
import re
2018-08-02 04:36:31 +00:00
import os
2018-07-27 10:56:40 +00:00
from django.urls import path, include, re_path
2016-08-17 14:17:16 +00:00
from django.conf import settings
from django.conf.urls.static import static
2018-07-12 16:31:50 +00:00
from rest_framework.response import Response
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.utils.encoding import iri_to_uri
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
2016-08-23 15:29:33 +00:00
2018-03-04 05:01:33 +00:00
from .views import IndexView, LunaView
2016-08-23 15:29:33 +00:00
schema_view = get_schema_view(
openapi.Info(
2018-07-27 10:56:40 +00:00
title="Jumpserver API Docs",
default_version='v1',
2018-07-27 10:56:40 +00:00
description="Jumpserver Restful api docs",
terms_of_service="https://www.jumpserver.org",
contact=openapi.Contact(email="support@fit2cloud.com"),
license=openapi.License(name="GPLv2 License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
2018-07-27 08:21:55 +00:00
api_url_pattern = re.compile(r'^/api/(?P<version>\w+)/(?P<app>\w+)/(?P<extra>.*)$')
2018-07-12 16:31:50 +00:00
class HttpResponseTemporaryRedirect(HttpResponse):
status_code = 307
def __init__(self, redirect_to):
HttpResponse.__init__(self)
self['Location'] = iri_to_uri(redirect_to)
@csrf_exempt
2018-07-27 08:21:55 +00:00
def redirect_format_api(request, *args, **kwargs):
2018-07-27 10:56:40 +00:00
_path, query = request.path, request.GET.urlencode()
matched = api_url_pattern.match(_path)
2018-07-12 16:31:50 +00:00
if matched:
2018-07-27 08:21:55 +00:00
version, app, extra = matched.groups()
2018-07-27 10:56:40 +00:00
_path = '/api/{app}/{version}/{extra}?{query}'.format(**{
2018-07-12 16:31:50 +00:00
"app": app, "version": version, "extra": extra,
"query": query
})
2018-07-27 10:56:40 +00:00
return HttpResponseTemporaryRedirect(_path)
2018-07-12 16:31:50 +00:00
else:
2018-07-27 10:56:40 +00:00
return Response({"msg": "Redirect url failed: {}".format(_path)}, status=404)
2018-07-12 16:31:50 +00:00
v1_api_patterns = [
2018-07-27 10:56:40 +00:00
path('users/v1/', include('users.urls.api_urls', namespace='api-users')),
path('assets/v1/', include('assets.urls.api_urls', namespace='api-assets')),
path('perms/v1/', include('perms.urls.api_urls', namespace='api-perms')),
path('terminal/v1/', include('terminal.urls.api_urls', namespace='api-terminal')),
path('ops/v1/', include('ops.urls.api_urls', namespace='api-ops')),
path('audits/v1/', include('audits.urls.api_urls', namespace='api-audits')),
path('orgs/v1/', include('orgs.urls.api_urls', namespace='api-orgs')),
path('common/v1/', include('common.urls.api_urls', namespace='api-common')),
2018-07-12 16:31:50 +00:00
]
app_view_patterns = [
2018-07-27 10:56:40 +00:00
path('users/', include('users.urls.views_urls', namespace='users')),
path('assets/', include('assets.urls.views_urls', namespace='assets')),
path('perms/', include('perms.urls.views_urls', namespace='perms')),
path('terminal/', include('terminal.urls.views_urls', namespace='terminal')),
path('ops/', include('ops.urls.view_urls', namespace='ops')),
path('audits/', include('audits.urls.view_urls', namespace='audits')),
path('orgs/', include('orgs.urls.views_urls', namespace='orgs')),
2018-07-12 16:31:50 +00:00
]
2018-08-02 04:36:31 +00:00
XPACK_DIR = os.path.join(settings.BASE_DIR, 'xpack')
if os.path.isdir(XPACK_DIR):
app_view_patterns.append(path('xpack/', include('xpack.urls', namespace='xpack')))
2018-07-12 16:31:50 +00:00
urlpatterns = [
2018-07-27 10:56:40 +00:00
path('', IndexView.as_view(), name='index'),
path('luna/', LunaView.as_view(), name='luna-error'),
path('settings/', include('common.urls.view_urls', namespace='settings')),
path('common/', include('common.urls.view_urls', namespace='common')),
path('api/v1/', redirect_format_api),
path('api/', include(v1_api_patterns)),
2017-12-18 10:38:30 +00:00
# External apps url
2018-07-27 10:56:40 +00:00
path('captcha/', include('captcha.urls')),
2016-08-08 16:43:11 +00:00
]
2018-07-12 16:31:50 +00:00
urlpatterns += app_view_patterns
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
2018-04-12 09:31:37 +00:00
2016-08-17 14:17:16 +00:00
if settings.DEBUG:
2017-07-10 02:26:17 +00:00
urlpatterns += [
2018-07-27 10:56:40 +00:00
re_path('swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=None), name='schema-json'),
path('docs/', schema_view.with_ui('swagger', cache_timeout=None), name="docs"),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=None), name='redoc'),
2018-04-12 09:31:37 +00:00
]