-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[omm] deplopulate __init__ and delint (#1368)
- Loading branch information
Showing
12 changed files
with
163 additions
and
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/bin/bash | ||
set -e | ||
export OMM_CONFIG=/workspace/.devcontainer/omm_config.py | ||
flask --app OpenMediaMatch db upgrade --directory src/openMediaMatch/migrations | ||
flask --app OpenMediaMatch run --debug | ||
flask --app OpenMediaMatch.app db upgrade --directory /workspace/src/openMediaMatch/migrations | ||
flask --app OpenMediaMatch.app run --debug |
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 |
---|---|---|
@@ -1,78 +1 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
import os | ||
import sys | ||
|
||
import flask | ||
import flask_migrate | ||
import flask_sqlalchemy | ||
|
||
db = flask_sqlalchemy.SQLAlchemy() | ||
migrate = flask_migrate.Migrate() | ||
|
||
|
||
def create_app(): | ||
""" | ||
Create and configure the Flask app | ||
""" | ||
app = flask.Flask(__name__) | ||
if "OMM_CONFIG" in os.environ: | ||
app.config.from_envvar("OMM_CONFIG") | ||
elif sys.argv[0].endswith("/flask"): # Default for flask CLI | ||
# The devcontainer settings. If you are using the CLI outside | ||
# the devcontainer and getting an error, just override the env | ||
app.config.from_pyfile("/workspace/.devcontainer/omm_config.py") | ||
else: | ||
raise RuntimeError("No flask config given - try populating OMM_CONFIG env") | ||
app.config.update( | ||
SQLALCHEMY_DATABASE_URI=app.config.get("DATABASE_URI"), | ||
SQLALCHEMY_TRACK_MODIFICATIONS=False, | ||
) | ||
app.db = db | ||
|
||
db.init_app(app) | ||
migrate.init_app(app, db) | ||
|
||
@app.route("/") | ||
def index(): | ||
""" | ||
Sanity check endpoint showing a basic status page | ||
TODO: in development mode, this could show some useful additional info | ||
""" | ||
return flask.render_template( | ||
"index.html.j2", production=app.config.get("PRODUCTION") | ||
) | ||
|
||
@app.route("/status") | ||
def status(): | ||
""" | ||
Liveness/readiness check endpoint for your favourite Layer 7 load balancer | ||
""" | ||
return "I-AM-ALIVE\n" | ||
|
||
# Register Flask blueprints for whichever server roles are enabled... | ||
# URL prefixing facilitates easy Layer 7 routing :) | ||
# Linters complain about imports off the top level, but this is needed | ||
# to prevent circular imports | ||
from .blueprints import hashing, matching, curation | ||
|
||
if app.config.get("ROLE_HASHER", False): | ||
app.register_blueprint(hashing.bp, url_prefix="/h") | ||
|
||
if app.config.get("ROLE_MATCHER", False): | ||
app.register_blueprint(matching.bp, url_prefix="/m") | ||
|
||
if app.config.get("ROLE_CURATOR", False): | ||
app.register_blueprint(curation.bp, url_prefix="/c") | ||
|
||
from . import models | ||
|
||
@app.cli.command("seed") | ||
def seed_data(): | ||
# TODO: This is a placeholder for where some useful seed data can be loaded; | ||
# particularly important for development | ||
bank = models.Bank(name="bad_stuff", enabled=True) | ||
db.session.add(bank) | ||
db.session.commit() | ||
|
||
return app |
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 |
---|---|---|
@@ -1,3 +1,75 @@ | ||
from OpenMediaMatch import create_app | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
app = create_app() | ||
import os | ||
import sys | ||
|
||
import flask | ||
import flask_migrate | ||
|
||
from OpenMediaMatch import database | ||
from OpenMediaMatch.blueprints import hashing, matching, curation | ||
|
||
|
||
def create_app(): | ||
""" | ||
Create and configure the Flask app | ||
""" | ||
app = flask.Flask(__name__) | ||
migrate = flask_migrate.Migrate() | ||
|
||
if "OMM_CONFIG" in os.environ: | ||
app.config.from_envvar("OMM_CONFIG") | ||
elif sys.argv[0].endswith("/flask"): # Default for flask CLI | ||
# The devcontainer settings. If you are using the CLI outside | ||
# the devcontainer and getting an error, just override the env | ||
app.config.from_pyfile("/workspace/.devcontainer/omm_config.py") | ||
else: | ||
raise RuntimeError("No flask config given - try populating OMM_CONFIG env") | ||
app.config.update( | ||
SQLALCHEMY_DATABASE_URI=app.config.get("DATABASE_URI"), | ||
SQLALCHEMY_TRACK_MODIFICATIONS=False, | ||
) | ||
|
||
database.db.init_app(app) | ||
migrate.init_app(app, database.db) | ||
|
||
@app.route("/") | ||
def index(): | ||
""" | ||
Sanity check endpoint showing a basic status page | ||
TODO: in development mode, this could show some useful additional info | ||
""" | ||
return flask.render_template( | ||
"index.html.j2", production=app.config.get("PRODUCTION") | ||
) | ||
|
||
@app.route("/status") | ||
def status(): | ||
""" | ||
Liveness/readiness check endpoint for your favourite Layer 7 load balancer | ||
""" | ||
return "I-AM-ALIVE\n" | ||
|
||
# Register Flask blueprints for whichever server roles are enabled... | ||
# URL prefixing facilitates easy Layer 7 routing :) | ||
# Linters complain about imports off the top level, but this is needed | ||
# to prevent circular imports | ||
|
||
if app.config.get("ROLE_HASHER", False): | ||
app.register_blueprint(hashing.bp, url_prefix="/h") | ||
|
||
if app.config.get("ROLE_MATCHER", False): | ||
app.register_blueprint(matching.bp, url_prefix="/m") | ||
|
||
if app.config.get("ROLE_CURATOR", False): | ||
app.register_blueprint(curation.bp, url_prefix="/c") | ||
|
||
@app.cli.command("seed") | ||
def seed_data(): | ||
# TODO: This is a placeholder for where some useful seed data can be loaded; | ||
# particularly important for development | ||
bank = database.Bank(name="bad_stuff", enabled=True) | ||
database.db.session.add(bank) | ||
database.db.session.commit() | ||
|
||
return app |
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
20 changes: 17 additions & 3 deletions
20
...-media-match/src/OpenMediaMatch/models.py → ...edia-match/src/OpenMediaMatch/database.py
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,38 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
||
""" | ||
A shim around persistence layer of the OMM instance. | ||
This includes relational data, blob storage, etc. | ||
It doesn't include logging (just use current_app.logger). | ||
We haven't made all of the hard decisions on the storage yet, and | ||
think future deployers may change their mind about which backends to | ||
use. We know we are going to have more than relational data, so | ||
SQLAlchemy isn't going to be enough. Thus an even more abstract | ||
accessor. | ||
""" | ||
|
||
import typing as t | ||
|
||
from flask import g | ||
|
||
from OpenMediaMatch.storage.interface import IUnifiedStore | ||
from OpenMediaMatch.storage.default import DefaultOMMStore | ||
|
||
T = t.TypeVar("T", bound=t.Callable[..., t.Any]) | ||
|
||
|
||
def get_storage() -> IUnifiedStore: | ||
""" | ||
Get the storage object, which is just a wrapper around the real storage. | ||
""" | ||
if "storage" not in g: | ||
# dougneal, you'll need to eventually add constructor arguments | ||
# for this to pass in the postgres/database object. We're just | ||
# hiding flask bits from pytx bits | ||
g.storage = DefaultOMMStore() | ||
return g.storage |
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
Oops, something went wrong.