diff --git a/README.md b/README.md index c84c6bf..f8c407c 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ The use of environment variables allow you to provide over-rides to default sett | `APPRISE_STATEFUL_MODE` | This can be set to the following possible modes:
📌 **hash**: This is also the default. It stores the server configuration in a hash formatted that can be easily indexed and compressed.
📌 **simple**: Configuration is written straight to disk using the `{KEY}.cfg` (if `TEXT` based) and `{KEY}.yml` (if `YAML` based).
📌 **disabled**: Straight up deny any read/write queries to the servers stateful store. Effectively turn off the Apprise Stateful feature completely. | `SECRET_KEY` | A Django variable acting as a *salt* for most things that require security. This API uses it for the hash sequences when writing the configuration files to disk (`hash` mode only). | `ALLOWED_HOSTS` | A list of strings representing the host/domain names that this API can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations. By default this is set to `*` allowing any host. Use space to delimit more than one host. +| `BASE_URL` | Those who are hosting the API behind a proxy that requires a subpath to gain access to this API should specify this path here as well. By default this is not set at all. | `DEBUG` | This defaults to `False` however can be set to `True` if defined with a non-zero value (such as `1`). diff --git a/apprise_api/api/context_processors.py b/apprise_api/api/context_processors.py new file mode 100644 index 0000000..00fae63 --- /dev/null +++ b/apprise_api/api/context_processors.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2020 Chris Caron +# All rights reserved. +# +# This code is licensed under the MIT License. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files(the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions : +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +from .utils import ConfigCache + + +def stateful_mode(request): + """ + Returns our loaded Stateful Mode + """ + return {'STATEFUL_MODE': ConfigCache.mode} diff --git a/apprise_api/api/templates/base.html b/apprise_api/api/templates/base.html index 7a9fa6c..9038f4b 100644 --- a/apprise_api/api/templates/base.html +++ b/apprise_api/api/templates/base.html @@ -40,10 +40,12 @@
+{% else %} +
+

{% trans "Persistent Store Endpoints" %}

+

{% blocktrans %}The administrator of this system has disabled persistent storage.{% endblocktrans %}

+
+{% endif %} {% endblock %} {% block jsfooter %} diff --git a/apprise_api/api/templates/welcome.html b/apprise_api/api/templates/welcome.html index b59a98e..d0d8e24 100644 --- a/apprise_api/api/templates/welcome.html +++ b/apprise_api/api/templates/welcome.html @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load i18n %} + {% block body %}

{% trans "The Apprise API" %}

@@ -31,7 +32,7 @@ - /notify/ + {{BASE_URL}}/notify/ {% blocktrans %}Used to notify one one or more specified Apprise URLs. See the Apprise Wiki if you need help @@ -88,7 +89,7 @@ # {% blocktrans %}Notifies an email address{% endblocktrans %}
curl -X POST -d '{"urls":"mailto://user:pass@gmail.com","body":"test body","title":"test title"}' \
    -H "Content-Type: application/json" \
