-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48e5fea
commit 9ac797a
Showing
4 changed files
with
129 additions
and
0 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,83 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/risor-io/risor" | ||
"github.com/risor-io/risor/modules/all" | ||
"github.com/risor-io/risor/parser" | ||
) | ||
|
||
type Request struct { | ||
Content string `json:"content"` | ||
} | ||
|
||
type Response struct { | ||
Result string `json:"result"` | ||
Time float64 `json:"time"` | ||
} | ||
|
||
func main() { | ||
var port string | ||
flag.StringVar(&port, "port", "3000", "Define port for the server to listen on") | ||
flag.Parse() | ||
|
||
r := chi.NewRouter() | ||
r.Use(middleware.Logger) | ||
r.Post("/execute", func(w http.ResponseWriter, r *http.Request) { | ||
executeHandler(w, r) | ||
}) | ||
|
||
log.Println("Server started on http://localhost:" + port) | ||
log.Fatal(http.ListenAndServe(":" + port, r)) | ||
} | ||
|
||
func executeHandler(w http.ResponseWriter, r *http.Request) { | ||
var req Request | ||
var res Response | ||
json.NewDecoder(r.Body).Decode(&req) | ||
|
||
input := req.Content | ||
if input == "" { | ||
http.Error(w, "Please provide a code snippet", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
ctx := context.Background() | ||
start := time.Now() | ||
|
||
result, err := risor.Eval(ctx, string(input), | ||
risor.WithBuiltins(all.Builtins()), | ||
risor.WithDefaultBuiltins(), | ||
risor.WithDefaultModules()) | ||
if err != nil { | ||
parserErr, ok := err.(parser.ParserError) | ||
if ok { | ||
http.Error(w, parserErr.FriendlyMessage(), http.StatusInternalServerError) | ||
} else { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
res.Result = result.Inspect() | ||
|
||
res.Time = time.Since(start).Seconds() | ||
|
||
response, err := json.Marshal(res) | ||
if err != nil { | ||
http.Error(w, "Internal Server Error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusCreated) | ||
w.Write(response) | ||
} |
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 main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestExecuteHandler(t *testing.T) { | ||
payload := Request{ | ||
Content: "['welcome', 'to', 'risor', '👋'] | strings.join(' ')", | ||
} | ||
payloadBytes, _ := json.Marshal(payload) | ||
|
||
req, err := http.NewRequest("POST", "/execute", bytes.NewBuffer(payloadBytes)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
|
||
executeHandler(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusCreated { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusCreated) | ||
} | ||
|
||
var response Response | ||
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expectedResult := "\"welcome to risor 👋\"" | ||
if response.Result != expectedResult { | ||
t.Errorf("handler returned unexpected result: got %s want %s", response.Result, expectedResult) | ||
} | ||
|
||
if response.Time <= 0 { | ||
t.Errorf("handler returned invalid response time: %f", response.Time) | ||
} | ||
} |
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