A minimal example of a lightweight webapp in OCaml.
It features a possible organization of files to maximize code sharing between server (native OCaml) and client (BuckleScript). Some of the things shared are:
- React components, which are server-side rendered with Tyxml and hydrated with ReasonReact
- Routes, through the OCaml library Routes.
- API endpoints interface types, through library ATD, that also generates encoders / decoders automatically.
shared folder can contain other shared code like validation functions, data processing, etc.
Create an opam local switch:
dune in the newly created switch:
opam install dune
Install all dependencies:
Build and run the server:
Open the browser and go to http://localhost:3000/.
It's also possible to get hot reloading while developing. For that, start the server (to be able to serve API requests) with
make run, run BuckleScript in
yarn start and then start Webpack dev server with
Run databases locally
docker-compose -f docker-compose.dev.yml up -d
Deploying to Heroku
Right now, the example allows to easily deploy the app to Heroku. Build times are longer than they should, but hopefully this will be fixed soon.
- Install the Heroku CLI: http://toolbelt.heroku.com/
heroku create your_appfrom the app folder
- Set stack for the app to
heroku stack:set container
git push heroku master
- Amazing tutorial to create a lightweight OCaml webapp: https://shonfeder.gitlab.io/ocaml_webapp/
- Deploying native Reason/OCaml with Zeit's now.sh: https://jaredforsyth.com/posts/deploying-native-reason-ocaml-with-now-sh/
- Deploying OCaml server on Heroku: https://medium.com/@aleksandrasays/deploying-ocaml-server-on-heroku-f91dcac11f11