Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshiarora386 committed Nov 11, 2024
2 parents 8707ccc + 9622d63 commit bb15e61
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 279 deletions.
34 changes: 22 additions & 12 deletions docs/microservices-framework/CRUD_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ title: Generating CRUD API
The gen-crud-api command in Godspeed is a powerful tool that automatically generates CRUD (Create, Read, Update, Delete) APIs for your data models.
<!-- This command significantly simplifies the process of building back-end APIs, allowing you to focus on other parts of your application. -->

The framework generates CRUD API using Prisma's database model files and ORM client. It uses Godspeed's [Prisma plugin](./datasources/datasource-plugins/Prisma%20Datasource.md) as the ORM and generates **http** eventsource based CRUD APIs by default.
The framework generates CRUD API using Prisma's database model files and ORM client. It uses Godspeed's [Prisma plugin](./datasources/datasource-plugins/Prisma%20Datasource.md) as the ORM and generates CRUD APIs served from **http** eventsource, which is Express.js by default.

**Currently supported eventsources:**
<!-- **Currently supported eventsources:**
- Http eventsources: [Express](./event-sources/event-source-plugins/Express%20Http%20Eventsource.md), [Fastify](./event-sources/event-source-plugins/Fastify%20Eventsource.md)
- Graphql eventsource: [Apollo Graphql](./event-sources/event-source-plugins/Apollo%20GraphQl%20Eventsource.md)
- Graphql eventsource: [Apollo Graphql](./event-sources/event-source-plugins/Apollo%20GraphQl%20Eventsource.md) -->

### Watch this video to see how CRUD API is generated in Godspeed

