-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
185 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2022 3liz | ||
# Author David Marteau | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
|
||
""" Cache Observer that send a BAN request | ||
""" | ||
import asyncio | ||
import logging | ||
|
||
from datetime import datetime | ||
from tornado.httpclient import AsyncHTTPClient, HTTPRequest | ||
|
||
from pyqgisserver.config import confservice | ||
|
||
LOGGER=logging.getLogger('SRVLOG') | ||
|
||
server_address = None | ||
http_client = None | ||
|
||
def init() -> None: | ||
""" | ||
""" | ||
LOGGER.debug("*** Initializing ban observer") | ||
confservice.add_section('cache.observers:ban') | ||
|
||
global server_address, http_client | ||
server_address = confservice.get('cache.observers:ban','server_address') | ||
http_client = AsyncHTTPClient() | ||
|
||
LOGGER.debug("*** Ban observer: sending_request to %s", server_address) | ||
|
||
async def ban( key: str) -> None: | ||
""" Ban key | ||
""" | ||
LOGGER.info("Sending BAN request to %s", server_address) | ||
|
||
request = HTTPRequest(server_address, method='BAN', | ||
headers={ 'X-Map-Id': key }, | ||
user_agent="py-qgis-server; ban observer", | ||
allow_nonstandard_methods=True) | ||
|
||
response = await http_client.fetch(request, raise_error=False) | ||
if response.code != 200: | ||
LOGGER.error("Ban server returned status code %s", response.code) | ||
|
||
|
||
|
||
def observe(key: str, datetime: datetime, insert: bool) -> None: | ||
asyncio.create_task(ban(key)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ services: | |
ports: | ||
- 127.0.0.1:5672:5672 | ||
- 127.0.0.1:15672:15672 | ||
|
||
|
||
networks: | ||
- backend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ services: | |
volumes: | ||
- ${PG_RUN}:/var/run/postgresql | ||
- ${PGPASSFILE}:/.pgpass | ||
networks: | ||
- backend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# | ||
# See https://www.varnish-software.com/developers/tutorials/running-varnish-docker/ | ||
# | ||
# References: | ||
# - https://book.varnish-software.com/4.0/chapters/VCL_Basics.html | ||
# | ||
version: '3.8' | ||
services: | ||
qgis-server: | ||
environment: | ||
QGSRV_CACHE_OBSERVERS: ban | ||
QGSRV_CACHE_OBSERVERS_BAN_SERVER_ADDRESS: "http://varnish:80" | ||
QGSRV_CACHE_CHECK_INTERVAL: 10 | ||
varnish: | ||
image: varnish:7.0.2 | ||
environment: | ||
VARNISH_SIZE: 500M | ||
volumes: | ||
- ${PWD}/varnish.vcl:/etc/varnish/default.vcl:ro | ||
- ${PWD}/varnish.secret:/etc/varnish/secret:ro | ||
command: ["-S", "/etc/varnish/secret"] | ||
tmpfs: | ||
- /var/lib/varnish:exec | ||
ports: | ||
- 127.0.0.1:8889:80 | ||
networks: | ||
- backend | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
varnishsecretwhateveritcanbe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# | ||
# This is an example VCL file for Varnish. | ||
# | ||
# It does not do anything by default, delegating control to the | ||
# builtin VCL. The builtin VCL is called when there is no explicit | ||
# return statement. | ||
# | ||
# See the VCL chapters in the Users Guide for a comprehensive documentation | ||
# at https://www.varnish-cache.org/docs/. | ||
|
||
# Marker to tell the VCL compiler that this VCL has been written with the | ||
# 4.0 or 4.1 syntax. | ||
vcl 4.1; | ||
|
||
import std; | ||
|
||
# acl for administrative requests (i.e BAN) | ||
# Set this to the configured network between admin backend | ||
# and varnish | ||
acl purge { | ||
"172.199.0.0"/16; // Our backend network | ||
} | ||
|
||
# Default backend definition. Set this to point to your content server. | ||
backend default { | ||
.host = "qgis-server"; | ||
.port = "8080"; | ||
} | ||
|
||
sub vcl_recv { | ||
# Happens before we check if we have this in cache already. | ||
# | ||
# Typically you clean up the request here, removing cookies you don't need, | ||
# rewriting the request, etc. | ||
|
||
# Handle BAN request | ||
if (req.method == "BAN") { | ||
if (!client.ip ~ purge) { | ||
return(synth(405,"Not Allowed")); | ||
} | ||
if (std.ban("obj.http.X-Map-Id ~ " + req.http.X-Map-Id)) { | ||
return(synth(200,"Ban Added")); | ||
} else { | ||
return(synth(400, std.ban_error())); | ||
} | ||
} | ||
} | ||
|
||
sub vcl_backend_response { | ||
# Happens after we have read the response headers from the backend. | ||
# | ||
# Here you clean the response headers, removing silly Set-Cookie headers | ||
# and other mistakes your backend does. | ||
|
||
# Set grace period long enough to get | ||
# the response from long loading projects | ||
set beresp.grace = 10m; | ||
return (deliver); | ||
} | ||
|
||
sub vcl_deliver { | ||
# Happens when we have all the pieces we need, and are about to send the | ||
# response to the client. | ||
# | ||
# You can do accounting or modifying the final object here. | ||
} | ||
|