How to host via Apache, an API in Flask, and a website in React?

22 Views Asked by At

I've made several attempts to configure Apache to be able to use Flask + React. My request is as follows: when accessing the server, all routes that are not '/api/', should be directed to React. As many as they are, for Flask. On every attempt, I had to choose between one and the other, and I did so by changing the '/api' of the code below:

WSGIScriptAlias /api C:/Users/bruno.peres/Desktop/Site/API/wsgi_scripts/api.wsgi

This is my current VirtualHost

<VirtualHost *:80>

    ServerName site.local

    # Configuração para o React
    DocumentRoot "C:/Users/bruno.peres/Desktop/Site/Frontend"

    <Directory "C:/Users/bruno.peres/Desktop/Site/Frontend">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/api [NC]
    RewriteRule ^/(.*)$ / [R,L]

    # Configuração para a API Flask WSGI
    WSGIScriptAlias /api "C:/Users/bruno.peres/Desktop/Site/API/wsgi_scripts/api.wsgi"

    <Directory "C:/Users/bruno.peres/Desktop/Site/API">
        Require all granted
        AllowOverride All
    </Directory>
</VirtualHost>

This is my .wsgi:

import sys
sys.path.insert(0, 'C:/Users/bruno.peres/Desktop/Site/API')
from __init__ import app as application

This is my API:

from flask import Flask, render_template, request, redirect, url_for, jsonify
import psycopg2

app = Flask(__name__)

@app.route('/api/')
def hello_world():
    return 'Hello Flask under Apache!'

if __name__ == "__main__":
    app.run(debug=True)

I hope to be able to run both on the same Apache server.

0

There are 0 best solutions below