Compare commits
7 Commits
f7ca80f146
...
051fc1ffdd
| Author | SHA1 | Date | |
|---|---|---|---|
| 051fc1ffdd | |||
| 695c4d7b84 | |||
| 0263fffb3c | |||
| 18ec901518 | |||
| d54a2be28f | |||
| bd9ac2f3bf | |||
| 4c33dff400 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
docker/.env
|
||||
scenesense-docker/.env
|
||||
scenesense-docker/db/
|
||||
.dockerignore
|
||||
@@ -1,15 +1,17 @@
|
||||
# Use the official Python image with Python 3.10
|
||||
FROM python:3.10
|
||||
FROM python:3.10-slim-bookworm
|
||||
|
||||
# Set the working directory to /app
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the server files and requirements.txt into the container at /app
|
||||
COPY ../server/ ../requirements.txt /app/
|
||||
# Copy requirements.txt into the container at /app
|
||||
COPY requirements.txt /app/
|
||||
|
||||
# Install any needed packages specified in requirements.txt
|
||||
RUN pip install --trusted-host pypi.python.org -r requirements.txt
|
||||
|
||||
# Copy server file
|
||||
COPY server/server_api.py /app/
|
||||
# Expose port 5000 for the Flask app to listen on
|
||||
EXPOSE 5000
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
version: '3'
|
||||
services:
|
||||
db:
|
||||
image: mariadb:latest
|
||||
container_name: mariadb-container
|
||||
env_file:
|
||||
- .env
|
||||
|
||||
ports:
|
||||
- "3306:3306" # Map MariaDB port to host if needed
|
||||
volumes:
|
||||
- ./db:/var/lib/mysql # Mount a volume for MariaDB data
|
||||
|
||||
app:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "5000:5000"
|
||||
env_file:
|
||||
- .env
|
||||
depends_on:
|
||||
- db
|
||||
@@ -1,2 +1,3 @@
|
||||
flask
|
||||
flask_sqlalchemy
|
||||
pymysql
|
||||
27
scenesense-docker/docker-compose.yml
Normal file
27
scenesense-docker/docker-compose.yml
Normal file
@@ -0,0 +1,27 @@
|
||||
version: '3'
|
||||
services:
|
||||
db:
|
||||
image: mariadb:11.0
|
||||
container_name: scenesense-db
|
||||
env_file:
|
||||
- .env
|
||||
volumes:
|
||||
- ./db:/var/lib/mysql
|
||||
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
|
||||
|
||||
app:
|
||||
image: scenesense-server:1.0
|
||||
container_name: scenesense-app
|
||||
ports:
|
||||
- "15000:5000"
|
||||
env_file:
|
||||
- .env
|
||||
depends_on:
|
||||
- db
|
||||
|
||||
adminer:
|
||||
image: adminer:latest
|
||||
container_name: scenesense-db-adminer
|
||||
restart: always
|
||||
ports:
|
||||
- "18000:8080"
|
||||
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE records (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
timestamp VARCHAR(255) NOT NULL,
|
||||
description VARCHAR(255) NOT NULL
|
||||
);
|
||||
@@ -1,7 +0,0 @@
|
||||
# api_keys.py
|
||||
|
||||
api_keys = {
|
||||
"your_api_key": "user1",
|
||||
"another_api_key": "user2",
|
||||
# Add more API keys as needed
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import os
|
||||
|
||||
class Config:
|
||||
# Use the environment variable for the database URI
|
||||
SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI")
|
||||
@@ -1,17 +1,23 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from api_keys import api_keys
|
||||
import config
|
||||
import datetime
|
||||
import os
|
||||
import json
|
||||
|
||||
# Flask app instance
|
||||
app = Flask(__name__)
|
||||
|
||||
# Configuration for SQLAlchemy
|
||||
app.config.from_object(config.Config)
|
||||
app.config[
|
||||
'SQLALCHEMY_DATABASE_URI'
|
||||
] = os.environ.get(
|
||||
"SQLALCHEMY_DATABASE_URI"
|
||||
)
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
# Database Model for Records
|
||||
class Record(db.Model):
|
||||
__tablename__ = 'records'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
timestamp = db.Column(db.String(255), nullable=False)
|
||||
description = db.Column(db.String(255), nullable=False)
|
||||
@@ -23,15 +29,35 @@ def parse_timestamp(timestamp_str):
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
# Function to validate API keys based on their values and access level
|
||||
def validate_api_key(api_key, required_access_level):
|
||||
api_keys_json = os.environ.get('API_KEYS_JSON', '{}')
|
||||
api_keys_dict = json.loads(api_keys_json)
|
||||
|
||||
if api_key in api_keys_dict:
|
||||
access_level = api_keys_dict[api_key].get("access_level")
|
||||
return access_level == required_access_level
|
||||
|
||||
return False
|
||||
|
||||
# API key authentication
|
||||
@app.before_request
|
||||
def authenticate():
|
||||
# Check if the API key provided in the request headers is valid
|
||||
api_key = request.headers.get("Authorization")
|
||||
|
||||
# Allow all requests with RW keys
|
||||
if validate_api_key(api_key, "RW"):
|
||||
return None # Allow the request to proceed
|
||||
|
||||
# Allow GET requests with RO keys
|
||||
if request.method == "GET" and validate_api_key(api_key, "RO"):
|
||||
return None # Allow the request to proceed
|
||||
|
||||
# Return a 401 Unauthorized response for invalid API keys or access levels
|
||||
return jsonify({'error': 'Invalid API key or access level'}), 401
|
||||
|
||||
|
||||
if api_key not in api_keys:
|
||||
# Return a 401 Unauthorized response for invalid API keys
|
||||
return jsonify({'error': 'Invalid API key'}), 401
|
||||
|
||||
# Route to add a new record
|
||||
@app.route(
|
||||
|
||||
Reference in New Issue
Block a user