Skip to content

Commit

Permalink
HTTP servers and supporting Risor config updates (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
myzie authored Feb 23, 2024
1 parent 6bddcff commit 2d31268
Show file tree
Hide file tree
Showing 49 changed files with 1,692 additions and 717 deletions.
26 changes: 26 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
codecov:
require_ci_to_pass: true
coverage:
status:
project:
default:
target: auto
threshold: 1%
if_ci_failed: error
only_pulls: true
patch:
default:
target: auto
threshold: 1% # Allowed drop in coverage for the patch
if_ci_failed: error
only_pulls: true
ignore:
- "examples/**/*"
- "cmd/risor-api/*"
- "cmd/risor-docs/*"
- "cmd/risor-lsp/*"
- "cmd/risor-modgen/*"
- "vscode/**/*"
- "tests/**/*"
- "terraform/**/*"
- "research/**/*"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ terraform.tfstate.backup
.DS_Store
test/
coverage.out
unit-tests.xml
157 changes: 0 additions & 157 deletions cfg.go

This file was deleted.

4 changes: 3 additions & 1 deletion cmd/risor-api/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/risor-io/risor/cmd/risor-api

go 1.21
go 1.22

toolchain go1.22.0

replace github.com/risor-io/risor => ../..

Expand Down
4 changes: 3 additions & 1 deletion cmd/risor-lsp/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/risor-io/risor/cmd/risor-lsp

go 1.21
go 1.22

toolchain go1.22.0

replace github.com/risor-io/risor => ../..

Expand Down
2 changes: 1 addition & 1 deletion cmd/risor/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/risor-io/risor/cmd/risor

go 1.21
go 1.22

replace (
github.com/risor-io/risor => ../..
Expand Down
6 changes: 4 additions & 2 deletions cmd/risor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ var rootCmd = &cobra.Command{
}

// Build up a list of options to pass to the VM
var opts []risor.Option
opts := []risor.Option{
risor.WithConcurrency(),
risor.WithListenersAllowed(),
}
if viper.GetBool("no-default-globals") {
opts = append(opts, risor.WithoutDefaultGlobals())
} else {
Expand Down Expand Up @@ -273,7 +276,6 @@ var rootCmd = &cobra.Command{
if modulesDir := viper.GetString("modules"); modulesDir != "" {
opts = append(opts, risor.WithLocalImporter(modulesDir))
}
opts = append(opts, risor.WithConcurrency())

// Determine what code is to be executed. The code may be supplied
// via the --code option, a path supplied as an arg, or stdin.
Expand Down
4 changes: 3 additions & 1 deletion examples/go/struct/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/risor-io/risor/examples/go/struct

go 1.21
go 1.22

toolchain go1.22.0

replace github.com/risor-io/risor => ../../..

Expand Down
18 changes: 18 additions & 0 deletions examples/scripts/http_server.risor
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

http.handle("/", func(w, r) {
return "OK"
})

http.handle("/hello", func(w, r) {
return "Hello from Risor!"
})

http.handle("/goodbye", func(w, r) {
return "Goodbye from Risor!"
})

http.handle("/animals/{name}", func(w, r) {
return { animal: r.path_value("name") }
})

http.listen_and_serve(":8080")
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/risor-io/risor

go 1.21
go 1.22

require (
github.com/risor-io/risor/modules/gha v0.0.0-20240213105055-b1d3a53935e5
Expand Down
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.21
go 1.22

toolchain go1.22.0

use (
.
Expand Down
4 changes: 3 additions & 1 deletion modules/aws/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/risor-io/risor/modules/aws

go 1.21
go 1.22

toolchain go1.22.0

replace github.com/risor-io/risor => ../..

Expand Down
4 changes: 3 additions & 1 deletion modules/cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/risor-io/risor/modules/cli

go 1.21
go 1.22

toolchain go1.22.0

replace github.com/risor-io/risor => ../..

Expand Down
4 changes: 3 additions & 1 deletion modules/gha/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/risor-io/risor/modules/gha

go 1.21
go 1.22

toolchain go1.22.0

replace github.com/risor-io/risor => ../..

Expand Down
46 changes: 43 additions & 3 deletions modules/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,60 @@ func MethodCmd(method string) object.BuiltinFunction {
}
}

func Handle(ctx context.Context, args ...object.Object) object.Object {
numArgs := len(args)
if numArgs != 2 {
return object.NewArgsRangeError("http.handle", 2, 3, numArgs)
}
pattern, errObj := object.AsString(args[0])
if errObj != nil {
return errObj
}
callFn, ok := object.GetCloneCallFunc(ctx)
if !ok {
return object.Errorf("http.handle: no clone-call function found in context")
}
var handler http.Handler
switch fn := args[1].(type) {
case http.Handler:
handler = fn
case *object.Function:
handler = HandlerFunc(fn, callFn)
default:
return object.Errorf("type error: unsupported http handler type: %s", fn.Type())
}
http.Handle(pattern, handler)
return object.Nil
}

func Builtins() map[string]object.Object {
return map[string]object.Object{
"fetch": object.NewBuiltin("fetch", Fetch),
}
}

func Module() *object.Module {
return object.NewBuiltinsModule("http", map[string]object.Object{
type ModuleOpts struct {
ListenersAllowed bool
}

func Module(opts ...ModuleOpts) *object.Module {
var listenersAllowed bool
if len(opts) > 0 {
listenersAllowed = opts[0].ListenersAllowed
}
builtins := map[string]object.Object{
"delete": object.NewBuiltin("http.delete", MethodCmd(http.MethodDelete)),
"get": object.NewBuiltin("http.get", MethodCmd(http.MethodGet)),
"head": object.NewBuiltin("http.head", MethodCmd(http.MethodHead)),
"patch": object.NewBuiltin("http.patch", MethodCmd(http.MethodPatch)),
"post": object.NewBuiltin("http.post", MethodCmd(http.MethodPost)),
"put": object.NewBuiltin("http.put", MethodCmd(http.MethodPut)),
"request": object.NewBuiltin("http.request", NewHttpRequest),
})
}
if listenersAllowed {
builtins["listen_and_serve"] = object.NewBuiltin("http.listen_and_serve", ListenAndServe)
builtins["listen_and_serve_tls"] = object.NewBuiltin("http.listen_and_serve_tls", ListenAndServeTLS)
builtins["handle"] = object.NewBuiltin("http.handle", Handle)
}
return object.NewBuiltinsModule("http", builtins)
}
Loading

0 comments on commit 2d31268

Please sign in to comment.