diff --git a/examples/aws-rails/lib/sst.rb b/examples/aws-rails/lib/sst.rb new file mode 100644 index 000000000..6b5cda915 --- /dev/null +++ b/examples/aws-rails/lib/sst.rb @@ -0,0 +1,32 @@ +require 'json' +module SST + class << self + private + + def parse_resource(resource_name) + env_var = "SST_RESOURCE_#{resource_name}" + parse_json(ENV[env_var]) + end + + def parse_json(json_string) + return nil if json_string.nil? + JSON.parse(json_string) + rescue JSON::ParserError + json_string # Return the original string if it's not valid JSON + end + + end + + def MyService + @MyService ||= parse_resource('MyService') + end + + def MyVpc + @MyVpc ||= parse_resource('MyVpc') + end + + def MyBucket + @MyBucket ||= parse_resource('MyBucket') + end + +end diff --git a/examples/cloudflare-auth/.gitignore b/examples/cloudflare-auth/.gitignore new file mode 100644 index 000000000..b938a7c19 --- /dev/null +++ b/examples/cloudflare-auth/.gitignore @@ -0,0 +1,4 @@ +.env + +# sst +.sst diff --git a/examples/cloudflare-auth/authenticator.ts b/examples/cloudflare-auth/authenticator.ts new file mode 100644 index 000000000..717b72353 --- /dev/null +++ b/examples/cloudflare-auth/authenticator.ts @@ -0,0 +1,34 @@ +import { auth } from "sst/auth"; + +const session = auth.sessions<{ + user: { + userID: string; + }; +}>(); + +const authorizer = () => + auth.authorizer({ + providers: {}, + session, + callbacks: { + auth: { + async allowClient() { + return true; + }, + async success(ctx, input, req) { + return ctx.session({ + type: "user", + properties: { + userID: "ok", + }, + }); + }, + }, + }, + }); + +export default { + fetch(event: any, ctx: any) { + return authorizer().fetch(event, ctx); + }, +}; diff --git a/examples/cloudflare-auth/bun.lockb b/examples/cloudflare-auth/bun.lockb new file mode 100755 index 000000000..faed8d72a Binary files /dev/null and b/examples/cloudflare-auth/bun.lockb differ diff --git a/examples/cloudflare-auth/package.json b/examples/cloudflare-auth/package.json new file mode 100644 index 000000000..b15dfb74a --- /dev/null +++ b/examples/cloudflare-auth/package.json @@ -0,0 +1,17 @@ +{ + "name": "cloudflare-worker", + "version": "1.0.0", + "description": "", + "type": "module", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "@cloudflare/workers-types": "^4.20240403.0", + "sst": "latest" + } +} diff --git a/examples/cloudflare-auth/sst-env.d.ts b/examples/cloudflare-auth/sst-env.d.ts new file mode 100644 index 000000000..ac168d9fd --- /dev/null +++ b/examples/cloudflare-auth/sst-env.d.ts @@ -0,0 +1,10 @@ +/* This file is auto-generated by SST. Do not edit. */ +/* tslint:disable */ +/* eslint-disable */ +import "sst" +export {} +import "sst" +declare module "sst" { + export interface Resource { + } +} diff --git a/examples/cloudflare-auth/sst.config.ts b/examples/cloudflare-auth/sst.config.ts new file mode 100644 index 000000000..b692a3434 --- /dev/null +++ b/examples/cloudflare-auth/sst.config.ts @@ -0,0 +1,21 @@ +/// + +export default $config({ + app(input) { + return { + name: "cloudflare-auth", + removal: input?.stage === "production" ? "retain" : "remove", + home: "cloudflare", + }; + }, + async run() { + const auth = new sst.cloudflare.Auth("Auth", { + authenticator: { + handler: "./authenticator.ts", + }, + }); + return { + url: auth.url, + }; + }, +}); diff --git a/examples/cloudflare-auth/tsconfig.json b/examples/cloudflare-auth/tsconfig.json new file mode 100644 index 000000000..c6b5c8500 --- /dev/null +++ b/examples/cloudflare-auth/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "lib": ["esnext"], + "types": ["@cloudflare/workers-types"], + "moduleResolution": "bundler", + "module": "ESNext", + } +} diff --git a/pkg/types/rails/rails.go b/pkg/types/rails/rails.go new file mode 100644 index 000000000..d8c0c0f61 --- /dev/null +++ b/pkg/types/rails/rails.go @@ -0,0 +1,57 @@ +package rails + +import ( + "fmt" + "io" + "os" + "path/filepath" + + "github.com/sst/ion/internal/fs" + "github.com/sst/ion/pkg/project/common" +) + +func Generate(root string, links common.Links) error { + projects := fs.FindDown(root, "config.ru") + files := []io.Writer{} + for _, project := range projects { + // check if lib path exists + if _, err := os.Stat(filepath.Join(filepath.Dir(project), "lib")); err == nil { + path := filepath.Join(filepath.Dir(project), "lib", "sst.rb") + file, _ := os.Create(path) + files = append(files, file) + } + } + writer := io.MultiWriter(files...) + writer.Write([]byte(`require 'json' +module SST + class << self + private + + def parse_resource(resource_name) + env_var = "SST_RESOURCE_#{resource_name}" + parse_json(ENV[env_var]) + end + + def parse_json(json_string) + return nil if json_string.nil? + JSON.parse(json_string) + rescue JSON::ParserError + json_string # Return the original string if it's not valid JSON + end + + end +`, + )) + + for name := range links { + writer.Write([]byte(fmt.Sprintf(` + def %s + @%s ||= parse_resource('%s') + end +`, name, name, name))) + } + + writer.Write([]byte("\nend")) + + return nil +} diff --git a/pkg/types/types.go b/pkg/types/types.go index 7d977c46f..dc4f5a40e 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -6,6 +6,7 @@ import ( "github.com/sst/ion/pkg/project/common" "github.com/sst/ion/pkg/project/path" "github.com/sst/ion/pkg/types/python" + "github.com/sst/ion/pkg/types/rails" "github.com/sst/ion/pkg/types/typescript" ) @@ -30,4 +31,5 @@ func Generate(cfgPath string, complete common.Links) error { var All = []Generator{ typescript.Generate, python.Generate, + rails.Generate, }