This is a small example project showing a Servant based web server along with the Composite libraries and Opaleye talking to PostgreSQL.
-
Start PostgreSQL. If you don't have one running, install it via Nix, brew, your package manager, download, etc. Then do these two commands to initialize an empty data directory and run PostgreSQL:
initdb exampledbpostgres -D exampledb
-
Create the
exampledatabase and schema using the provided scriptexample.sql:psql postgres < example.sql -
Compile the example server if you haven't already using
stack build. If you don't have stack, you'll need to install that as well.- A few warnings about hpack versions are a-okay.
-
Run the server via
stack exec myawesomeserver-exe. It should start listening on port 8080. -
You can now access the API with
curlor whatever you like to make HTTP requests with. For example,curl http://localhost:8080/users.
You can access the Swagger documentation at http://localhost:8080/. Assuming you have the Swagger
code generation JAR, you can generate a Python client as well, using the generate-swagger.hs
script in example/scripts/:
stack example/scripts/generate-swagger.hs \
--output-dir example/client/ \
--swagger-codegen-jar-path <path-to-jar>Test out the client (first cd to example/client/myawesomeserver_gen):
>>> from myawesomeserver import api, api_client, configuration
>>> config = configuration.Configuration()
>>> config.host = 'http://localhost:8080'
>>> client = api_client.ApiClient(configuration=config)
>>> my_api = api.DefaultApi(api_client=client)
>>> my_api.users_get()
[{'login': 'string', 'usertype': 'Owner'}, {'login': 'Cathy', 'usertype': 'Regular'}]