mte/src/mte.ml

98 lines
3.6 KiB
OCaml
Raw Normal View History

2025-09-21 17:44:14 +02:00
(* MTE - the MirageOS Taler Exchange
Copyright (C) 2025 Olivier Pierre <swrup@protonmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
2025-09-28 16:58:47 +02:00
the Free Software Foundation, version 3.
2025-09-21 17:44:14 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. *)
2025-09-28 18:36:25 +02:00
(* /terms + /privacy
- try to find a response with an acceptable mime-type
- pick the version in the most preferred language of the user
- apply compression if that is allowed by the client
- set ETAG header
- If it did not change, a "304 Not Modified" response will be returned
- A "Taler-Terms-Version" header is generated to indicate the legal version of the terms
- When returning a full response (not a "304 Not Modified"),
include a "Avail-Languages" header: a comma-separated list of the languages available *)
2025-09-23 20:58:34 +02:00
module Static = struct
2025-09-28 17:24:32 +02:00
let static kind req _server _env =
let get_ok = function
| Error e -> Fmt.failwith "TODO handle me, %s" e
| Ok v -> v
2025-09-23 20:58:34 +02:00
in
2025-09-28 17:24:32 +02:00
let etag =
(* TODO keep our static asset etag as Etag.t in Config? *)
Assets.etag kind |> Headers.Etag.of_crockford32 |> Result.get_ok
2025-09-23 20:58:34 +02:00
in
let headers = Vif.Request.headers req in
2025-09-28 17:24:32 +02:00
let has_matching_etag =
match Vif.Headers.get headers "if-none-match" with
2025-09-28 19:37:49 +02:00
| None -> false
2025-09-28 17:24:32 +02:00
| Some s ->
2025-09-28 19:37:49 +02:00
s |> Headers.Etag.parse |> get_ok |> Headers.Etag.evaluate etag
2025-09-23 20:58:34 +02:00
in
2025-09-28 17:24:32 +02:00
match has_matching_etag with
| true ->
2025-09-28 19:37:49 +02:00
let open Vif.Response.Syntax in
2025-09-28 17:24:32 +02:00
let* () = Vif.Response.empty in
Vif.Response.respond `Not_modified
| false ->
2025-09-28 19:37:49 +02:00
let mime = Headers.select_mimetype headers |> get_ok in
let lang = Headers.select_language headers |> get_ok in
2025-09-28 17:24:32 +02:00
let compression = Headers.select_encoding headers in
2025-09-28 19:37:49 +02:00
let data = Assets.get_content ~mime ~lang kind in
(* -- *)
let open Vif.Response.Syntax in
2025-09-28 17:24:32 +02:00
let* () = Vif.Response.with_string ?compression req data in
2025-09-28 19:37:49 +02:00
let* () =
let etag_field_value = Headers.Etag.to_field_value etag in
Vif.Response.add ~field:"etag" etag_field_value
in
2025-09-28 18:23:43 +02:00
let* () =
(* todo: is it "taler-privacy-version" for /policy ? *)
Vif.Response.add ~field:"taler-terms-version"
Config.terms_legal_version
in
2025-09-28 18:36:25 +02:00
let* () =
Vif.Response.add ~field:"avail-languages"
Headers.avail_languages_header_value
in
2025-09-28 17:24:32 +02:00
let* () =
2025-09-28 19:37:49 +02:00
let content_type = Fmt.str "%s/%s" (fst mime) (snd mime) in
Vif.Response.add ~field:"content-type" content_type
2025-09-28 17:24:32 +02:00
in
Vif.Response.respond `OK
2025-09-23 20:58:34 +02:00
let terms = static Assets.Terms
let privacy = static Assets.Privacy
end
2025-09-13 15:56:19 +02:00
2025-09-28 20:04:52 +02:00
let hello req _server _env =
let open Vif.Response.Syntax in
let* () = Vif.Response.with_string req "Hello~~\n" in
let* () = Vif.Response.add ~field:"content-type" "text/plain" in
Vif.Response.respond `OK
2025-09-13 15:56:19 +02:00
let routes =
let open Vif.Uri in
let open Vif.Route in
(*let open Vif.Type in*)
[
2025-09-28 20:04:52 +02:00
get (rel /?? nil) --> hello; get (rel / "terms" /?? nil) --> Static.terms
2025-09-23 20:58:34 +02:00
; get (rel / "privacy" /?? nil) --> Static.privacy
2025-09-13 15:56:19 +02:00
]
2025-09-13 17:57:29 +02:00
let () =
Miou_unix.run @@ fun () ->
let env = () in
let middlewares = Vif.Middlewares.[] in
Vif.run ~middlewares routes env