Skip to content

Commit

Permalink
Merge pull request #678 from ocsigen/static
Browse files Browse the repository at this point in the history
Static linking without config file
  • Loading branch information
balat authored Sep 2, 2024
2 parents b50323b + 2caf9d4 commit dff77b9
Show file tree
Hide file tree
Showing 22 changed files with 306 additions and 135 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ install: all META
cp $(SERVER_CMI) $(OCAMLFIND_DESTDIR)/$(PKG_NAME)/server
cp $(SERVER_CMX) $(OCAMLFIND_DESTDIR)/$(PKG_NAME)/server
cp $(LIBDIR)/$(PKG_NAME).client.cma $(OCAMLFIND_DESTDIR)/$(PKG_NAME)/client
cp $(LIBDIR)/$(PKG_NAME).server.a $(OCAMLFIND_DESTDIR)/$(PKG_NAME)/server
cp $(LIBDIR)/$(PKG_NAME).server.cm* $(OCAMLFIND_DESTDIR)/$(PKG_NAME)/server
scripts/install.sh $(TEMPLATE_DIR) $(TEMPLATE_NAME)

Expand Down
4 changes: 2 additions & 2 deletions opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
opam-version: "2.0"
name: "ocsigen-start"
version: "6.3.0"
version: "7.0.0"
authors: "[email protected]"
maintainer: "[email protected]"
synopsis: "Higher-level library for developing Web and mobile applications with users, registration, notifications, etc"
Expand All @@ -23,7 +23,7 @@ depends: [
"pgocaml_ppx" {>= "4.0"}
"safepass" {>= "3.0"}
"ocsigen-i18n" {>= "4.0.0"}
"eliom" {>= "10.4.0" & < "11.0.0"}
"eliom" {>= "11.0.0" & < "12.0.0"}
"ocsigen-toolkit" {>= "2.7.0"}
"ocsigen-ppx-rpc"
"ocsigen-i18n" {>= "3.7.0"}
Expand Down
4 changes: 2 additions & 2 deletions template.distillery/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ cordova
# Style
.sass-cache

static/css/%%%PROJECT_NAME%%%.css.map
static/css/%%%PROJECT_NAME%%%.css
static/css/project_name.css.map
static/css/project_name.css

# Dune
_build
9 changes: 3 additions & 6 deletions template.distillery/Makefile.options
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ USE_NPM := yes
##----------------------------------------------------------------------
## The following part contains the configuration for the ocsigenserver.

# User to run server with (make run.*)
WWWUSER := www-data
WWWGROUP := www-data

# Port for running the server (make run.*)
PORT := 80
PORT := 8080

# Port for testing (make test.*)
TEST_PORT := 8080

# Root of installation (must end with /)
PREFIX := /usr/local/
#PREFIX := /usr/local/
PREFIX := ${OPAM_SWITCH_PREFIX}/

# Local folder for make test.* (must end with /)
TEST_PREFIX := local/
Expand Down
56 changes: 41 additions & 15 deletions template.distillery/Makefile.os
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ all byte opt:: ${VOLATILE_SCHEMA}

DIST_FILES = $(ELIOMSTATICDIR)/$(PROJECT_NAME).js $(LIBDIR)/$(PROJECT_NAME).cma

.PHONY: test.byte test.opt staticfiles
.PHONY: test.byte test.opt staticfiles byte opt

test.byte:: byte | $(addprefix $(TEST_PREFIX),$(DIST_DIRS)) staticfiles
@echo "==== The website is available at http://localhost:$(TEST_PORT) ===="
Expand All @@ -56,30 +56,52 @@ test.opt:: opt | $(addprefix $(TEST_PREFIX),$(DIST_DIRS)) staticfiles
@echo "==== The website is available at http://localhost:$(TEST_PORT) ===="
$(OCSIGENSERVER.OPT) $(RUN_DEBUG) -c $(patsubst %.conf.in,$(TEST_PREFIX)$(ETCDIR)/%-test.conf,$(CONF_IN))

test.static.byte: static.byte | $(addprefix $(TEST_PREFIX),$(DIST_DIRS)) staticfiles
@echo "==== The website is available at http://localhost:$(TEST_PORT) ===="
dune exec ./$(PROJECT_NAME)_main.bc
test.static.opt: static.opt | $(addprefix $(TEST_PREFIX),$(DIST_DIRS)) staticfiles
@echo "==== The website is available at http://localhost:$(TEST_PORT) ===="
dune exec ./$(PROJECT_NAME)_main.exe

$(addprefix $(TEST_PREFIX), $(DIST_DIRS)):
mkdir -p $@

staticfiles:
cp -rf $(LOCAL_STATIC_CSS) $(LOCAL_STATIC_IMAGES) $(LOCAL_STATIC_FONTS) $(TEST_PREFIX)$(ELIOMSTATICDIR)

##----------------------------------------------------------------------
## Static executable

static.byte: byte
dune build $(PROJECT_NAME)_main.bc

static.opt: opt
dune build $(PROJECT_NAME)_main.exe

##----------------------------------------------------------------------
## Installing & Running

.PHONY: install install.byte install.byte install.opt install.static install.etc install.lib install.lib.byte install.lib.opt run.byte run.opt
.PHONY: install install.exe install.byte install.byte install.opt install.static install.etc install.lib install.lib.byte install.lib.opt run.byte run.opt
install: install.byte install.opt
install.exe: install.etc install.static
dune install
install.byte: install.lib.byte install.etc install.static | $(addprefix $(PREFIX),$(DATADIR) $(LOGDIR) $(shell dirname $(CMDPIPE)))
install.opt: install.lib.opt install.etc install.static | $(addprefix $(PREFIX),$(DATADIR) $(LOGDIR) $(shell dirname $(CMDPIPE)))
install.lib: install.lib.byte install.lib.opt
install.lib.byte: $(TEST_PREFIX)$(LIBDIR)/$(PROJECT_NAME).cma | $(PREFIX)$(LIBDIR)
install $< $(PREFIX)$(LIBDIR)
install.lib.opt: $(TEST_PREFIX)$(LIBDIR)/$(PROJECT_NAME).cmxs | $(PREFIX)$(LIBDIR)
install $< $(PREFIX)$(LIBDIR)
install.lib.byte: byte | $(PREFIX)$(LIBDIR)
install $(TEST_PREFIX)$(LIBDIR)/$(PROJECT_NAME).cma $(PREFIX)$(LIBDIR)
install.lib.opt: opt | $(PREFIX)$(LIBDIR)
install $(TEST_PREFIX)$(LIBDIR)/$(PROJECT_NAME).cmxs $(PREFIX)$(LIBDIR)
install.static: $(TEST_PREFIX)$(ELIOMSTATICDIR)/$(PROJECT_NAME).js | $(PREFIX)$(STATICDIR) $(PREFIX)$(ELIOMSTATICDIR)
cp -r $(LOCAL_STATIC_CSS) $(PREFIX)$(FILESDIR)
cp -r $(LOCAL_STATIC_IMAGES) $(PREFIX)$(FILESDIR)
cp -r $(LOCAL_STATIC_FONTS) $(PREFIX)$(FILESDIR)
[ -z $(WWWUSER) ] || chown -R $(WWWUSER) $(PREFIX)$(FILESDIR)
install $(addprefix -o ,$(WWWUSER)) $< $(PREFIX)$(ELIOMSTATICDIR)
HASH=`md5sum _build/default/client/$(PROJECT_NAME).bc.js | cut -d ' ' -f 1` && \
install $(addprefix -o ,$(WWWUSER)) $(JS_PREFIX)_$$HASH.js $(PREFIX)$(ELIOMSTATICDIR) && \
ln -sf $(PROJECT_NAME)_$$HASH.js $(PREFIX)$(ELIOMSTATICDIR)/$(PROJECT_NAME).js
HASH=`cat $(LOCAL_CSS) | md5sum | cut -d ' ' -f 1` && \
install $(addprefix -o ,$(WWWUSER)) $(CSS_PREFIX)_$$HASH.css $(PREFIX)$(ELIOMSTATICDIR)/css && \
ln -sf $(PROJECT_NAME)_$$HASH.css $(PREFIX)$(ELIOMSTATICDIR)/css/$(PROJECT_NAME).css
install.etc: $(TEST_PREFIX)$(ETCDIR)/$(PROJECT_NAME).conf | $(PREFIX)$(ETCDIR)
install $< $(PREFIX)$(ETCDIR)/$(PROJECT_NAME).conf

Expand All @@ -95,12 +117,19 @@ $(addprefix $(PREFIX),$(DATADIR) $(LOGDIR) $(ELIOMSTATICDIR) $(shell dirname $(C
install $(addprefix -o ,$(WWWUSER)) -d $@

run.byte:
@echo "==== Running ocsigenserver with configuration file ===="
@echo "==== The website is available at http://localhost:$(PORT) ===="
$(OCSIGENSERVER) $(RUN_DEBUG) -c ${PREFIX}${ETCDIR}/${PROJECT_NAME}.conf
run.opt:
@echo "==== Running ocsigenserver with configuration file ===="
@echo "==== The website is available at http://localhost:$(PORT) ===="
$(OCSIGENSERVER.OPT) $(RUN_DEBUG) -c ${PREFIX}${ETCDIR}/${PROJECT_NAME}.conf

run.static:
@echo "==== Running static executable ===="
@echo "==== The website is available at http://localhost:$(PORT) ===="
${PROJECT_NAME}

##----------------------------------------------------------------------

##----------------------------------------------------------------------
Expand Down Expand Up @@ -155,7 +184,7 @@ $(TEST_CONFIG_FILES): $(TEST_PREFIX)$(ETCDIR)/%-test.conf: %.conf.in $(JS_AND_CS
##----------------------------------------------------------------------
## Compilation

.PHONY: gen-dune config-files
.PHONY: config-files

config-files: | $(TEST_PREFIX)$(ELIOMSTATICDIR) $(TEST_PREFIX)$(LIBDIR)
HASH=`md5sum _build/default/client/$(PROJECT_NAME).bc.js | cut -d ' ' -f 1` && \
Expand All @@ -164,20 +193,17 @@ config-files: | $(TEST_PREFIX)$(ELIOMSTATICDIR) $(TEST_PREFIX)$(LIBDIR)
cp -f _build/default/$(PROJECT_NAME).cm* $(TEST_PREFIX)$(LIBDIR)/
$(MAKE) $(CONFIG_FILES) $(TEST_CONFIG_FILES) PROJECT_NAME=$(PROJECT_NAME)

all:: gen-dune
all::
$(ENV_PSQL) dune build $(DUNE_OPTIONS) @install @$(PROJECT_NAME) $(PROJECT_NAME).cmxs

byte:: gen-dune
byte::
$(ENV_PSQL) dune build $(DUNE_OPTIONS) @$(PROJECT_NAME)
make config-files PROJECT_NAME=$(PROJECT_NAME)

opt:: gen-dune
opt::
$(ENV_PSQL) dune build $(DUNE_OPTIONS) $(PROJECT_NAME).cmxs @$(PROJECT_NAME)
make config-files PROJECT_NAME=$(PROJECT_NAME)

gen-dune:
@ocaml tools/gen_dune.ml > client/dune.client

##----------------------------------------------------------------------

##----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion template.distillery/Makefile.style
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ifeq ($(shell which sassc),)
[ -d $(SASSDIR) ] && \
SASS_PATH=$(SASS_PATH) sass --style compressed $(SASS_SRC) $@
else
[ -d $(SASSDIR) ] && \
[ -d $(SASSDIR) ] &&
sassc -t compressed $(addprefix -I ,$(subst :, ,$(SASS_PATH))) $(SASS_SRC) $@
endif
$(POSTCSS) --use autoprefixer --replace $@
Expand Down
6 changes: 3 additions & 3 deletions template.distillery/PROJECT_NAME.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
<!-- we use the PostgreSQL backend for ocsipersist,
but there is also a DBM and an SQLite backend -->
<!--
<extension findlib-package="ocsipersist.dbm">
<extension findlib-package="ocsipersist-dbm-config">
<delayloading val="false"/>
<store dir="%%DATADIR%%"/>
<ocsidbm name="ocsidbm"/>
</extension>
-->
<!--
<extension findlib-package="ocsipersist.sqlite">
<extension findlib-package="ocsipersist-sqlite-config">
<database file="%%DATADIR%%/ocsidb"/>
</extension>
-->
<extension findlib-package="ocsipersist.pgsql">
<extension findlib-package="ocsipersist-pgsql-config">
<database
host="%%DB_HOST%%"
port="%%DB_PORT%%"
Expand Down
4 changes: 3 additions & 1 deletion template.distillery/PROJECT_NAME.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ module Demo_timepicker = Demo_timepicker
module Demo_tips = Demo_tips
module Demo_tongue = Demo_tongue
module Demo_users = Demo_users
module %%%MODULE_NAME%%%_config = %%%MODULE_NAME%%%_config

[%%client.start]

module %%%MODULE_NAME%%%_config = %%%MODULE_NAME%%%_config
module %%%MODULE_NAME%%%_language = %%%MODULE_NAME%%%_language
module %%%MODULE_NAME%%%_mobile = %%%MODULE_NAME%%%_mobile
module %%%MODULE_NAME%%%_phone_connect = %%%MODULE_NAME%%%_phone_connect
8 changes: 4 additions & 4 deletions template.distillery/PROJECT_NAME.opam
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
opam-version: "2.0"
name: "%%%PROJECT_NAME%%%"
version: "0.1"
synopsis: "Pseudo package for defining project dependencies"
synopsis: "%%%PROJECT_NAME%%%"

depends: [
"eliom" {>= "10.0.0" & < "11.0.0"}
"ocsipersist-pgsql" {>= "1.0" & < "2.0"}
"ocsigen-start"
"eliom" {>= "11.0.0" & < "12.0.0"}
"ocsipersist-pgsql-config" {>= "2.0" & < "3.0"}
"ocsigen-start" {>= "7.0.0" & < "8.0.0"}
]
1 change: 1 addition & 0 deletions template.distillery/PROJECT_NAME_base.eliom
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(* This file was generated by Ocsigen Start.
Feel free to use it, modify it, and redistribute it as you wish. *)

let%server () = %%%MODULE_NAME%%%_static_config.set_static_config ()
let%server application_name = !%%%MODULE_NAME%%%_config.app_name
let%client application_name = Eliom_client.get_application_name ()
let%shared displayed_app_name = "%%%PROJECT_NAME%%%"
Expand Down
4 changes: 3 additions & 1 deletion template.distillery/PROJECT_NAME_config.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ let os_db =
in
element ~name:"os-db" ~attributes ()

let _ = Eliom_config.parse_config [app; avatars; os_db]
let _ =
if Ocsigen_config.has_configuration_file ()
then Eliom_config.parse_config [app; avatars; os_db]
9 changes: 9 additions & 0 deletions template.distillery/PROJECT_NAME_main.eliom
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(** This is the main file if you are using static linking without config file.
It is not used if you are using a config file and ocsigenserver *)

module%shared %%%MODULE_NAME%%% = %%%MODULE_NAME%%%

let%server _ =
Ocsigen_server.start
[ Ocsigen_server.host
[Staticmod.run ~dir:"local/var/www/%%%PROJECT_NAME%%%" (); Eliom.run ()] ]
6 changes: 0 additions & 6 deletions template.distillery/PROJECT_NAME_mobile.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@

[%%client.start]

[@@@ocaml.warning "-33"]

open %%%MODULE_NAME%%% (* for dependency reasons *)

[@@@ocaml.warning "+33"]

[%%client open Js_of_ocaml]
[%%client open Js_of_ocaml_lwt]

Expand Down
31 changes: 31 additions & 0 deletions template.distillery/PROJECT_NAME_static_config.eliom.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(* Set static configuration in case you are using static linking only.
These information are taken from the config file otherwise.
*)
let%server set_static_config () =
if not (Ocsigen_config.has_configuration_file ())
then begin
Ocsigen_config.set_ports [`All, 8080];
Ocsigen_config.set_veryverbose ();
Ocsigen_config.set_debugmode true;
Ocsigen_config.set_logdir "local/var/log/%%%PROJECT_NAME%%%";
Ocsigen_config.set_datadir "local/var/data/%%%PROJECT_NAME%%%";
Ocsigen_config.set_uploaddir (Some "/tmp");
Ocsigen_config.set_usedefaulthostname true;
Ocsigen_config.set_command_pipe "local/var/run/%%%PROJECT_NAME%%%-cmd";
Ocsigen_config.set_default_charset (Some "utf-8");
Ocsipersist_settings.set_host "%%PGHOST%%";
Ocsipersist_settings.set_port %%PGPORT%%;
Ocsipersist_settings.set_database "ocsipersist_%%%PROJECT_NAME%%%";
Ocsipersist.init ();
%%%MODULE_NAME%%%_config.os_db_database := Some "%%PGDATABASE%%";
%%%MODULE_NAME%%%_config.os_db_host := Some "%%PGHOST%%";
%%%MODULE_NAME%%%_config.os_db_user :=
(let u = "%%PGUSER%%" in if u = "" then None else Some u);
%%%MODULE_NAME%%%_config.os_db_password := Some "%%PGPASSWORD%%";
%%%MODULE_NAME%%%_config.os_db_port := Some %%PGPORT%%;
%%%MODULE_NAME%%%_config.app_name :=
Filename.chop_suffix
(Unix.readlink "local/var/www/%%%PROJECT_NAME%%%/%%%PROJECT_NAME%%%.js") ".js" ;
%%%MODULE_NAME%%%_config.css_name :=
Unix.readlink "local/var/www/%%%PROJECT_NAME%%%/css/%%%PROJECT_NAME%%%.css";
end
37 changes: 29 additions & 8 deletions template.distillery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ you can try updating it with `sudo npm install -g npm`. Depending on your
setup, you may have to update your `$PATH` for the new `npm` to become
visible.

Generally, you can compile it and run ocsigenserver on it by

First create a local database:
```shell
make db-init
make db-create
make db-schema
```
Then compile and run the app, either as a library loaded dynamically
into ocsigenserver using a configuration file:
```shell
make test.byte (or test.opt)
```
or as an OCaml executable, using ocsigenserver as a libary
(without configuration file):
```shell
make test.static.byte (or test.static.opt)
```

Then connect to `http://localhost:8080` to see the running app skeleton.
Registration will work only if sendmail if configured on your system.
Expand Down Expand Up @@ -95,30 +103,43 @@ make db-status
```

- Test your application by compiling it and running ocsigenserver locally
with a configuration file:
```
make test.byte (or test.opt)
```

- Test your application by compiling it as an OCaml executable without
configuration file:
```
make test.static.byte (or test.static.opt)
```

- Compile it only
```Shell
make all (or byte or opt)
```

- Deploy your project on your system
```Shell
sudo make install (or install.byte or install.opt)
make install (or install.byte or install.opt)
```

- Run the server on the deployed project
```Shell
sudo make run.byte (or run.opt)
make run.byte (or run.opt)
```

- Run the static executable of the deployed project
```Shell
make run.static
```

If `WWWUSER` in the `Makefile.options` is you, you don't need the
`sudo`. If Eliom isn't installed globally, however, you need to
re-export some environment variables to make this work:
If you want to use a global install, update PREFIX in `Makefile.options`
and run make ```install``` as root. If OCaml/Eliom is not installed
globally, you will need to re-export some environment variables to make
this work:
```Shell
sudo PATH=$PATH OCAMLPATH=$OCAMLPATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH make run.byte/run.opt
sudo PATH=$PATH OCAMLPATH=$OCAMLPATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH make run.opt
```

- If you need a findlib package in your project, add it to the
Expand Down
Loading

0 comments on commit dff77b9

Please sign in to comment.