|
9 | 9 | "github.com/TRC-Loop/ccolon/vm" |
10 | 10 | ) |
11 | 11 |
|
12 | | -func NewHttpModule() *vm.ModuleValue { |
| 12 | +func NewHttpModule(machine *vm.VM) *vm.ModuleValue { |
13 | 13 | return &vm.ModuleValue{ |
14 | 14 | Name: "http", |
15 | 15 | Methods: map[string]*vm.NativeFuncValue{ |
@@ -43,12 +43,89 @@ func NewHttpModule() *vm.ModuleValue { |
43 | 43 | if !ok { |
44 | 44 | return nil, fmt.Errorf("http.listen() handler must be a function") |
45 | 45 | } |
46 | | - _ = handler // handler will be called via VM callback |
| 46 | + |
47 | 47 | addr := fmt.Sprintf(":%d", port.Val) |
48 | 48 | fmt.Printf("CColon HTTP server listening on %s\n", addr) |
49 | | - // We store the handler ref but can't call it without VM access. |
50 | | - // The server integration is done through the VM's CallFunc. |
51 | | - return nil, fmt.Errorf("http.listen() requires VM callback support (use the listen helper)") |
| 49 | + |
| 50 | + mux := http.NewServeMux() |
| 51 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 52 | + body, _ := io.ReadAll(r.Body) |
| 53 | + r.Body.Close() |
| 54 | + |
| 55 | + // Build request dict |
| 56 | + headerDict := &vm.DictValue{ |
| 57 | + Entries: make(map[string]vm.Value), |
| 58 | + Order: []string{}, |
| 59 | + } |
| 60 | + for key := range r.Header { |
| 61 | + lk := strings.ToLower(key) |
| 62 | + headerDict.Entries[lk] = &vm.StringValue{Val: r.Header.Get(key)} |
| 63 | + headerDict.Order = append(headerDict.Order, lk) |
| 64 | + } |
| 65 | + |
| 66 | + reqDict := &vm.DictValue{ |
| 67 | + Entries: map[string]vm.Value{ |
| 68 | + "method": &vm.StringValue{Val: r.Method}, |
| 69 | + "path": &vm.StringValue{Val: r.URL.Path}, |
| 70 | + "body": &vm.StringValue{Val: string(body)}, |
| 71 | + "headers": headerDict, |
| 72 | + "query": &vm.StringValue{Val: r.URL.RawQuery}, |
| 73 | + }, |
| 74 | + Order: []string{"method", "path", "body", "headers", "query"}, |
| 75 | + } |
| 76 | + |
| 77 | + result, err := machine.CallFunc(handler, []vm.Value{reqDict}) |
| 78 | + if err != nil { |
| 79 | + http.Error(w, "handler error: "+err.Error(), 500) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // Handle response |
| 84 | + switch resp := result.(type) { |
| 85 | + case *vm.StringValue: |
| 86 | + w.Header().Set("Content-Type", "text/plain") |
| 87 | + w.WriteHeader(200) |
| 88 | + w.Write([]byte(resp.Val)) |
| 89 | + case *vm.DictValue: |
| 90 | + // Expect {status: int, body: string, headers: dict} |
| 91 | + status := 200 |
| 92 | + respBody := "" |
| 93 | + if s, ok := resp.Entries["status"]; ok { |
| 94 | + if iv, ok := s.(*vm.IntValue); ok { |
| 95 | + status = int(iv.Val) |
| 96 | + } |
| 97 | + } |
| 98 | + if b, ok := resp.Entries["body"]; ok { |
| 99 | + if sv, ok := b.(*vm.StringValue); ok { |
| 100 | + respBody = sv.Val |
| 101 | + } |
| 102 | + } |
| 103 | + if h, ok := resp.Entries["headers"]; ok { |
| 104 | + if hd, ok := h.(*vm.DictValue); ok { |
| 105 | + for key, val := range hd.Entries { |
| 106 | + if sv, ok := val.(*vm.StringValue); ok { |
| 107 | + w.Header().Set(key, sv.Val) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + w.WriteHeader(status) |
| 113 | + w.Write([]byte(respBody)) |
| 114 | + case *vm.NilValue: |
| 115 | + w.WriteHeader(204) |
| 116 | + default: |
| 117 | + w.Header().Set("Content-Type", "text/plain") |
| 118 | + w.WriteHeader(200) |
| 119 | + w.Write([]byte(result.String())) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + // This blocks, so it won't return until the server stops |
| 124 | + err := http.ListenAndServe(addr, mux) |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("http.listen() failed: %s", err) |
| 127 | + } |
| 128 | + return &vm.NilValue{}, nil |
52 | 129 | }, |
53 | 130 | }, |
54 | 131 | }, |
|
0 commit comments