Skip to content

Commit

Permalink
Change api endpoint
Browse files Browse the repository at this point in the history
To allow the default endpoint to return HTML for users
  • Loading branch information
xrchz committed Nov 9, 2017
1 parent b45fa33 commit 415de8b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
TODO: Change (text) API endpoint to regression.cgi/api/

TODO: Write HTML interface for status output
Thus regression.cgi/{,running,waiting,stopped,etc.} will return a nicely
presented webpage (with links) for viewing the current status of the queues.
Expand Down
17 changes: 14 additions & 3 deletions api.sml
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,32 @@ fun refresh () =
else ignore (List.foldl (add_waiting avoid_ids) 1 snapshots)
in () end

datatype request_api = Get of api | Post of id * string
datatype request =
Get of api
| Post of id * string
| Html of string * string option

fun get_api () =
case (OS.Process.getEnv "PATH_INFO",
OS.Process.getEnv "REQUEST_METHOD") of
(SOME path_info, SOME "GET")
=> Option.map Get (api_from_string path_info (OS.Process.getEnv "QUERY_STRING"))
=>
if String.isPrefix "/api" path_info then
Option.map Get
(api_from_string
(String.extract(path_info,4,NONE))
(OS.Process.getEnv "QUERY_STRING"))
else SOME (Html (path_info, OS.Process.getEnv "QUERY_STRING"))
| (SOME path_info, SOME "POST")
=> (case String.tokens (equal #"/") path_info of
["log",n] =>
["api","log",n] =>
(Option.mapPartial
(fn len =>
Option.compose
((fn id => Post(id,TextIO.inputN(TextIO.stdIn,len))),
id_from_string) n)
(Option.composePartial(Int.fromString,OS.Process.getEnv) "CONTENT_LENGTH"))
| ["api","refresh"] => SOME (Get Refresh) (* GitHub webhook requests this with POST *)
| _ => NONE)
| _ => NONE

Expand Down Expand Up @@ -184,6 +194,7 @@ fun dispatch_log id data =

fun dispatch_req (Get api) = dispatch api
| dispatch_req (Post (id,data)) = dispatch_log id data
| dispatch_req (Html (path,query)) = html_response "HTML interface coming soon..."

fun main () =
let
Expand Down
21 changes: 21 additions & 0 deletions serverLib.sml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ fun cgi_die ls =

fun cgi_assert b ls = if b then () else cgi_die ls

val html_response_header = "Content-Type:text/html\n\n<!doctype html>"

structure HTML = struct
fun element tag attrs body =
String.concat["<",tag," ",String.concatWith" "attrs,">",body,"</",tag,">"]
fun elt tag body = element tag [] body
val html = element "html" ["lang='en'"]
val head = elt "head"
val charset = "<meta charset='utf-8'>"
val header = head charset
val body = elt "body"
val p = elt "p"
end

fun html_response s =
let
open HTML
val () = TextIO.output(TextIO.stdOut, html_response_header)
val () = TextIO.output(TextIO.stdOut, html (String.concat[header,body (p s)]))
in () end

local
open Posix.IO Posix.FileSys
val flock = FLock.flock {ltype=F_WRLCK, whence=SEEK_SET, start=0, len=0, pid=NONE}
Expand Down
2 changes: 1 addition & 1 deletion worker.sml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ val system_capture_append = system_capture_with " &>>"
val poll_delay = Time.fromSeconds(60 * 30)

structure API = struct
val endpoint = "https://cakeml.org/regression.cgi"
val endpoint = "https://cakeml.org/regression.cgi/api"
fun curl_cmd api = (curl_path,
["--silent","--show-error"] @ api_curl_args api @ [String.concat[endpoint,api_to_string api]])
val send = system_output o curl_cmd
Expand Down

0 comments on commit 415de8b

Please sign in to comment.