darold / pgFormatter

A PostgreSQL SQL syntax beautifier that can work as a console program or as a CGI. On-line demo site at http://sqlformat.darold.net/
PostgreSQL License
1.69k stars 101 forks source link

Expand web capabilities of the app #342

Closed matchaxnb closed 3 months ago

matchaxnb commented 4 months ago

Salut Gilles,

I just added a JSON output capability to your project to be able to use it as a service. In order to leverage this feature, you just need to call the website as follows:

curl --no-progress-meter \
  https://your.host -X POST -H 'Content-type: application/json' \
  -d '{"content": "SELECT 1;"}' -H "Accept: application/json" | jq -r '.formatted' 
SELECT
    1;

curl http://your.host -X POST -H 'Content-type: application/json' \
  -d '{"content": "SELECT 1;"}' -H "Accept: application/json";
{"formatted":"SELECT\n    1;\n\n"}

This makes it easily integrated to other services and is the purpose why I use that.

I also included a Dockerfile for CGI to easily test that feature.

Build and run it using:

docker build -t pg_formatter -f cgi.Dockerfile .
docker run -p 8080 --name pg_formatter --rm pg_formatter

You may disable this feature by setting the configuration parameter "enable_api" to 0, similar to how you enable or disable debug.

I hope you find that useful and merge it :)

à la prochaine

darold commented 3 months ago

Great feature, thank you.