Skip to content

Commit

Permalink
[yaml-v2] Update Backend Deployment feature (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
costinsin authored Mar 6, 2024
1 parent ce4ab8a commit 9e9830c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 69 deletions.
36 changes: 31 additions & 5 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,48 @@ defaults:
on:
pull_request:
types: [opened, synchronize]
branches:
- main
- dev

jobs:
deploy_preview:
name: Deploy Preview
runs-on: ubuntu-latest
env:
HUGO_VERSION: 0.120.4
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: Genez-io/genezio-github-action@main
with:
token: ${{ secrets.GENEZIO_TOKEN }}
- name: Install Hugo CLI
run: |
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
- name: Install Dart Sass
run: sudo snap install dart-sass
- name: Install Docs Node.js dependencies
run: npm ci
- name: Build Docs
run: npx docusaurus build --out-dir public
run: npm run build
- name: Clone Repo https://github.com/Genez-io/genezio-landing-page/
run: git clone https://github.com/Genez-io/genezio-landing-page/
- name: Build with Hugo
env:
# For maximum backward compatibility with Hugo modules
HUGO_ENVIRONMENT: production
HUGO_ENV: production
run: |
cd genezio-landing-page
npm ci
hugo \
--minify \
--baseURL "https://genezio-landing-page-$STAGE.app.genez.io"
- name: Copy public folder
run: cp -r genezio-landing-page/public/ public/
- name: Copy build files to /public/docs
run: |
mkdir -p public/docs/
cp -r build/* public/docs/
- name: Compute stage
run: echo "STAGE=$(echo -n ${GITHUB_REF##*/} | shasum | head -c 8)" >> $GITHUB_ENV
- name: Deploy
Expand All @@ -35,5 +61,5 @@ jobs:
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `👋 Your preview is ready! ${process.env["STAGE"]}`
body: `👋 Your preview is ready! https://genezio-landing-page-${process.env["STAGE"]}.app.genez.io/docs/`
})
191 changes: 128 additions & 63 deletions docs/features/backend-deployment.md
Original file line number Diff line number Diff line change
@@ -1,106 +1,171 @@
---
sidebar_position: 1
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import Admonition from '@theme/Admonition';

# Backend Deployment

Genezio offers a seamless and efficient solution for deploying backend logic. The platform leverages a function-as-a-service infrastructure, allowing users to deploy their backend classes easily with a simple command: `genezio deploy`. 

<!-- {% hint style="success" %} -->

:::tip
You can deploy classes written in TypeScript, JavaScript, and Dart (experimental).
You can deploy classes written in TypeScript, JavaScript, Go (experimental) and Dart (experimental).
:::

<!-- ::: -->

## Code Structure

To deploy with genezio, your code has to be structured in classes. Mark your classes for deployment with the `GenezioDeploy` decorator. This decorator also enables you to set the default type of the methods implemented in the specific class to `jsonrpc`, `http` or `cron`.

<!-- :::info -->

:::info
Our recommendation is to use the official genezio templates and examples to be provided with fully written configuration files without having to write one from scratch.
:::warning
We do not recommend you to manually create your projects. You should instead use the `genezio create` command to create a new project. This command will generate a project with the correct structure and configuration files.

Check out this tutorial to learn how to [create-your-first-project](../getting-started "mention") with genezio.
:::

<!-- ::: -->
## Code Structure

A snippet of a class that can be deployed with genezio is below:
To deploy with genezio, your code has to be structured in classes. Each public method of the class will be deployed as a separate function.

<!-- {% code title="index.ts" %} -->
The following snippet of code shows a simple `HelloWorldService` class:

```typescript title="index.ts" showLineNumbers
import { GenezioDeploy } from "@genezio/types";
<Tabs groupId="languages">
<TabItem value="ts" label="TypeScript">
```ts title="service.ts" showLineNumbers
import { GenezioDeploy } from "@genezio/types";

@GenezioDeploy()
export class HelloWorldService {
hello(name: string, sender: string): string {
console.log(
`Hello world request received with name ${name} from ${sender}!`
);
return `Hello, ${name}, from ${sender}!`;
}
}
```
@GenezioDeploy()
export class HelloWorldService {
hello(name: string, sender: string): string {
console.log(`Hello world request received with name ${name} from ${sender}!`);

<!-- {% endcode %} -->
return `Hello, ${name}, from ${sender}!`;
}
}
```

## Configuration file
</TabItem>
<TabItem value="js" label="JavaScript">
```js title="service.js" showLineNumbers
import { GenezioDeploy } from "@genezio/types";

To deploy your code, your project needs a minimum configuration file that sets various configurations for the project such as name, region to deploy to, and so on. A minimum `genezio.yaml` to deploy a project should look like this:
@GenezioDeploy()
export class HelloWorldService {
hello(name, sender) {
console.log(`Hello world request received with name ${name} from ${sender}!`);

<!-- {% code title="genezio.yaml" %} -->
return `Hello, ${name}, from ${sender}!`;
}
}
```

