Swi prolog as an API for a JS framework

Is it possible to use Prolog as an API via Apache? I want to use Vue.js in my project. I have an idea to use Apache2 + Vue.js on port 80 and http_server/2 on another port. Then do requests from JS to Prolog’s port. Although I’ve never launched the Prolog’s server.
So here are two questions:

  1. Is that a good way to connect the framework with Prolog?
  2. How to start the Prolog’s server in the background mode?

hi, I use about the same things, I run swi-prolog server on port 8443 with ssl certificate

I’ve used SWI with Vue, it works.

  1. I run SWI on one port and point Apache2 (or NGINX) at that, in NGINX it’s proxy_pass, I’ve not used Apache but it does the same job. For Vue if it’s just a static HTML page being served, along with the compiled JavaScript, then Apache can serve it. Otherwise let Apache serve all the static files and SWI serve the HTML page that the Vue app is loaded into. Then JS sends requests still on port 80 to Apache, which sends it on to SWI-Prolog. This way you don’t need to expose the SWI-Prolog port to the world on your server. Keep as few ports exposed on a server as possible as a general rule of thumb.

  2. I write a quick run.sh script like so:

#!/usr/bin/env bash
/usr/bin/swipl --quiet -s /path/to/entry_point.pl -g 'thread_get_message(stop)'

Then I use systemd and create a service for the swipl app, setting the ExecStart parameter to that run script. Enable the service and start it. The thread_get_message(stop) goal keeps the SWI process running, I like to pass it in from the command-line so I don’t need it in my development code which I’ll be running with the REPL, but you can manage that in several ways.

1 Like

I use a setting under Win10. The browser (JS) layer containing Vue.js and Babylon.js talking to a Logtalk app (includes a server, SWI als backend) and to a Python app (includes also a server). The browser is also a router makes it possible that Python and Logtalk can talk together. The communication is a simple JSON protocol (inspired XMPP) over Websockets. Works great.

Cheers

Hans

@PaulBrownMagic @hnbeck thank you guys. I used Docker for this stuff and created a docker-compose file:

version: '3'
services:
  nginx:
    restart: always
    container_name: gnginx
    image: nginx
    network_mode: "host"
    ports:
      - "80:80"
    volumes:
     - $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
     - $PWD/nginx:/var/log/nginx

  vue:
    build: ./vue
    restart: always
    ports:
      - "8080:8080"
    volumes:
      - ./vue:/var/www/swi-vue/project/vue
      #- /var/www/swi-vue/project/vue/node_modules

  node:
    build: ./node-api
    restart: always
    ports:
      - "8070:8070"
    volumes:
      - ./node-api:/var/www/swi-vue/project/node-api
      - /var/www/swi-vue/project/node-api/node_modules

  mongo:
    image: mongo
    restart: always
    volumes:
      - ./mongo:/data/db
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8090:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: root

and nginx.conf

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;

        #location /api {
        #    proxy_pass http://backend:8060;
        #}

        location /api {
            proxy_pass http://localhost:8070;
        }

        location / {
            proxy_pass http://localhost:8080;

            # The following is for the websocket connection of the webpack dev server (https://gist.github.com/simongfxu/ea128160c296f31e41e6)
             proxy_redirect off;
             proxy_http_version 1.1;
             proxy_set_header Upgrade $http_upgrade;
             proxy_set_header Connection "upgrade";
        }
    }
}

Using Nginx you can use proxy_pass to navigate through ports so there is no need to use CORS.

1 Like