Skip to content

Commit

Permalink
Merge pull request #11 from Favo02/better-ssr-usage
Browse files Browse the repository at this point in the history
Better SvelteKit SSR usage, fix CORS problems
  • Loading branch information
Favo02 authored Apr 22, 2024
2 parents 15512c7 + 33a71f7 commit 75586d2
Show file tree
Hide file tree
Showing 25 changed files with 544 additions and 474 deletions.
1 change: 0 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
SIHL_ENV=production
SIHL_SECRET=<secret>
DATABASE_URL=postgresql://<username>:<password>@<host>:<port>/<database>
CORS_ORIGIN=<frontend url>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Tech stack

TL;DR: **OCaml** 🐪 (Sihl) + **JavaScript** 🤢 (Svelte) + **PostgreSQL** 🐘
TL;DR: **OCaml** 🐪 (Sihl) + **JavaScript** 🤢 (SvelteKit) + **PostgreSQL** 🐘

> [!TIP]
> If you want to preserve your mental health, you should **NOT** write JavaScript code _(and frontends in general)_. OCaml is just better.
Expand Down
7 changes: 0 additions & 7 deletions backend/src/common/middlewares.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,3 @@ let verify_expiration =
Rock.Middleware.create ~filter: verify_expiration ~name: "verify expiration"

let logger = Opium.Middleware.logger

let allow_cors =
let origin = match Sihl.Configuration.read_string "CORS_ORIGIN" with
| Some origin -> origin
| None -> "*" (* this will make the authorization (Set-Cookie headers) NOT work *)
in
Opium.Middleware.allow_cors ~origins: [ origin ] ()
2 changes: 1 addition & 1 deletion backend/src/common/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let session_return ?(max_age = 86400L) (status : int) (json : (string * string)
Opium.Response.of_json
?status: (Some (Opium.Status.of_code status))
(`Assoc (json |> List.map (fun (k, v) -> (k, `String v))))
|> Session.set_cookie ~max_age:max_age ~scope:"/" ~same_site:"none" ~http_only:true session
|> Session.set_cookie ~max_age:max_age ~scope:"/" ~same_site:"strict" ~http_only:true session
|> Lwt.return

(* same as return but with fixed json payload fiels "error" and "message" *)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let auth = Web.choose ~scope: "/api" ~middlewares: [ require_login; verify_expir
Web.post "/reviews/create" Handlers.Reviews.create;
]

let router = Web.choose ~middlewares: [ logger; allow_cors; ] [
let router = Web.choose ~middlewares: [ logger; ] [
public;
no_auth;
auth;
Expand Down
2 changes: 1 addition & 1 deletion frontend/.env.template
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VITE_API_URL=<backend url>
API_URL=<backend url>
13 changes: 11 additions & 2 deletions frontend/src/routes/+error.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions frontend/src/routes/+page.server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 4 additions & 45 deletions frontend/src/routes/+page.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions frontend/src/routes/login/+page.server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 14 additions & 55 deletions frontend/src/routes/login/+page.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions frontend/src/routes/profile/+page.server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 75586d2

Please sign in to comment.