Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add frontend app #85

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,6 @@ dmypy.json

# Project specific

data/images
static/app
static/img/*/*
gh_pages
17 changes: 2 additions & 15 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
FastAPI,
HTTPException,
Query,
Request,
Response,
UploadFile,
status,
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi_filter import FilterDepends
from fastapi_pagination import Page, add_pagination
Expand Down Expand Up @@ -50,6 +47,8 @@
"name": " AGPL-3.0",
"url": "https://www.gnu.org/licenses/agpl-3.0.en.html",
},
docs_url="/api/docs",
openapi_url="/api/openapi.json",
)

app.add_middleware(
Expand Down Expand Up @@ -102,12 +101,6 @@ def get_current_user(

# Routes
# ------------------------------------------------------------------------------
@app.get("/", response_class=HTMLResponse)
def main_page(request: Request):
return templates.TemplateResponse(
"index.html",
{"request": request},
)


@app.post("/api/v1/auth")
Expand Down Expand Up @@ -303,10 +296,4 @@ def status_endpoint():
return {"status": "running"}


@app.get("/robots.txt", response_class=PlainTextResponse)
def robots_txt():
return """User-agent: *\nDisallow: /"""


add_pagination(app)
app.mount("/images", StaticFiles(directory=str(settings.images_dir)), name="images")
4 changes: 2 additions & 2 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic_settings import BaseSettings, SettingsConfigDict

ROOT_DIR = Path(__file__).parent.parent
DATA_DIR = ROOT_DIR / "data"
STATIC_DIR = ROOT_DIR / "static"


class LoggingLevel(Enum):
Expand Down Expand Up @@ -41,7 +41,7 @@ class Settings(BaseSettings):
oauth2_server_url: str | None = None
sentry_dns: str | None = None
log_level: LoggingLevel = LoggingLevel.INFO
images_dir: Path = DATA_DIR / "images"
images_dir: Path = STATIC_DIR / "img"

model_config = SettingsConfigDict(env_file=".env", extra="ignore")

Expand Down
2 changes: 1 addition & 1 deletion app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class ProofCreate(BaseModel):

file_path: str
mimetype: str
type: ProofTypeEnum
type: ProofTypeEnum | None = None


class ProofBase(ProofCreate):
Expand Down
14 changes: 11 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ x-api-common: &api-common
services:
api:
<<: *api-common
ports:
- "${API_PORT}:8000"
volumes:
- ./data:/opt/open-prices/data
- ./static:/opt/open-prices/static

postgres:
restart: $RESTART_POLICY
Expand All @@ -39,6 +37,16 @@ services:
ports:
- "${POSTGRES_EXPOSE:-127.0.0.1:5432}:5432"

nginx:
image: nginx:1.25-alpine
volumes:
# Mount the nginx configuration file
- ./nginx.conf:/etc/nginx/nginx.conf
# Mount the static files
- ./static:/var/static
ports:
- ${API_PORT}:80

volumes:
postgres-data:
name: ${COMPOSE_PROJECT_NAME:-open_prices}_postgres-data
2 changes: 2 additions & 0 deletions docker/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ x-api-base: &api-base
- ./README.md:/opt/open-prices/README.md
- ./docs:/opt/open-prices/docs
- ./gh_pages:/opt/open-prices/gh_pages
# Make migrations available so that we can run them easily
- ./alembic:/opt/open-prices/alembic

services:
api:
Expand Down
56 changes: 56 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

resolver 127.0.0.11 valid=5s;

server {
# There is a single server block for all requests
listen 80 default_server;
listen [::]:80 default_server;
root /var/static;
index index.html;

location /api/ {
proxy_pass http://api:8000$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_read_timeout 90;
client_max_body_size 50M;
}

# Make vue.js app available under /app
location /app/ {
alias /var/static/app/;
try_files $uri $uri/ /app/index.html;
}
}
}
Empty file added static/img/.gitkeep
Empty file.
6 changes: 3 additions & 3 deletions app/templates/index.html → static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h2 class="emphasized-title" id="how-does-open-prices-work">How
does Open Prices work?</h2>
<p>We are crowdsourcing an open-source dataset of food prices.
Prices can be added by retailers or hackers through <a
href="https://prices.openfoodfacts.org/docs">our API</a>.
href="https://prices.openfoodfacts.org/api/docs">our API</a>.
A web and mobile app is in the works to make it easier for
people to add prices.</p>
<p>Open Prices is not only meant to store individual prices, but also
Expand Down Expand Up @@ -109,7 +109,7 @@ <h4 class="emphasized-title" id="how-can-i-contribute">How
<p>You can contribute by adding prices to the dataset. You
can
do so by using <a
href="https://prices.openfoodfacts.org/docs">our API</a>
href="https://prices.openfoodfacts.org/api/docs">our API</a>
or by using our web or mobile app (coming soon). You
need an
Open Food Facts account to contribute. If you don’t have
Expand Down Expand Up @@ -204,7 +204,7 @@ <h4 class="emphasized-title" id="how-can-i-get-in-touch">How
</div>
<div class="small-12 medium-8 columns">
<p>You can contribute prices by using <a
href="https://prices.openfoodfacts.org/docs">our API</a>.
href="https://prices.openfoodfacts.org/api/docs">our API</a>.
If you want to contribute prices at scale, please get in
touch with us at <a
href="mailto:[email protected]">[email protected]</a>.</p>
Expand Down
2 changes: 2 additions & 0 deletions static/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
5 changes: 0 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ def location(db=override_get_db()):

# Tests
# ------------------------------------------------------------------------------
def test_hello():
response = client.get("/")
assert response.status_code == 200


def test_create_price(user, db=override_get_db()):
# without authentication
response = client.post(
Expand Down
Loading