Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
simsong committed Jan 15, 2025
2 parents c28a99c + 69a380e commit 1068883
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ Keep your Lambda functions optimized for performance and cost.

# Lambda and S3
Lamda limits returns to 6MB and uploads to around 256K. So large uploads are done with presigned POST to S3 and large downloads by putting the data into S3 and having it pulled with a presigned URL.


colima start
docker ps -a
4 changes: 4 additions & 0 deletions deploy/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
stuff
"""
__version__='0.9.0'
13 changes: 12 additions & 1 deletion deploy/app/apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import functools
import subprocess
import json
from functools import lru_cache
import base64
from os.path import join

from flask import request

from . import db
from .paths import ETC_DIR
from .auth import get_dbreader,AuthError
from .constants import C,__version__

Expand Down Expand Up @@ -115,10 +119,16 @@ def get_user_dict():

userdict = db.validate_api_key(api_key)
if not userdict:
logging.info("api_key %s is invalid ipaddr=%s request.url=%s", api_key,request.remote_addr,request.url)
logging.info("api_key %s is invalid ipaddr=%s request.url=%s",
api_key,request.remote_addr,request.url)
raise AuthError(f"api_key '{api_key}' is invalid")
return userdict

@lru_cache(maxsize=1)
def favicon_base64():
with open( join( ETC_DIR, C.FAVICON), 'rb') as f:
return base64.b64encode(f.read()).decode('utf-8')

def page_dict(title='', *, require_auth=False, lookup=True, logout=False,debug=False):
"""Returns a dictionary that can be used by post of the templates.
:param: title - the title we should give the page
Expand Down Expand Up @@ -169,6 +179,7 @@ def page_dict(title='', *, require_auth=False, lookup=True, logout=False,debug=F
ret= fix_types({
C.API_BASE: api_base,
C.STATIC_BASE: static_base,
'favicon_base64':favicon_base64(),
'api_key': api_key, # the API key that is currently active
'user_id': user_id, # the user_id that is active
'user_name': user_name, # the user's name
Expand Down
2 changes: 1 addition & 1 deletion deploy/app/bottle_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def lambda_startup():
clogging.setup(level=os.environ.get('PLANTTRACER_LOG_LEVEL',logging.INFO))
fix_boto_log_level()

if C.PLANTTRACER_S3_BUCKET in os.environ:
if os.environ.get(C.PLANTTRACER_S3_BUCKET,None):
db_object.S3_BUCKET = os.environ[C.PLANTTRACER_S3_BUCKET]
else:
config = auth.config()
Expand Down
1 change: 1 addition & 0 deletions deploy/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class C:
FFMPEG_PATH = 'FFMPEG_PATH'

# Other
FAVICON = 'icon.png'
API_BASE='API_BASE'
STATIC_BASE='STATIC_BASE'
TRACKING_COMPLETED='TRACKING COMPLETED' # keep case; it's used as a flag
Expand Down
1 change: 1 addition & 0 deletions deploy/app/db_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def make_urn(*, object_name, scheme = None ):
if scheme == C.SCHEME_S3 and S3_BUCKET is None:
scheme = C.SCHEME_DB
if scheme == C.SCHEME_S3:
assert len(S3_BUCKET)>0
netloc = S3_BUCKET
elif scheme == C.SCHEME_DB:
netloc = DB_TABLE
Expand Down
9 changes: 7 additions & 2 deletions deploy/app/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@

def ffmpeg_path():
if C.FFMPEG_PATH in os.environ:
return os.environ[C.FFMPEG_PATH]
pth = os.environ[C.FFMPEG_PATH]
if os.path.exists(path):
return pth
pth = shutil.which('ffmpeg')
if pth:
return pth
if os.path.exists(AWS_LAMBDA_LINUX_STATIC_FFMPEG):
return AWS_LAMBDA_LINUX_STATIC_FFMPEG
return shutil.which('ffmpeg')
raise FileNotFoundError("ffmpeg")
6 changes: 3 additions & 3 deletions deploy/app/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!DOCTYPE html><!-- -*- mode: html -*- -->
<html lang="en">
<head>
<title> {% block title %} {{title}} {% endblock %} </title>
<meta charset="UTF-8"/>
<link rel="icon" href="https://planttracer.com/favicon.png" type="image/png"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="google" content="notranslate"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<link rel="icon" href="https://planttracer.com/favicon.png" type="image/x-icon"/>
<title> {% block title %} {{title}} {% endblock %} </title>

<!-- use Pure.css - https://purecss.io/ -->
<link rel="stylesheet"
Expand Down
7 changes: 5 additions & 2 deletions deploy/demo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json
"""
Amazon-provided demo.
"""

import os
import os.path


def lambda_handler(event, context):
def lambda_handler(event, context): # pylint: disable=unused-argument
"""Sample pure Lambda function
Parameters
Expand Down
Binary file added deploy/etc/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 11 additions & 10 deletions deploy/lambda_handler.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import json
import os
import os.path
"""
Create a lambda_handler and catch any import errors
This is kind of gross, but it was the easiest way I found to persist the info in e into the function definition.
The problem is that the act of importing the bottle app causes startup code to run.
Perhaps we can refactor the bottle app so that the app is created here?
"""

import logging
import traceback

# Create a lambda_handler and catch any import errors
# This is kind of gross, but it was the easiest way I found to persist the info in e into the function definition.
# The problem is that the act of importing the bottle app causes startup code to run.
# Perhaps we can refactor the bottle app so that the app is created here?

IMPORT_ERROR_FILE = '/tmp/import-error'

try:
from apig_wsgi import make_lambda_handler
from app.bottle_app import lambda_startup,app

logging.basicConfig(
level=logging.INFO,
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s"
)
logger = logging.getLogger(__name__)
lambda_startup()
lambda_app = make_lambda_handler(app)

# pylint: disable=broad-exception-caught
except Exception as e:
with open(IMPORT_ERROR_FILE,'w') as f:
f.write(str(e))
f.write("\n")
f.write(''.join(traceback.TracebackException.from_exception(e).format()))
def lambda_handler(event, context):
def lambda_handler(event, context): # pylint: disable=unused-argument
with open(IMPORT_ERROR_FILE,'r') as f:
return {
"statusCode": 200,
Expand Down

0 comments on commit 1068883

Please sign in to comment.