0

It has been a while I built an app with Angularjs front-end and API back-end, so forgive me if this a pretty dumb question. I would like to run a Python back-end which serves an API. Every endpoint should start with /api/. Now this app runs in the folder 'api", this is somewhat my folder structure:

-api/
-- controllers/
-- __init__.py
-config/
-public/
-- assets/
-- index.html
-server.py

Now I am figuring out how I could run a webserver, which accesses the files from the api folder when I use an endpoint which starts with /api, and every other endpoint should access my AngularJS app which is available from the public folder. This way I can access the Python API endpoints from my AngularJS app and still be pretty secure because my Python source files are not available from the public folder.

So, AngularJS endpoints: /home /login /logout etc.

Python API endpoints: /api/user, /api/user/profile etc. (every single endpoint which starts with /api/.

Any ideas? I could run two servers on different ports, but that should not be the way to go. I've done something like this with PHP, but never done this with Python and somehow it gives me a headache at the moment.

If someone can help me out, that would be great.

1 Answer 1

1

You can use nginx to deploy this project.

server {
    listen 80;
    listen [::]:80;

    server_name xyz.com;

    root /var/www/frontend/xyz-frontend/dist;
    index index.php index.html index.htm index.nginx-debian.html;


    # handles the api requests
    location /api {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            proxy_pass http://unix:/var/www/services/xyz/api.sock;
    }

    # FOR ANGULAR BUILD
    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html /custom_50x.html;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Welcome. I think you should use a framework for the python server.
I do, I use Flask. I guess, what I did with PHP, was using PHP for the API and let PHP render the index.html on every non-api URL. The public folder contained one PHP file (index.php), which only loaded (require) a Bootstrap.php, which was located in the root folder. Perhaps, for development, I should be able to render the index.html on every non-api url, as default, so angular takes care of the url endpoints from there. That should work I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.