Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
types: codegen sst module for ruby on rails projects
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Oct 8, 2024
1 parent 719f9b1 commit 35b8b95
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/aws-rails/lib/sst.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions examples/cloudflare-auth/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env

# sst
.sst
34 changes: 34 additions & 0 deletions examples/cloudflare-auth/authenticator.ts
Original file line number Diff line number Diff line change
@@ -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);
},
};
Binary file added examples/cloudflare-auth/bun.lockb
Binary file not shown.
17 changes: 17 additions & 0 deletions examples/cloudflare-auth/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions examples/cloudflare-auth/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -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 {
}
}
21 changes: 21 additions & 0 deletions examples/cloudflare-auth/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path="./.sst/platform/config.d.ts" />

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,
};
},
});
8 changes: 8 additions & 0 deletions examples/cloudflare-auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"lib": ["esnext"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "bundler",
"module": "ESNext",
}
}
57 changes: 57 additions & 0 deletions pkg/types/rails/rails.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -30,4 +31,5 @@ func Generate(cfgPath string, complete common.Links) error {
var All = []Generator{
typescript.Generate,
python.Generate,
rails.Generate,
}

0 comments on commit 35b8b95

Please sign in to comment.