text stringlengths 0 601k |
|---|
let choose_routers_without_path_builds_paths _ ( ) = let router = Sihl . Web . ( choose [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " bar " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " ... |
let choose_routers_with_empty_scope_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " " : [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " " / ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . ... |
let choose_routers_with_slash_scope_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " " :/ [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " bar " ( fun _ -> Lwt . return @@ Opium . Response... |
let choose_routers_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " :/ root " [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " " / ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_te... |
let choose_nested_routers_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " :/ root " [ choose ~ scope " : sub " [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " fooz " ( fun _ -> Lwt . ret... |
let externalize_link _ ( ) = let actual = Sihl . Web . externalize_path ~ prefix " : prefix " " foo / bar " in Alcotest . ( check string " prefixes path " " prefix / foo / bar " actual ) ; let actual = Sihl . Web . externalize_path ~ prefix " : prefix " " foo / bar " / in Alcotest . ( check string " preserve trailing "... |
let find_bearer_token _ ( ) = let token_value = " tokenvalue123 " in let token_header = Format . sprintf " Bearer % s " token_value in let req = Opium . Request . get " / some / path / login " |> Opium . Request . add_header ( " authorization " , token_header ) in let handler req = let token = Sihl . Web . Request . be... |
let find_bearer_token_with_space _ ( ) = let token_value = " tokenvalue123 and after space " in let token_header = Format . sprintf " Bearer % s " token_value in let req = Opium . Request . get " / some / path / login " |> Opium . Request . add_header ( " authorization " , token_header ) in let handler req = let token ... |
let suite = [ ( " router " , [ test_case " choose routers without path builds paths " ` Quick choose_routers_without_path_builds_paths ; test_case " choose routers with empty scope builds paths " ` Quick choose_routers_with_empty_scope_builds_paths ; test_case " choose routers with slash scope builds paths " ` Quick ch... |
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " web " suite ) ; ; |
type context = { arguments : ( string * string ) list ; connection : Cohttp_lwt_unix . Server . conn ; request : Cohttp . Request . t ; body : Cohttp_lwt . Body . t } |
let headers = let h = Cohttp . Header . init_with " Access - Control - Allow - Origin " " " * in let h = Cohttp . Header . add h " content - type " " application / json " in h |
let string_response ( ? headers = headers ) = Server . respond_string ~ headers |
let error_response ( ? headers = headers ) ( ? status = ` Internal_server_error ) ( errors : Result_util . message list ) : ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t = let error_msg : string = JsonUtil . string_of_write ( JsonUtil . write_list Result_util . write_message ) errors in let ( ) = Lwt . asyn... |
let api_result_response ( ~ string_of_success : ' ok -> string ) : ' ok Api . result -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t = function | { Result_util . value = Result . Ok ok ; Result_util . status ; _ } -> let body : string = string_of_success ok in let status :> Cohttp . Code . status_code = st... |
let kasa_response ~ string_of_success x = api_result_response ~ string_of_success ( Api_common . result_kasa x ) |
let method_not_allowed_respond meths = let headers = Cohttp . Header . add_multi ( Cohttp . Header . init ( ) ) " Allow " ( List . map Cohttp . Code . string_of_method meths ) in Server . respond ~ headers ~ status ` : Method_not_allowed ~ body : Cohttp_lwt . Body . empty ( ) |
let options_respond methods = let meths_str = List . map Cohttp . Code . string_of_method methods in let headers = Cohttp . Header . init_with " Access - Control - Allow - Origin " " " * in let headers = Cohttp . Header . add headers " Access - Control - Allow - Headers " " Content - Type " in let headers = Cohttp . He... |
type ' a route = { path : string ; operation : ' a } |
type route_handler = ( context : context -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) route |
type route_filter = ( context : context -> chain ( : context : context -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) route |
type url_matcher = { re : Re . re ; labels : string list ; route : string } |
let label_prepattern = Re . rep1 ( Re . alt [ Re . alnum ; Re . char ' _ ' ] ) |
let variable_pattern = Re . compile ( Re . seq [ Re . char ' { ' ; label_prepattern ; Re . char ' } ' ] ) |
let label_pattern = Re . compile label_prepattern |
let create_url_matcher ( url : string ) : url_matcher = let labels = Re . matches variable_pattern url in let labels = List . flatten ( List . map ( Re . matches label_pattern ) labels ) in let pattern = Re . split_full variable_pattern url |> List . map ( function | ` Text s -> Re . str s | ` Delim _ -> Re . group ( R... |
let rec match_url ( url_matchers : ( ' a * url_matcher ) list ) ( url : string ) : ( ' a * ( string * string ) list ) list = match url_matchers with | ( arg , matcher ) :: tail -> ( try let matching = Re . exec matcher . re url |> Re . Group . all |> Array . to_list |> List . tl in let get_parameters : ( string * strin... |
let request_handler context = function | [ ] -> Server . respond_not_found ~ uri ( : Request . uri context . request ) ( ) | [ route , arguments ] -> Lwt . catch ( fun ( ) -> let context = { context with arguments = arguments } in route . operation ~ context ) ( fun exn -> api_result_response ~ string_of_success ( : fu... |
let route_handler ( routes : route_handler list ) : context : context -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t = let url_matchers : ( route_handler * url_matcher ) list = List . map ( fun route -> ( route , create_url_matcher route . path ) ) routes in fun ~ context -> let url : string = Uri . pct_d... |
let inputString ( m : input ) : Pla . t = match m with | IContext -> Pla . string " processor . context " | IReal name | IInt name | IBool name -> Pla . string name |
let inputName ( i , acc ) s = match s with | IContext -> i , Pla . string " processor . context " :: acc | _ -> i + 1 , [ % pla { | in_ <# i # i [ > n ] } ] | :: acc |
let performFunctionCall module_name ( config : config ) = let args = List . fold_left inputName ( 0 , [ ] ) config . process_inputs |> snd |> List . rev |> Pla . join_sep Pla . comma in let copy = match config . process_outputs with | [ ] -> Pla . unit | [ _ ] -> let value = Pla . string " ret " in [ % pla { | out_0 [ ... |
let noteFunctions ( params : params ) = let module_name = params . module_name in let on_args = Pla . map_sep Pla . comma inputString params . config . noteon_inputs in let off_args = Pla . map_sep Pla . comma inputString params . config . noteoff_inputs in ( [ % pla { | node . noteOn = function ( note , velocity , cha... |
let controlChangeFunction ( params : params ) = let module_name = params . module_name in let ctrl_args = Pla . map_sep Pla . comma inputString params . config . controlchange_inputs in [ % pla { | node . controlChange = function ( control , value , channel ) { processor . <# module_name # s > _controlChange ( <# ctrl_... |
let rec removeContext inputs = match inputs with | IContext :: t -> removeContext t | _ -> inputs |
let get ( params : params ) runtime code : ( Pla . t * FileKind . t ) list = let config = params . config in let module_name = params . module_name in let inputs = removeContext config . process_inputs in let nprocess_inputs = List . length inputs in let nprocess_outputs = List . length config . process_outputs in let ... |
let oslo_polygon = { { | " type " : " Feature " , " geometry " : { " type " : " Polygon " , " coordinates " : [ [ [ 10 . 489165172838884 , 60 . 017259872374645 ] , [ 10 . 580764868996987 , 60 . 0762384207017 ] , [ 10 . 592122568549627 , 60 . 09394183519897 ] , [ 10 . 572782530207661 , 60 . 11678480264957 ] , [ 10 . 600... |
type objectType object method alpha : bool t prop method depth : bool t prop method stencil : bool t prop method antialias : bool t prop method premultipliedAlpha : bool t prop method preserveDrawingBuffer : bool t prop method preferLowPowerToHighPerformance : bool t prop method failIfMajorPerformanceCaveat : bool t pr... |
let defaultContextAttributes = Js . Unsafe . ( obj [ | " alpha " , inject _true ; " depth " , inject _true ; " stencil " , inject _false ; " antialias " , inject _true ; " premultipliedAlpha " , inject _false ; " preserveDrawingBuffer " , inject _false ; " preferLowPowerToHighPerformance " , inject _false ; " failIfMaj... |
type ' a uniformLocation object method size : int readonly_prop method _type : uniformType readonly_prop method name : js_string t readonly_prop end object method rangeMin : int readonly_prop method rangeMax : int readonly_prop method precision : int readonly_prop end object method canvas : Dom_html . canvasElement t r... |
module Event = struct let webglcontextlost = Dom_html . Event . make " webglcontextlost " let webglcontextrestored = Dom_html . Event . make " webglcontextrestored " let webglcontextcreationerror = Dom_html . Event . make " webglcontextcreationerror " end object method getContext : js_string t -> renderingContext t opt... |
let getContext ( c : Dom_html . canvasElement t ) = let c : canvasElement t = Js . Unsafe . coerce c in let ctx = c ## getContext ( Js . string " webgl " ) in if Opt . test ctx then ctx else c ## ( getContext ( Js . string " experimental - webgl " ) ) |
let getContextWithAttributes ( c : Dom_html . canvasElement t ) attribs = let c : canvasElement t = Js . Unsafe . coerce c in let ctx = c ## getContext_ ( Js . string " webgl " ) attribs in if Opt . test ctx then ctx else c ## getContext_ ( Js . string " experimental - webgl " ) attribs |
let error f = Printf . ksprintf ( fun s -> Firebug . console ## error ( Js . string s ) ; failwith s ) f |
let debug f = Printf . ksprintf ( fun s -> Firebug . console ## log ( Js . string s ) ) f |
let alert f = Printf . ksprintf ( fun s -> Dom_html . window ## alert ( Js . string s ) ; failwith s ) f |
let check_error gl = if gl ## getError <> gl . ## _NO_ERROR_ then error " WebGL error " |
let init_canvas canvas_id = let canvas = Opt . get ( Opt . bind ( Dom_html . document ## getElementById ( string canvas_id ) ) Dom_html . CoerceTo . canvas ) ( fun ( ) -> error " can ' t find canvas element % s " canvas_id ) in let gl = Opt . get ( try WebGL . getContext canvas with _ -> null ) ( fun ( ) -> alert " can... |
let load_shader ( gl : WebGL . renderingContext t ) shader text = gl ## shaderSource shader text ; gl ## compileShader shader ; if not ( to_bool ( gl ## getShaderParameter shader gl . ## _COMPILE_STATUS_ ) ) then error " An error occurred compiling the shaders : \ n % s \ n % s " ( to_string text ) ( to_string ( gl ## ... |
let create_program ( gl : WebGL . renderingContext t ) vert_src frag_src = let vertexShader = gl ## createShader gl . ## _VERTEX_SHADER_ in let fragmentShader = gl ## createShader gl . ## _FRAGMENT_SHADER_ in load_shader gl vertexShader vert_src ; load_shader gl fragmentShader frag_src ; let prog = gl ## createProgram ... |
let get_source src_id = let script = Opt . get ( Opt . bind ( Dom_html . document ## getElementById ( string src_id ) ) Dom_html . CoerceTo . script ) ( fun ( ) -> error " can ' t find script element % s " src_id ) in script . ## text |
let float32array a = let array = new % js Typed_array . float32Array ( Array . length a ) in Array . iteri ( fun i v -> Typed_array . set array i v ) a ; array |
module Proj3D = struct type t = float array let scale x y z : t = [ | x ; 0 . ; 0 . ; 0 . ; 0 . ; y ; 0 . ; 0 . ; 0 . ; 0 . ; z ; 0 . ; 0 . ; 0 . ; 0 . ; 1 . ] | let translate x y z : t = [ | 1 . ; 0 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; 0 . ; 0 . ; 0 . ; 1 . ; 0 . ; x ; y ; z ; 1 . ] | let rotate_x t : t = [ | 1 . ; 0 . ;... |
type line = | V of ( float * float * float ) | VN of ( float * float * float ) | F of ( ( int * int ) * ( int * int ) * ( int * int ) ) |
let line_regexp = Regexp . regexp " ( v | vn | f ) \\ ( [ ^\\ ] ) +\\ ( [ ^\\ ] ) +\\ ( [ ^\\ ] ) " + |
let couple_regexp = Regexp . regexp " ( [ 0 - 9 ] ) ( [ +// 0 - 9 ] ) " + |
let read_coord_couple c = match Regexp . string_match couple_regexp c 0 with | None -> None | Some res -> ( match List . map ( Regexp . matched_group res ) [ 1 ; 2 ] with | [ Some v ; Some vn ] -> Some ( int_of_string v , int_of_string vn ) | _ -> None ) |
let read_line l = match Regexp . string_match line_regexp l 0 with | None -> None | Some res -> ( match List . map ( Regexp . matched_group res ) [ 1 ; 2 ; 3 ; 4 ] with | [ Some " v " ; Some x ; Some y ; Some z ] -> Some ( V ( float_of_string x , float_of_string y , float_of_string z ) ) | [ Some " vn " ; Some x ; Some... |
let concat a = let length = Array . fold_left ( fun len l -> len + List . length l ) 0 a in let next = let pos = ref ( - 1 ) in let l = ref [ ] in let rec aux _ = match ! l with | t :: q -> l := q ; t | [ ] -> incr pos ; l := a . ( ! pos ) ; aux 0 in aux in Array . init length next |
let make_model vertex norm face = let vertex ' = Array . init ( Array . length face ) ( fun i -> let ( av , _an ) , ( bv , _bn ) , ( cv , _cn ) = face . ( i ) in let a1 , a2 , a3 = vertex . ( av - 1 ) in let b1 , b2 , b3 = vertex . ( bv - 1 ) in let c1 , c2 , c3 = vertex . ( cv - 1 ) in [ a1 ; a2 ; a3 ; b1 ; b2 ; b3 ; ... |
let read_model a = let vertex = ref [ ] in let norm = ref [ ] in let face = ref [ ] in Array . iter ( fun s -> match read_line s with | None -> ( ) | Some ( F ( a , b , c ) ) -> face := ( a , b , c ) :: ! face | Some ( V ( a , b , c ) ) -> vertex := ( a , b , c ) :: ! vertex | Some ( VN ( a , b , c ) ) -> norm := ( a ,... |
let http_get url = XmlHttpRequest . get url >>= fun r -> let cod = r . XmlHttpRequest . code in let msg = r . XmlHttpRequest . content in if cod = 0 || cod = 200 then Lwt . return msg else fst ( Lwt . wait ( ) ) |
let getfile f = try Lwt . return ( Sys_js . read_file ~ name : f ) with Not_found -> http_get f >|= fun s -> s |
let fetch_model s = getfile s >|= fun s -> let a = Regexp . split ( Regexp . regexp " \ n " ) s in read_model ( Array . of_list a ) |
let pi = 4 . . * atan 1 . |
let start ( pos , norm ) = let fps_text = Dom_html . document ## createTextNode ( Js . string " loading " ) in Opt . iter ( Opt . bind ( Dom_html . document ## getElementById ( string " fps " ) ) Dom_html . CoerceTo . element ) ( fun span -> Dom . appendChild span fps_text ) ; debug " init canvas " ; let _canvas , gl =... |
let go _ = ignore ( debug " fetching model " ; catch ( fun ( ) -> fetch_model " monkey . model " >>= start ) ( fun exn -> error " uncaught exception : % s " ( Printexc . to_string exn ) ) ) ; _true |
let _ = Dom_html . window . ## onload := Dom_html . handler go |
type event_kind = | New_thread | Thread_switch | Cycle_start | Cycle_end | Pid_is | Event | Measure_start | Measure_end | Trace_end |
type event = { name : string ; categories : string list ; phase : event_kind ; timestamp : int ; pid : int ; tid : int } |
type events = event list |
let create_event ( ? categories = [ ] ) ( ? pid = 0 ) 0 ( ? tid = 0 ) 0 ~ phase ~ timestamp name = { name ; categories ; phase ; timestamp ; pid ; tid } |
module Output = struct module JSON = struct let phase_of_kind = function | New_thread | Pid_is -> ` String " M " | Cycle_start -> ` String " b " | Cycle_end -> ` String " e " | Thread_switch -> ` String " X " | Event -> ` String " i " | Measure_start -> ` String " B " | Measure_end -> ` String " E " | Trace_end -> ` St... |
let emitk ~ buf ( k : event_kind ) event_kind pos = let num = match k with | New_thread -> 0 | Thread_switch -> 1 | Cycle_start -> 2 | Cycle_end -> 3 | Pid_is -> 4 | Event -> 5 | Measure_start -> 6 | Measure_end -> 7 | Trace_end -> 8 in Bigstring . set_uint8_exn buf ~ pos num ; pos + 1 |
let emiti ~ buf ( i : int ) int pos = Bigstring . set_uint64_le_exn buf ~ pos i ; pos + 8 |
let emits ~ buf ( s : string ) string ( pos : int ) int = let sl = String . length s in let pos = emiti ~ buf sl pos in Bigstring . From_string . blit ~ src : s ~ src_pos : 0 ~ len : sl ~ dst : buf ~ dst_pos : pos ; pos + sl |
let finish wr ~ buf final_len = Writer . write_bigstring wr ~ pos : 0 ~ len : final_len buf |
let emit_event wr ~ buf ( event : event ) event = match event . phase with | New_thread -> emitk ~ buf New_thread 0 |> emiti ~ buf event . timestamp |> emiti ~ buf event . tid |> emits ~ buf event . name |> finish ~ buf wr | Thread_switch -> emitk ~ buf Thread_switch 0 |> emiti ~ buf event . timestamp |> emiti ~ buf ev... |
module type IO = sig type ' + a t val ( ) >>= : ' a t -> ( ' a -> ' b t ) -> ' b t val return : ' a -> ' a t end |
module type S = sig type ' + a io type ' a result = | Ok of ' a | Error of int type ( ' a , ' body ) op = ' body Rd . t -> ( ' a result * ' body Rd . t ) io type ' body provider = ( ' body , ' body ) op type ' body acceptor = ( bool , ' body ) op type www_authenticate = { scheme : string ; realm : string ; params : ( s... |
let default_variances = [ " Accept " ; " Accept - Encoding " ; " Accept - Charset " ; " Accept - Language " ] |
module type CLOCK = sig val now : unit -> int end |
module Make ( IO : IO ) ( Clock : CLOCK ) = struct type ' + a io = ' a IO . t open IO type ' a result = | Ok of ' a | Error of int type ( ' a , ' body ) op = ' body Rd . t -> ( ' a result * ' body Rd . t ) io type ' body provider = ( ' body , ' body ) op type ' body acceptor = ( bool , ' body ) op type www_authenticate... |
module Session = struct module Backend = struct include Session . Lift . IO ( Deferred ) ( Session . Memory ) let create ( ) = Session . Memory . create ( ) end include Session_webmachine . Make ( Deferred ) ( Backend ) end |
module UnixClock = struct let now ( ) = int_of_float ( Unix . gettimeofday ( ) ) end inherit [ Body . t ] resource inherit [ Body . t ] Session . manager ~ cookie_key backend method private increment session rd = let value = string_of_int ( 1 + int_of_string session . Session . value ) in self # session_set value rd me... |
let main ( ) = let port = 8080 in let mem = Session . Backend . create ( ) in let routes = [ " " , /* fun ( ) -> new counter mem ] in let dispatch = dispatch ' routes in let handler ~ body _ request = dispatch ~ body ~ request >>| begin function | None -> ( ` Not_found , Cohttp . Header . init ( ) , ` String " Not foun... |
let _ = Mirage_crypto_rng_unix . initialize ( ) ; Scheduler . go_main ~ main ( ) |
module Session = struct module Backend = struct include Session . Lift . IO ( Lwt ) ( Session . Memory ) let create ( ) = Session . Memory . create ( ) end include Session_webmachine . Make ( Lwt ) ( Backend ) end |
module UnixClock = struct let now ( ) = int_of_float ( Unix . gettimeofday ( ) ) end inherit [ Cohttp_lwt . Body . t ] resource inherit [ Cohttp_lwt . Body . t ] Session . manager ~ cookie_key backend method private increment session rd = let value = string_of_int ( 1 + int_of_string session . Session . value ) in self... |
let main ( ) = let port = 8080 in let mem = Session . Backend . create ( ) in let routes = [ " " , /* fun ( ) -> new counter mem ] in let dispatch = dispatch ' routes in let callback _conn request body = dispatch ~ body ~ request >|= begin function | None -> ( ` Not_found , Cohttp . Header . init ( ) , ` String " Not f... |
let ( ) = Mirage_crypto_rng_unix . initialize ( ) ; Lwt_main . run ( main ( ) ) |
let logger ( handler : Cohttp_lwt_unix . Server . conn -> Cohttp . Request . t -> Cohttp_lwt . Body . t -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) ( conn : Cohttp_lwt_unix . Server . conn ) ( request : Cohttp . Request . t ) ( body : Cohttp_lwt . Body . t ) : ( Cohttp . Response . t * Cohttp_lwt . B... |
let server = let app_args = App_args . default in let common_args = Common_args . default in let websim_args = Websim_args . default in let options = App_args . options app_args @ Websim_args . options websim_args @ Common_args . options common_args in let usage_msg : string = " kappa webservice " in let ( ) = Arg . pa... |
let ( ) = let ( ) = Lwt . async_exception_hook := ignore in ignore ( Lwt_main . run server ) |
let og_image html = let open Soup in let soup = parse html in try soup $ " meta [ metaproperty = og : image ] image " |> R . attribute " content " |> Option . some with Failure _ -> None |
let image_src html = let open Soup in let soup = parse html in try soup $ " link [ linkrel " =\ image_src ] " " \ |> R . attribute " href " |> Option . some with Failure _ -> None |
let twitter_image html = let open Soup in let soup = parse html in try soup $ " meta [ metaname " =\ twitter : image ] " " \ |> R . attribute " content " |> Option . some with Failure _ -> None |
let og_description html = let open Soup in let soup = parse html in try soup $ " meta [ metaproperty = og : description ] description " |> R . attribute " content " |> Option . some with Failure _ -> None |
let description html = let open Soup in let soup = parse html in try soup $ " meta [ metaproperty = description ] description " |> R . attribute " content " |> Option . some with Failure _ -> None |
let preview_image html = let preview_image = match og_image html with | None -> ( match image_src html with | None -> twitter_image html | Some x -> Some x ) x | Some x -> Some x in match Option . map String . trim preview_image with | Some " " -> None | Some x -> Some x | None -> None |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.