<div style={{ position: 'relative', paddingBottom: '56.25%', height: 0, overflow: 'hidden' }}>
<iframe style={{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%' }} src="https://www.youtube.com/embed/UOtFzRaoQnE?si=P_NqkqfdBVY1jJop" frameborder="0" allowfullscreen></iframe>
</div>

### Steps to generate CRUD API over REST and Graphql
## Steps to generate CRUD API over REST

### Step 1. Create a godspeed project
Create a new project from the CLI and open the created project in vscode

[(See How to create)](./guide/get-started.md#step-2:-create-a-project-and-start-the-server)

### Step 2. Install the prisma plugin
Add the 'prisma-as-datastore' plugin from the CLI of vscode
Add the 'prisma-as-datastore' plugin from the CLI

[(See How to add Prisma plugin)](./datasources/datasource-plugins/Prisma%20Datasource.md#add-plugin)

Expand Down Expand Up @@ -58,6 +58,18 @@ datasource db {
### Step 4. Create Prisma Schema
Now Create a [prisma schema](https://www.prisma.io/docs/orm/prisma-schema/overview#example) file in `src/datasources` directory

> **Important Note**:
> When configuring the Prisma client in your Godspeed project, ensure you add the `output` field in your Prisma schema's `generator` block. This field should point to this location `src/datasources/prisma-clients/<name_of_your_prisma_file>` where the generated Prisma client files will be stored.

```prisma
generator client {
provider = "prisma-client-js"
output = "./prisma-clients/lms" //just change the name of prisma schema here
previewFeatures = ["metrics"] // to be used in case you want to generate metrics for prisma queries for telemetry.
}
```
This setup ensures that the generated client is available at the specified path i.e. `src/datasources/prisma-clients/`

If your schema name is **lms.prisma**, your file content should look like this.

```prisma
Expand All @@ -68,8 +80,8 @@ If your schema name is **lms.prisma**, your file content should look like this.
generator client {
provider = "prisma-client-js"
output = "./prisma-clients/lms"
previewFeatures = ["metrics"]
output = "./prisma-clients/lms" //in place of lms, give the name of your prisma schema here
previewFeatures = ["metrics"] //if you want to generate metrics for prisma queries for telemetry
}
model User {
Expand All @@ -82,11 +94,9 @@ If your schema name is **lms.prisma**, your file content should look like this.

4.2 Copy the generated file to `src/datasources` folder and rename it as per the name of this datasource that you want to keep. If you don't have an existing database setup with a model, then create a prisma model file from scratch.

4.3 Make sure to note the `output` parameter in the .prisma file which should point to location in `src/datasources/prisma-clients/<name_of_your_prisma_file>` and `previewFeatures` is to be added in case you want to generate metrics for prisma queries for telemetry.


### Step 5. Generate prisma client and sync your database
Run `godspeed prisma prepare`. This command will generate the prisma client and will sync the database with prisma schema. The generated client will be stored in `src/datasources/prisma-clients/` folder.
Run `godspeed prisma prepare`. This command will generate the prisma client and will sync the database with prisma schema. The generated client will be stored in `src/datasources/prisma-clients/` folder which we specified above in the generator client block.

```bash
$ godspeed prisma prepare
Expand Down Expand Up @@ -132,7 +142,7 @@ Inspect generated events, definitions and functions.

`localhost:<http_port>/<http_docs_endpoint>` which is by default `localhost:3000/api-docs`

### To expose same API via Graphql
<!-- ### To expose same API via Graphql

Simply add Graphql plugin and change your event URIs which have `http` to `http & graphql`, keeping the rest as the same. See how to use Graphql in detail in the [Apollo Graphql plugin documentation](./event-sources/event-source-plugins/Apollo%20GraphQl%20Eventsource.md)

Expand All @@ -142,6 +152,6 @@ Inspect generated events, definitions and functions.
<div style={{ position: 'relative', paddingBottom: '56.25%', height: 0, overflow: 'hidden' }}>

<iframe style={{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%' }} src="https://www.youtube.com/embed/dVt6GPSgY7A?si=gYrEESjBpIOfuNM5&amp;start=205" frameborder="0" allowfullscreen></iframe>
</div>
</div> -->


24 changes: 14 additions & 10 deletions docs/microservices-framework/authentication/jwt-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,23 @@ In our Express eventsource plugin, JWT Authentication is implemented using passp
**(b)** Export these variables to your environment, follow the below syntax to export, based on the shell, you are using:
<details>
<summary> For git bash </summary>
```

```bash
$ export JWT_SECRET=mysecret
$ export JWT_ISS= mycompany
```

</details>

<details> <summary> For windows powershell </summary>

```
<details>
<summary> For windows powershell </summary>

```bash
$env:JWT_SECRET= "mysecret"
$env:JWT_ISS= "mycompany"
$env:JWT_ISS= "mycompany"
```
</details>

</details>

After exporting the environment variable, you can access these variable anywhere in your project by using inline
scripting `<%config.issuer%>` in yaml or as `ctx.config.issuer` in js/ts workflows.
Expand All @@ -69,7 +73,7 @@ If you do not set these environment variables mentioned above, it will result in
### Step 2: Enable JWT Auth in your project's eventsource configuration file.
JWT configuration is written under authn: in the event source's configuration file. For Express, config file name will be http.yaml. Open this file and Set up jwt authn as shown below.

'src/eventsources/http.yaml'
`src/eventsources/http.yaml`
```
type: express
port: 4000
Expand Down Expand Up @@ -98,8 +102,8 @@ Once you have enabled it here, authentication will be true for all endpoints, un
required: true
```

<details>
<summary> User Login Example using JWT Authentciation </summary>

### User Login Example using JWT Authentciation

**Event**
```yaml
Expand Down Expand Up @@ -164,7 +168,7 @@ http.post./login: # defines the POST request that will be triggered when a cli
}
}
```
</details>


### How to access JWT payload
You can access the complete JWT payload in <% inputs.user %> in YAML workflows, and as ctx.inputs.data.user when writing JS/TS workflows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ Lets understand how to implement the above steps :

### Step 1: Define Configuration file

Inside the `datasources` directory, create a `YAML` file with a specific name to configure the datasource (e.g., kafka.yaml, gpt.yaml).
In this YAML file, ensure you specify a `type` field, and there must be a corresponding `TypeScript` file in the `types` directory that shares the same name as the `type` you defined. Like this:-
- Inside the `datasources` directory, create `YAML` files to setup your datasource integrations. For example, chatgpt.yaml or kafka.yaml.

- In this YAML file, ensure you specify a `type` field, and there must be a corresponding `TypeScript` file in the `types` directory that shares the same name as the `type` you defined.

:::tip
You can create multiple instances of same datasource type by creating multiple yaml files. For example, chatgpt1.yaml and chatgpt2.yaml.

FYI you can do the same for eventsources as well, For example http1.yaml and http2.yaml where both are of `type: express` and run on different ports.
:::

Let's see an example of creating chatgpt as custom datasource.

```
.
├── src
├── datasources
│ ├── types
│ | └── custom_datasource.ts
│ | └── chatgpt.ts
| |
│ └── custom_datasource.yaml
│ └── chatgpt.yaml
├── events
|
Expand All @@ -37,23 +46,26 @@ In this YAML file, ensure you specify a `type` field, and there must be a corres

### Example

GPT config file ( src/datasources/gpt.yaml )
Chatgpt Config file- `src/datasources/chatgpt.yaml`

```yaml
type: CHATGPT # Defines the datasource type as Chatgpt
type: chatgpt # should be same as the name of your datasource typescript file in `src/datasources/types/`

# custom configurations as per your datasource logic
model: gpt-4o
temperature: 1
max_tokens: 200

```
This file defines the model, temperature, and token settings for the ChatGPT API.
## Step 2: Implement datasource logic
1. In `src/datasources/types`, create a TypeScript file for your datasource logic (e.g., `kafka.ts`).
1. In `src/datasources/types`, create a TypeScript file for your datasource logic (e.g., `chatgpt.ts`).
2. Import `GSDataSource` and other required modules from `@godspeedsystems/core`.
3. Extend the `GSDataSource` class to implement custom methods for interacting with your custom datasource.
4. Initialize your client by calling the `initClient()` function.
5. Once your client is initialized, you can execute its methods using the `execute` function.
3. Extend the `GSDataSource` abstract class to implement custom methods to interact with your custom datasource.
4. Initialize and return your client by implementing the abstract `initClient()` method of `GSDatasource`.
5. Once your client is initialized, then implement the abstract `execute()` method of `GSDatasource`.

### Template for your custom datasource logic :
You can use the following template to start writing your custom datasource logic and then modify it as per your requirement.
Expand Down Expand Up @@ -97,7 +109,7 @@ You can use the following template to start writing your custom datasource logic

### Example Datasource Logic for GPT plugin

(src/datasources/types/gpt.ts)
`src/datasources/types/chatgpt.ts`

```typescript

Expand Down
43 changes: 30 additions & 13 deletions docs/microservices-framework/guide/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
:::tip Need Help?
**[Ask Godspeed GPT First!](https://chatgpt.com/g/g-Zsh9qhlGw-vishwakarma)**

Whether you're having trouble with setup, configurations or understanding the framework, try asking [Godspeed GPT](https://chatgpt.com/g/g-Zsh9qhlGw-vishwakarma)
Whether you're having trouble with setup, configurations or understanding the framework, try asking [Godspeed GPT](https://chatgpt.com/g/g-Zsh9qhlGw-vishwakarma) or you can [access the FAQs in our guide](/docs/microservices-framework/guides)
:::

### Watch this One-Click Installation & Step-by-Step Tutorial Guide

<div style={{ position: 'relative', paddingBottom: '56.25%', height: 0, overflow: 'hidden' }}>
<iframe style={{ position: 'absolute', top: 10, left: 10, width: '100%', height: '80%' }} src="https://www.youtube.com/embed/xb0fgMmFywc?si=EhuxwGAXJSSmOUCX" frameborder="0" allow="fullscreen;" allowfullscreen ></iframe>
</div>

### Pre-requisites:
1. Nodejs v18 (or higher) or Bunjs
2. Npm
3. Git
4. VS Code or any code editor

### **Step 1: Install Godspeed**
### Step 1: Install Godspeed
:::tip
To install prerequisites and Godspeed through our Easy Installation Script, Download it from the link provided below:
:::
Expand Down Expand Up @@ -73,7 +79,7 @@ Once the script finishes, you are ready to **[Create Your First Project](/docs/m
godspeed --version
```
---
### **Step 2: Create Your First Project**
### Step 2: Create Your First Project

1. **Create a new Godspeed project**:
- Use the `create` command to set up a new project:
Expand All @@ -91,27 +97,38 @@ Once the script finishes, you are ready to **[Create Your First Project](/docs/m
```
godspeed serve
```
Check the logs. They should indicate that the **Express server** is running on **port 3000**.
Example log:
Check the logs. They should indicate that the **Express server** is running on **port 3000**

Example log:
```bash
INFO:[Production Server Running]('express:' eventsource, '3000' port) Try it out at: http://localhost:3000/api-docs
```
---

### **Step 3: Test the Helloworld API**
### Step 3: Access Swagger UI

1. **Open Swagger UI**:
- Open your browser and navigate to `http://localhost:3000/api-docs`. This will display the **Swagger UI**.
![img](../../../static/img/swagger_helloworld.png)
In Godspeed, the **Swagger UI** is typically accessed at the `/api-docs` endpoint, appended to the `base URL` and `port` where the server is running. Here’s the general format for accessing Swagger UI:
```plaintext
http://<BASE_URL>:<PORT>/<http_docs_endpoint>` which is by default `localhost:3000/api-docs`
```
Default port of your service is `3000` and Swagger endpoint is `/api-docs`. If you want to customise default settings, you can modify the `./src/eventsources/http.yaml`
**To access Swagger UI, navigate to the following default url:**
```plaintext
http://localhost:3000/api-docs
```
![img](../../../static/img/swagger_helloworld.png)
### Step 4: Test the Helloworld API
2. **Try It Out**:
- In the Swagger UI, locate the `/helloworld` endpoint.
Click the **`Try it out`** button and send a request to test the API, It will ask you to fill the name. Once you fill and submit the name parameter,(e.g. John) then following response will be returned from the server.
```
Hello `John`
```
Default port of your service is `3000` and Swagger endpoint is `/api-docs`. If you want to customise default settings, you can modify the `./src/eventsources/http.yaml`
To understand working of this API [Lets Walkthrough your first Godspeed Project](https://godspeed.systems/docs/microservices-framework/guide/get-started#walking-through-your-first-godspeed-project)
Expand Down Expand Up @@ -235,11 +252,11 @@ Almost every application needs validation of data sent in request to the API and
Hello mastersilv3r
```
#### Swagger Collection
### Swagger Collection
If you need access to the Swagger collection of godspeed project, open it from `/docs` folder in your project. This is automatically generated from your API schema which we saw above.
#### Postman Collection
### Postman Collection
If you need the Postman Collection, import the Swagger file from `src/docs` in Postman.
Expand Down
Loading

0 comments on commit bb15e61

Please sign in to comment.