```yaml title="genezio.yaml" showLineNumbers
name: my-project-backend
region: us-east-1
language: ts
cloudProvider: genezio
packageManager: npm
```
</TabItem>
<TabItem value="go" label="Go">
```go title="service.go" showLineNumbers
type HelloWorldService struct {}

<!-- {% endcode %} -->
// Required. Will run when the HelloWorldService "class" is constructed.
func New() HelloWorldService {
return HelloWorldService{}
}

For more details on the `genezio.yaml` file, check the [genezio-configuration-file](../project-structure/genezio-configuration-file "mention") section.
func (s HelloWorldService) Hello(name string, sender string) string {
fmt.Println("Hello world request received with name", name, "from", sender, "!")

<!-- :::info -->
return "Hello, " + name + ", from " + sender + "!"
}
```

:::info
For Dart backend code, a classes list should also be set in the `genezio.yaml`. Check the classes section from [#configuration-file](../project-structure/genezio-configuration-file "mention")
for more information.
:::
<Admonition type="tip">
The `New` function is required in Go because, unlike TypeScript and JavaScript, Go does not embrace OOP, therefore there is no way to specify a `struct` constructor.
</Admonition>

</TabItem>
</Tabs>

<!-- ::: -->
## Configuration file

The `genezio.yaml` configuration file is the file that genezio uses to understand how to deploy your project. It contains information about the project, such as the name, region, and the backend details.

<Tabs groupId="languages">
<TabItem value="ts" label="TypeScript">
```yaml title="genezio.yaml" showLineNumbers
name: my-backend
region: us-east-1
yamlVersion: 2
backend:
path: .
language:
name: ts
runtime: nodejs20.x
classes:
- path: service.ts
```

<Admonition type="tip">
The `classes` field can be omitted if the classes are decorated with `@GenezioDeploy()`. Learn more about decorators in the [Decorators](../project-structure/genezio-decorators "mention") section.

If a class is neither decorated or specified in the `genezio.yaml` file, it will not be deployed.
</Admonition>

</TabItem>
<TabItem value="js" label="JavaScript">
```yaml title="genezio.yaml" showLineNumbers
name: my-backend
region: us-east-1
yamlVersion: 2
backend:
path: .
language:
name: js
runtime: nodejs20.x
classes:
- path: service.js
```

<Admonition type="tip">
The `classes` field can be omitted if the classes are decorated with `@GenezioDeploy()`.

If a class is neither decorated or specified in the `genezio.yaml` file, it will not be deployed.
</Admonition>

</TabItem>
<TabItem value="go" label="Go">
```yaml title="genezio.yaml" showLineNumbers
name: my-backend
region: us-east-1
yamlVersion: 2
backend:
path: .
language:
name: go
classes:
- path: service.go
```

</TabItem>
</Tabs>

For more details on the `genezio.yaml` configuration file features, check the [Genezio Configuration File](../project-structure/genezio-configuration-file "mention") section.

## Deploy your project

The command to deploy has to be executed at the same path where `genezio.yaml`is saved:
After you have written your classes and configured the `genezio.yaml` file, you can deploy your project by running the following command:

```
```sh title="Terminal"
genezio deploy
```

Executing `genezio deploy` will deploy the `HelloWorldService`class implemented in the snippet above.

## Testing

webhooks
You can make requests to the remote server either by using the[ testing functionality from the genezio dashboard](testing), or by using a client application - such as a React app.
After deploying, you can make requests to the remote server by:

To call the methods implemented in the `HelloWorldService`, you can make use of the genezio generated SDK for your project. Check [generated-sdk](generated-sdk "mention") section to find out how to install it in your project.
- using the [testing functionality](testing) from the genezio dashboard
- building a client application that uses the generated type safe SDK - such as a React app or a CLI
- Check the [Generated Sdk](generated-sdk "mention") section to find out how to use the Genezio SDK in your project.

## Next Steps <a href="#next-steps" id="next-steps"></a>
## Next Steps

Now let's see how to schedule the execution of a function as a cron job, or implement HTTP Webhooks:

- [Cron Jobs](cron-methods)
- [HTTP Webhooks](http-methods-webhooks)
- [Cron Jobs](cron-methods)
- [HTTP Webhooks](http-methods-webhooks)

Now you are ready for some more advanced use cases:

- [Web3 Application](https://genezio.com/blog/create-your-first-web3-app/)
- [ChatGPT App](https://genezio.com/blog/create-your-first-app-using-chatgpt/)
- [Shopping Cart Implementation](https://genezio.com/blog/implement-a-shopping-cart-using-typescript-redis-and-react/)
- [Web3 Application](https://genezio.com/blog/create-your-first-web3-app/)
- [ChatGPT App](https://genezio.com/blog/create-your-first-app-using-chatgpt/)
- [Shopping Cart Implementation](https://genezio.com/blog/implement-a-shopping-cart-using-typescript-redis-and-react/)
2 changes: 1 addition & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const config = {
// },
prism: {
theme: prismThemes.oneLight,
darkTheme: prismThemes.dracula,
darkTheme: prismThemes.oneDark,
},
},
};
Expand Down

0 comments on commit 9e9830c

Please sign in to comment.