Skip to content

Commit

Permalink
NodeJS template
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasdarruda committed Aug 14, 2024
1 parent f84ddf1 commit 11e8b46
Show file tree
Hide file tree
Showing 16 changed files with 7,387 additions and 54 deletions.
1 change: 0 additions & 1 deletion nodejs/REDME.md

This file was deleted.

2 changes: 1 addition & 1 deletion nodejs/cookiecutter.defaults.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
default_context:
elixir_version: "1.14"
app_name: spawn_example
25 changes: 2 additions & 23 deletions nodejs/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
{
"elixir_version": [
"1.14",
"1.15",
"1.16"
],
"app_name": "elixir_sdk_example",
"app_name_hyphenate": "elixir_sdk_example",
"app_description": "Spawn App.",
"app_image_tag": "eigr/spawn-elixir-example:1.4.1",
"app_port": 8090,
"app_module_name": "ElixirSdkExample",
"spawn_app_spawm_system": "spawn-system",
"spawn_app_namespace": "default",
"spawn_app_statestore_type": "Postgres",
"spawn_sdk_version": "1.14",
"_copy_without_render": [
".github",
"docs",
".dockerignore",
".gitignore",
".envrc",
".formatter.exs"
]
"app_name": "spawn_example",
"spawn_system": "spawn-system"
}
16 changes: 0 additions & 16 deletions nodejs/{{cookiecutter.app_name}}/.k8s/host.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions nodejs/{{cookiecutter.app_name}}/.k8s/system.yaml

This file was deleted.

37 changes: 37 additions & 0 deletions nodejs/{{cookiecutter.app_name}}/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# {{ cookiecutter.app_name }}

This is a [Spawn](https://github.com/eigr/spawn) app

# Running in dev mode

Make sure the proxy is running with:

```
spawn dev run -p ./protos -s {{ cookiecutter.spawn_system }} -W
```

Run your application with:

```
yarn start
```

### Invoking your Actor

You can now invoke your Actor with:

```
curl -vvv -H 'Accept: application/json' http://localhost:9980/v1/hello_world?message=World
```

> **NOTE**: This uses the HTTP transcoding from the protobuf definition, you can also invoke this actor from other Spawn hosts.
# Setup for production

```
spawn new prod
```

# Documentation

See [SDK Documentation](https://github.com/eigr/spawn-node-sdk/tree/main?tab=readme-ov-file#documentation)
31 changes: 31 additions & 0 deletions nodejs/{{cookiecutter.app_name}}/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import spawn, { Kind } from '@eigr/spawn-sdk'
import {
HelloWorldActorState,
SayHelloRequest,
SayHelloResponse
} from './src/generated/actors/hello'
import { sayHelloHandler } from './src/hello_world_actor_handler'

const system = spawn.createSystem('{{ cookiecutter.spawn_system }}')

const actor = system.buildActor({
name: 'HelloWorldActor',
kind: Kind.NAMED,
stateType: HelloWorldActorState,
stateful: true,
snapshotTimeout: 5_000n,
deactivatedTimeout: 10_000n
})

actor.addAction(
{ name: 'SayHello', payloadType: SayHelloRequest, responseType: SayHelloResponse },
sayHelloHandler
)

system.register().then(() => {
console.log('[SpawnSystem] Actors registered successfully')

console.debug(
'[SpawnSystem] [debug] Make sure to run the Spawn Proxy with the `spawnctl dev run` command'
)
})
17 changes: 17 additions & 0 deletions nodejs/{{cookiecutter.app_name}}/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "{{ cookiecutter.app_name }}",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@eigr/spawn-sdk": "^1.2.0"
},
"devDependencies": {
"tsx": "^4.16.2"
},
"scripts": {
"build-protos": "protoc --ts_out ./src/generated/ --ts_opt force_client_none,force_server_none --proto_path ./protos/ ./protos/**/**/*.proto ./protos/**/*.proto",
"start": "yarn build-protos && tsx index.ts",
"start-bun": "yarn build-protos && bun run index.ts"
}
}
26 changes: 26 additions & 0 deletions nodejs/{{cookiecutter.app_name}}/protos/actors/hello.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";

import "google/protobuf/descriptor.proto";
import "google/api/annotations.proto";

service HelloWorldActor {
rpc SayHello(SayHelloRequest) returns (SayHelloResponse) {
option (google.api.http) = {
get: "/v1/hello_world"
};
}
}

message HelloWorldActorState {
int32 times = 1;
string last_message = 2;
}

message SayHelloRequest {
string message = 1;
}

message SayHelloResponse {
string response = 1;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.api;

import "google/api/http.proto";
import "google/protobuf/descriptor.proto";

option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";

extend google.protobuf.MethodOptions {
// See `HttpRule`.
HttpRule http = 72295728;
}
Loading

0 comments on commit 11e8b46

Please sign in to comment.