-     http://localhost:8000/notify/ +     {{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/notify/

  • @@ -105,7 +106,7 @@ }

    # The URL
    req = Request(
    -     "http://localhost:8000/notify/",
    +     "{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/notify/",
        json.dumps(payload).encode('utf-8'),
        {"Content-Type": "application/json"},
        method='POST',
    @@ -119,7 +120,7 @@
    <?php

    // The URL
    - $url = 'http://localhost:8000/notify/';
    + $url = '{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/notify/';

    //Initiate cURL.
    $ch = curl_init($url);
    @@ -155,6 +156,7 @@ +{% if STATEFUL_MODE != 'disabled' %}

    {% trans "Persistent Store Endpoints" %}

    {% blocktrans %}Those wishing to use the persistent store may do so. This section is a set it and forget it type @@ -176,7 +178,7 @@ - /add/{% trans "KEY" %} + {{BASE_URL}}/add/{% trans "KEY" %} {% blocktrans %}Used to add a new Apprise configuration or a set of URLs and associates them with the specified KEY. See the KEY of abc123{% endblocktrans %}
    curl -X POST -d '{"urls":"mailto://user:pass@gmail.com"}' \
        -H "Content-Type: application/json" \
    -     http://localhost:8000/add/abc123

    +     {{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/add/abc123
    
     						        # {% blocktrans %}Load a simple TEXT config entry KEY abc123{% endblocktrans %}
    curl -X POST -d '{"format":"text","config":"devops=mailto://user:pass@gmail.com"}' \
        -H "Content-Type: application/json" \
    -     http://localhost:8000/add/abc123/ +     {{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/add/abc123/
  • @@ -254,7 +256,7 @@ }

    # The URL if the key was abc123
    req = Request(
    -     "http://localhost:8000/add/abc123",
    +     "{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/add/abc123",
        json.dumps(payload).encode('utf-8'),
        {"Content-Type": "application/json"},
        method='POST',
    @@ -268,7 +270,7 @@
    <?php

    // The URL if the key was abc123
    - $url = 'http://localhost:8000/add/abc123';
    + $url = '{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/add/abc123';

    //Initiate cURL.
    $ch = curl_init($url);
    @@ -300,7 +302,7 @@ - /del/{% trans "KEY" %} + {{BASE_URL}}/del/{% trans "KEY" %} {% blocktrans %}There are no arguments required. If the KEY exists and has data associated with it, it will be removed.{% endblocktrans %} @@ -310,7 +312,7 @@
    
     								    # {% blocktrans %}Remove previously loaded configuration associated with the KEY of abc123{% endblocktrans %}
    - curl -X POST http://localhost:8000/del/abc123
    + curl -X POST {{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/del/abc123
  • @@ -321,7 +323,7 @@ from urllib.request import Request

    # The request if the key was abc123
    req = Request(
    -     "http://localhost:8000/del/abc123",
    +     "{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/del/abc123",
        json.dumps(payload).encode('utf-8'),
        {"Content-Type": "application/json"},
        method='POST',
    @@ -335,7 +337,7 @@
    <?php

    // The URL if the key was abc123
    - $url = 'http://localhost:8000/del/abc123';
    + $url = '{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/del/abc123';

    //Initiate cURL.
    $ch = curl_init($url);
    @@ -352,16 +354,16 @@ - /get/{% trans "KEY" %} + {{BASE_URL}}/get/{% trans "KEY" %} {% blocktrans %}This feature can be used by Apprise itself. It provides a means of remotely fetching it's configuration.{% endblocktrans %}
    # Use Apprise to retrieve your configuration:
    - apprise --body="test message" --config={{ request.scheme }}://{{request.META.HTTP_HOST}}{{ request.path }}{% trans "KEY" %}
    + apprise --body="test message" --config={{ request.scheme }}://{{request.META.HTTP_HOST}}{{BASE_URL}}/get/{% trans "KEY" %}

    - /notify/{% trans "KEY" %} + {{BASE_URL}}/notify/{% trans "KEY" %} {% blocktrans %}Notifies the URLs associated with the specified KEY.{% endblocktrans %}
    @@ -415,7 +417,7 @@ # {% blocktrans %}Notifies all URLs assigned the devops tag{% endblocktrans %}
    curl -X POST -d '{"tag":"devops","body":"test body","title":"test title"}' \
        -H "Content-Type: application/json" \
    -     http://localhost:8000/notify/KEY +     {{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/notify/KEY
  • @@ -432,7 +434,7 @@ }

    # The URL if the key was abc123
    req = Request(
    -     "http://localhost:8000/notify/abc123",
    +     "{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/notify/abc123",
        json.dumps(payload).encode('utf-8'),
        {"Content-Type": "application/json"},
        method='POST',
    @@ -446,7 +448,7 @@
    <?php

    // The URL if the key was abc123
    - $url = 'http://localhost:8000/notify/abc123';
    + $url = '{{request.scheme}}://{{request.META.HTTP_HOST}}{{BASE_URL}}/notify/abc123';

    //Initiate cURL.
    $ch = curl_init($url);
    @@ -495,4 +497,10 @@

    - {% endblock %} +{% else %} +
    +

    {% trans "Persistent Store Endpoints" %}

    +

    {% blocktrans %}The administrator of this system has disabled persistent storage.{% endblocktrans %}

    +
    +{% endif %} +{% endblock %} diff --git a/apprise_api/core/context_processors.py b/apprise_api/core/context_processors.py new file mode 100644 index 0000000..4a733ab --- /dev/null +++ b/apprise_api/core/context_processors.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2020 Chris Caron +# All rights reserved. +# +# This code is licensed under the MIT License. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files(the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions : +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +from django.conf import settings + + +def base_url(request): + """ + Returns our defined BASE_URL object + """ + return {'BASE_URL': settings.BASE_URL} diff --git a/apprise_api/core/settings/__init__.py b/apprise_api/core/settings/__init__.py index b0e7973..3394ac3 100644 --- a/apprise_api/core/settings/__init__.py +++ b/apprise_api/core/settings/__init__.py @@ -70,6 +70,8 @@ TEMPLATES = [ 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.request', + 'core.context_processors.base_url', + 'api.context_processors.stateful_mode', ], }, }, @@ -77,8 +79,12 @@ TEMPLATES = [ WSGI_APPLICATION = 'core.wsgi.application' +# Define our base URL +# The default value is to be a single slash +BASE_URL = os.environ.get('BASE_URL', '') + # Static files relative path (CSS, JavaScript, Images) -STATIC_URL = '/s/' +STATIC_URL = BASE_URL + '/s/' # The location to store Apprise configuration files APPRISE_CONFIG_DIR = os.environ.get(