add ability to handle other requets than GET

This commit is contained in:
zongor 2024-07-12 22:37:22 -04:00
parent 81b765a1b2
commit 6dbe6ec436
2 changed files with 18 additions and 8 deletions

View File

@ -2,6 +2,10 @@
(* get the request info *)
argv shatter
(* if its a get request we want to ignore the request body *)
"GET" streq?
{ pop } ifyes
(* check to see if we have the root route, if so return index.html *)
dup "/" streq?
choose

View File

@ -1068,19 +1068,25 @@ func Interpret(code Expr, argv []string, w io.Writer) error {
if e != nil {
return e
}
if fun.Tok != FUNCTION {
return fmt.Errorf("cannot `servehttp` exptected a function to serve @%v", idx)
}
http.ListenAndServe(str.Value.(string), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := make([]string, 0)
req = append(req, r.RequestURI)
if fun.Tok == FUNCTION {
zerr := Interpret(fun, req, w)
if zerr != nil {
fmt.Println(zerr)
}
} else {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
return
}
req = append(req, r.RequestURI)
req = append(req, string(b))
req = append(req, r.Method)
err = Interpret(fun, req, w)
if err != nil {
fmt.Println(err)
}
}))
case READALL:
str, e := pop()