Skip to content

Commit

Permalink
joker.http/start-file-server
Browse files Browse the repository at this point in the history
  • Loading branch information
candid82 committed Mar 28, 2018
1 parent 4eb0974 commit 856c864
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions std/http.joke
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@
{:added "1.0"
:go "startServer(addr, handler)"}
[^String addr ^Callable handler])

(defn start-file-server
"Starts HTTP server on the TCP network address addr that
serves HTTP requests with the contents of the file system rooted at root."
{:added "1.0"
:go "startFileServer(addr, root)"}
[^String addr ^String root])
22 changes: 22 additions & 0 deletions std/http/a_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ var send_ Proc = func(args []Object) Object {
return NIL
}

var start_file_server_ Proc = func(args []Object) Object {
c := len(args)
switch {
case c == 2:

addr := ExtractString(args, 0)
root := ExtractString(args, 1)
res := startFileServer(addr, root)
return res

default:
PanicArity(c)
}
return NIL
}

var start_server_ Proc = func(args []Object) Object {
c := len(args)
switch {
Expand Down Expand Up @@ -62,6 +78,12 @@ httpNamespace.InternVar("send", send_,
- headers (map)
- content-length (int)`, "1.0"))

httpNamespace.InternVar("start-file-server", start_file_server_,
MakeMeta(
NewListFrom(NewVectorFrom(MakeSymbol("addr"), MakeSymbol("root"))),
`Starts HTTP server on the TCP network address addr that
serves HTTP requests with the contents of the file system rooted at root.`, "1.0"))

httpNamespace.InternVar("start-server", start_server_,
MakeMeta(
NewListFrom(NewVectorFrom(MakeSymbol("addr"), MakeSymbol("handler"))),
Expand Down
6 changes: 6 additions & 0 deletions std/http/http_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ func startServer(addr string, handler Callable) Object {
PanicOnErr(err)
return NIL
}

func startFileServer(addr string, root string) Object {
err := http.ListenAndServe(addr, http.FileServer(http.Dir(root)))
PanicOnErr(err)
return NIL
}

0 comments on commit 856c864

Please sign in to comment.