Skip to content

Commit 9217c85

Browse files
committed
basic server stdlib
1 parent 6f0666b commit 9217c85

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

lib/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(library
22
(public_name rustscript)
3-
(libraries base stdio)
3+
(libraries base stdio lwt cohttp cohttp-lwt-unix)
44
(modules run types parser scanner eval operators preprocess)
55
(preprocess (pps ppx_blob))
66
(preprocessor_deps (file stdlib.rsc)))

lib/eval.ml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,57 @@ and typeof_builtin (args, state) _ss _loc =
345345
| Tuple [StringVal _] -> Atom 10, state
346346
| _ -> assert false
347347

348+
and serve_builtin (args, interpreter_state) ss loc =
349+
match args with
350+
| Tuple [Integer port; Lambda lambda] ->
351+
let open Lwt in
352+
let open Cohttp in
353+
let open Cohttp_lwt_unix in
354+
355+
let callback _conn req body =
356+
let uri = req |> Request.uri |> Uri.to_string in
357+
let meth = req |> Request.meth |> Code.string_of_method in
358+
let headers = req |> Request.headers |> Header.to_string in
359+
( body |> Cohttp_lwt.Body.to_string >|= fun body -> body )
360+
>>= fun body ->
361+
let args = Tuple [StringVal uri; StringVal meth; StringVal headers; StringVal body] in
362+
let thunk = Thunk {thunk_fn = lambda; thunk_args = args; thunk_fn_name = ResolvedIdent ~-1} in
363+
let res = match unwrap_thunk thunk interpreter_state ss loc with
364+
| StringVal s, _ -> s
365+
| _ -> assert false
366+
in
367+
Server.respond_string ~status:`OK ~body:res ()
368+
in
369+
Server.create ~mode:(`TCP (`Port port)) (Server.make ~callback ())
370+
| _ ->
371+
assert false
372+
373+
and serve_ssl_builtin (args, interpreter_state) ss loc =
374+
match args with
375+
| Tuple [StringVal cert_path; StringVal key_path; Integer port; Lambda lambda] ->
376+
let open Lwt in
377+
let open Cohttp in
378+
let open Cohttp_lwt_unix in
379+
380+
let callback _conn req body =
381+
let uri = req |> Request.uri |> Uri.to_string in
382+
let meth = req |> Request.meth |> Code.string_of_method in
383+
let headers = req |> Request.headers |> Header.to_string in
384+
( body |> Cohttp_lwt.Body.to_string >|= fun body -> body )
385+
>>= fun body ->
386+
let args = Tuple [StringVal uri; StringVal meth; StringVal headers; StringVal body] in
387+
let thunk = Thunk {thunk_fn = lambda; thunk_args = args; thunk_fn_name = ResolvedIdent ~-1} in
388+
let res = match unwrap_thunk thunk interpreter_state ss loc with
389+
| StringVal s, _ -> s
390+
| _ -> assert false
391+
in
392+
Server.respond_string ~status:`OK ~body:res ()
393+
in
394+
let tls_config = `Crt_file_path cert_path, `Key_file_path key_path, `No_password, `Port port in
395+
Server.create ~mode:(`TLS tls_config) (Server.make ~callback ())
396+
| _ ->
397+
assert false
398+
348399
and eval_pipe ~tc lhs rhs ss loc = fun s ->
349400
let (lhs, s) = (eval_expr lhs ss) s in
350401
let (rhs, s) = (eval_expr rhs ss) s in
@@ -539,6 +590,12 @@ and eval_lambda_call ?tc:(tail_call=false) call ss loc =
539590
| ResolvedIdent 14 -> map_keys_builtin ((eval_expr call.call_args ss) state) ss loc
540591
| ResolvedIdent 15 -> map_to_list_builtin ((eval_expr call.call_args ss) state) ss loc
541592
| ResolvedIdent 16 -> typeof_builtin ((eval_expr call.call_args ss) state) ss loc
593+
| ResolvedIdent 17 ->
594+
Lwt_main.run (serve_builtin ((eval_expr call.call_args ss) state) ss loc);
595+
Tuple [], state
596+
| ResolvedIdent 18 ->
597+
Lwt_main.run (serve_ssl_builtin ((eval_expr call.call_args ss) state) ss loc);
598+
Tuple [], state
542599
| UnresolvedIdent s ->
543600
printf "Error: unresolved function %s not found at %s\n" s (location_to_string loc);
544601
print_traceback ss;

lib/run.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ let base_static_idents () =
127127
"map_keys__builtin";
128128
"map_to_list__builtin";
129129
"typeof__builtin";
130+
"serve__builtin";
131+
"serve_ssl__builtin";
130132
] in
131133
List.zip_exn builtin_idents (List.range 0 (List.length builtin_idents))
132134

lib/stdlib.rsc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,5 @@ let list_dir(dir) = list_dir__builtin(dir)
239239
let map_keys(m) = map_keys__builtin(m)
240240
let map_to_list(m) = map_to_list__builtin(m)
241241
let typeof(m) = typeof__builtin(m)
242+
let start_server(port, callback) = serve__builtin(port, callback)
243+
let start_server_ssl(cert_path, key_path, port, callback) = serve_ssl__builtin(cert_path, key_path, port, callback)

0 commit comments

Comments
 (0)