Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve site from fs #70

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*
!asset/
asset/site/
!bin/
!lib/
!dune
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ build/
.vscode*

# Site dir
asset/site/
asset_site/

# Var dir
_var/
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ RUN opam install . --deps-only
ADD package.json package.json
RUN npm install

# Download site static files
COPY --from=ocurrent/v3.ocaml.org:live /data asset/site/

# Build project
COPY --chown=opam:opam . .
RUN opam exec -- dune build
Expand All @@ -33,6 +30,9 @@ RUN chmod -R 755 /var

RUN git clone https://github.com/ocaml/opam-repository /var/opam-repository

# Download site static files
COPY --from=ocurrent/v3.ocaml.org:live /data asset_site/

EXPOSE 8080

ENTRYPOINT /bin/server
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ utop: ## Run a REPL and link with the project's libraries

.PHONY: update-site
update-site: ## Update the site directory
rm -rf asset/site; ./script/update-site.sh
rm -rf asset_site; ./script/update-site.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ but you will need to download the `v3.ocaml.org` static files, which are crunche
make update-site
```

This will pull the v3.ocaml.org docker image and copy the static files from it into the `asset/site/` directory.
This will pull the v3.ocaml.org docker image and copy the static files from it into the `asset_site/` directory.

Then, build the project with:

Expand Down
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
; Force dune to include _next.

(subdir
asset/site/
asset_site/
(dirs *))
2 changes: 2 additions & 0 deletions lib/ocamlorg_web/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ let to_bool s =

let debug = to_bool @@ env_with_default "OCAMLORG_DEBUG" "true"

let site_dir = env_with_default "OCAMLORG_SITE_DIR" "asset_site/"

let port = env_with_default "PORT" "8080" |> int_of_string

let secret_key =
Expand Down
22 changes: 8 additions & 14 deletions lib/ocamlorg_web/router.ml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
let v3_loader root path request =
let headers, path =
let v3_loader root path =
let path =
let fpath = Fpath.v path in
if Fpath.is_dir_path fpath || not (Fpath.exists_ext fpath) then
(* charset is used internally in dream when setting this *)
( [ "Content-Type", "text/html; charset=utf-8" ]
, Filename.concat path "index.html" )
Filename.concat path "index.html"
else
Dream.mime_lookup path, path
path
in
match Asset.read (root ^ path) with
| None ->
Page_handler.not_found request
| Some asset ->
Dream.respond ~headers asset
Dream.from_filesystem root path

let loader root path request =
match Asset.read (root ^ path) with
Expand All @@ -21,18 +15,18 @@ let loader root path request =
| Some asset ->
Dream.respond ~headers:(Dream.mime_lookup path) asset

let media_loader _root path _request =
let media_loader _root path request =
match Ood_media.read path with
| None ->
Dream.empty `Not_Found
Page_handler.not_found request
| Some asset ->
Dream.respond asset

let site_route =
Dream.scope
""
[ Middleware.i18n ]
[ Dream.get "/**" (Dream.static ~loader:v3_loader "site/") ]
[ Dream.get "/**" (Dream.static ~loader:v3_loader Config.site_dir) ]

let preview_routes =
Dream.scope
Expand Down
2 changes: 1 addition & 1 deletion script/update-site.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

docker pull ocurrent/v3.ocaml.org:live
docker cp $(docker create --rm ocurrent/v3.ocaml.org:live):/data asset/site
docker cp $(docker create --rm ocurrent/v3.ocaml.org:live):/data asset_site/