简体   繁体   中英

Python API server with angular front-end

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.

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM