2018-12-10 15:05:02 +00:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
from os import getenv
|
|
|
|
from platform import node
|
|
|
|
from uuid import getnode as get_mac
|
|
|
|
|
|
|
|
from logdna import LogDNAHandler
|
|
|
|
|
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.request import Request
|
2021-11-23 21:00:25 +00:00
|
|
|
from sanic.response import json
|
|
|
|
|
|
|
|
log = logging.getLogger("logdna")
|
2018-12-10 15:05:02 +00:00
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
def get_my_ip_address(remote_server="google.com"):
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
|
|
|
s.connect((remote_server, 80))
|
|
|
|
return s.getsockname()[0]
|
|
|
|
|
|
|
|
|
|
|
|
def get_mac_address():
|
|
|
|
h = iter(hex(get_mac())[2:].zfill(12))
|
|
|
|
return ":".join(i + next(h) for i in h)
|
|
|
|
|
|
|
|
|
|
|
|
logdna_options = {
|
|
|
|
"app": __name__,
|
|
|
|
"index_meta": True,
|
|
|
|
"hostname": node(),
|
|
|
|
"ip": get_my_ip_address(),
|
2021-11-23 21:00:25 +00:00
|
|
|
"mac": get_mac_address(),
|
2018-12-10 15:05:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 04:13:13 +01:00
|
|
|
logdna_handler = LogDNAHandler(getenv("LOGDNA_API_KEY"), options=logdna_options)
|
2018-12-10 15:05:02 +00:00
|
|
|
|
|
|
|
logdna = logging.getLogger(__name__)
|
|
|
|
logdna.setLevel(logging.INFO)
|
|
|
|
logdna.addHandler(logdna_handler)
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
app = Sanic("Example")
|
2018-12-10 15:05:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.middleware
|
|
|
|
def log_request(request: Request):
|
2023-10-25 02:28:52 +01:00
|
|
|
logdna.info(f"I was Here with a new Request to URL: {request.url}")
|
2018-12-10 15:05:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def default(request):
|
2021-11-23 21:00:25 +00:00
|
|
|
return json({"response": "I was here"})
|
2018-12-10 15:05:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-11-23 21:00:25 +00:00
|
|
|
app.run(host="0.0.0.0", port=getenv("PORT", 8080))
|