-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Collins <[email protected]>
- Loading branch information
Showing
10 changed files
with
230 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env sim | ||
openapi: 3.0.0 | ||
info: | ||
title: Proxy API | ||
version: 1.0.0 | ||
servers: | ||
- url: http://localhost:5050 | ||
paths: | ||
/proxy: | ||
get: | ||
x-sim-script: | | ||
hello = http({"url": "http://localhost:8080/hello"}) | ||
response = { | ||
"status": hello["status"], | ||
"headers": { | ||
"Proxy": "true" | ||
}, | ||
"body": hello.body | ||
} | ||
responses: | ||
'200': | ||
description: OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/kitproj/sim/internal/types" | ||
) | ||
|
||
func getBody(r types.Request) (*bytes.Buffer, error) { | ||
w := &bytes.Buffer{} | ||
var err error | ||
switch value := r.GetBody(); body := value.(type) { | ||
case nil: | ||
case string: | ||
_, err = w.Write([]byte(body)) | ||
case []byte: | ||
_, err = w.Write(body) | ||
default: | ||
err = json.NewEncoder(w).Encode(body) | ||
} | ||
return w, err | ||
} | ||
|
||
func readBody(r *http.Response) (any, error) { | ||
cty := r.Header.Get("Content-Type") | ||
switch cty { | ||
case "": | ||
return nil, nil | ||
case "application/json": | ||
out := map[string]any{} | ||
err := json.NewDecoder(r.Body).Decode(&out) | ||
return out, err | ||
default: | ||
out := &bytes.Buffer{} | ||
_, err := io.Copy(out, r.Body) | ||
return out.String(), err | ||
} | ||
} | ||
|
||
func writeBody(w io.Writer, value any) error { | ||
switch body := value.(type) { | ||
case nil: | ||
return nil | ||
case string: | ||
_, err := w.Write([]byte(body)) | ||
return err | ||
case []byte: | ||
_, err := w.Write(body) | ||
return err | ||
default: | ||
return json.NewEncoder(w).Encode(body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package internal | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
var console = map[string]any{ | ||
"log": func(args ...any) { | ||
log.Println(append([]any{"console:"}, args...)...) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/kitproj/sim/internal/types" | ||
) | ||
|
||
func httpService(r types.Request) map[string]any { | ||
w, err := getBody(r) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to make HTTP request body: %w", err)) | ||
} | ||
log.Printf("HTTP %s %s", r.GetMethod(), r.GetURL()) | ||
resp, err := http.DefaultClient.Do(&http.Request{ | ||
Method: r.GetMethod(), | ||
URL: r.GetURL(), | ||
Header: httpHeaders(r.GetHeaders()), | ||
Body: io.NopCloser(w), | ||
}) | ||
log.Printf("HTTP %s %s %d", r.GetMethod(), r.GetURL(), resp.StatusCode) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to make HTTP request: %w", err)) | ||
} | ||
body, err := readBody(resp) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to read HTTP response body: %w", err)) | ||
} | ||
return Response{ | ||
"status": resp.StatusCode, | ||
"headers": reverseHttpHeaders(resp.Header), | ||
"body": body, | ||
} | ||
} | ||
|
||
func httpHeaders(in map[string]string) http.Header { | ||
out := http.Header{} | ||
for k, v := range in { | ||
out.Set(k, v) | ||
} | ||
return out | ||
} | ||
|
||
func reverseHttpHeaders(in http.Header) map[string]string { | ||
out := map[string]string{} | ||
for k, v := range in { | ||
out[k] = v[0] | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package internal | ||
|
||
import "github.com/google/uuid" | ||
|
||
func randomUUID() string { | ||
random, err := uuid.NewRandom() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return random.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type Request map[string]any | ||
|
||
func (r Request) GetMethod() string { | ||
if v, ok := r["method"].(string); ok { | ||
return v | ||
} | ||
return "GET" | ||
} | ||
|
||
func (r Request) GetURL() *url.URL { | ||
v, ok := r["url"].(string) | ||
if !ok { | ||
panic(fmt.Errorf("url absent or not a string")) | ||
} | ||
parsed, err := url.Parse(v) | ||
if err != nil { | ||
panic(fmt.Errorf("invalid url %q: %w", v, err)) | ||
} | ||
return parsed | ||
} | ||
|
||
func (r Request) GetHeaders() map[string]string { | ||
out := map[string]string{} | ||
headers, ok := r["headers"].(map[string]any) | ||
if !ok { | ||
return nil | ||
} | ||
for k, v := range headers { | ||
out[k] = fmt.Sprint(v) | ||
} | ||
return out | ||
} | ||
|
||
func (r Request) GetBody() any { | ||
return r["body"] | ||
} |
Oops, something went wrong.