Skip to content

Commit

Permalink
Update service definition docs
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-johansson committed Sep 26, 2024
1 parent 1f61670 commit 87b76d5
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 17 deletions.
19 changes: 13 additions & 6 deletions docs/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ One such element is the API annotation:
```

This annotation is all that's needed for Encore to understand that the Go package `hello` is a service, and
the `World` function is a public API endpoint.
the `World` function is a public API endpoint.

To create more services and endpoints, you simply create new Go packages and define endpoints using
the `//encore:api` annotation. _If you're curious, you can read more about [defining APIs](/docs/primitives/apis)._
Expand Down Expand Up @@ -116,7 +116,7 @@ The Local Development Dashboard is a powerful tool to help you move faster when
It comes with an API explorer, a Service Catalog with automatically generated documentation, and powerful oberservability features
like [distributed tracing](/docs/observability/tracing).

Through the Local Development Dashboard you also have access to [Encore Flow](/docs/develop/encore-flow),
Through the Local Development Dashboard you also have access to [Encore Flow](/docs/develop/encore-flow),
a visual representation of your microservice architecture that updates in real-time as you develop your application.

### Call your API
Expand Down Expand Up @@ -289,10 +289,17 @@ interface Response {

As you can see, it's all standard TypeScript.

You define an API endpoint by wrapping a regular async function in a call to `api`. Doing this makes Encore identify the `hello` directory as a service,
and that the `world` function is a public API endpoint. Encore automatically handles authentication, HTTP routing, request validation, error handling, observability, API documentation, and more.
You define an API endpoint by wrapping a regular async function in a call to `api`. Doing this makes Encore identify the `world` function as a public API endpoint. Encore automatically handles authentication, HTTP routing, request validation, error handling, observability, API documentation, and more.

If you want to create more services and endpoints, you simply create new folders and define endpoints by wrapping functions in the `api` function. _If you're curious, you can read more about defining [services](/docs/ts/primitives/services) and [APIs](/docs/ts/primitives/apis)._
The `world` endpoint is part of the `hello` service because in the same folder you will also find a file named `encore.service.ts` which looks like this:

```ts
import { Service } from "encore.dev/service";

export default new Service("hello");
```

This is how you create microservices with Encore. Encore will now consider files in the `hello` directory and all its subdirectories as part of the `hello` service. If you want to create more services, simply create a new folders, add a `encore.service.ts` file that is exporting a new `Service`. _If you're curious, you can read more about defining [services](/docs/ts/primitives/services) and [APIs](/docs/ts/primitives/apis)._

The Encore.ts [Backend Framework](/docs/ts) provides several declarative ways of using backend primitives like databases, Pub/Sub, and scheduled tasks by simply writing code.

Expand Down Expand Up @@ -329,7 +336,7 @@ The Local Development Dashboard is a powerful tool to help you move faster when
It comes with an API explorer, a Service Catalog with automatically generated documentation, and powerful oberservability features
like [distributed tracing](/docs/observability/tracing).

Through the Local Development Dashboard you also have access to [Encore Flow](/docs/develop/encore-flow),
Through the Local Development Dashboard you also have access to [Encore Flow](/docs/develop/encore-flow),
a visual representation of your microservice architecture that updates in real-time as you develop your application.

### Call your API
Expand Down
29 changes: 27 additions & 2 deletions docs/ts/primitives/services.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ lang: ts

Encore.ts makes it simple to build applications with one or many services, without needing to manually handle the typical complexity of developing microservices.

## Defining services
## Defining services explicitly (recommended)

To create an Encore service, add a file named `encore.service.ts` in a directory.

Expand All @@ -25,4 +25,29 @@ export default new Service("my-service");

That's it! Encore will consider this directory and all its subdirectories as part of the service.

For more on how to structure your application, see the [app structure guide](/docs/ts/develop/app-structure).
For more on how to structure your application, see the [app structure guide](/docs/ts/develop/app-structure).

## Defining services implicitly

If you [define an API](/docs/ts/primitives/services-and-apis#defining-apis), Encore will automatically consider the parent directory as a service. That is, if an `encore.service.ts` file is not present.

On disk it might look like this:

```
/my-app
├── encore.app // ... and other top-level project files
├── package.json
├── hello // hello service (a folder)
│   └── hello.ts // contains one or more API endpoints
└── world // world service (a folder)
└── world.ts // contains one or more API endpoints
```

<Callout type="important">

Defining services implicitly limits the way in which you can structure your application as all API endpoints must be defined in the top-level directory for the service.
For more flexibility, define services explicitly.

</Callout>
22 changes: 21 additions & 1 deletion docs/tutorials/rest-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,29 @@ This is needed when you want Encore to manage functionality like secrets and han

Now let's create a new `url` service.

🥐 In your application's root folder, create a new folder `url` and create a new file `url.ts` that looks like this:
🥐 In your application's root folder, create a directory named `url` containing a file named `encore.service.ts`.

```shell
$ mkdir url
$ touch url/encore.service.ts
```

🥐 Add the following code to `url/encore.service.ts`:

```ts
-- url/encore.service.ts --
import { Service } from "encore.dev/service";

export default new Service("url");
```

This is how you create microservices with Encore. Encore will now consider files in the `url` directory and all its subdirectories as part of the `url` service.


🥐 Create a new file `url.ts` in the `url` directory that looks like this:

```ts
-- url/url.ts --
import { api } from "encore.dev/api";
import { randomBytes } from "node:crypto";

Expand Down
20 changes: 19 additions & 1 deletion docs/tutorials/slack-bot.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,25 @@ Since Slack sends custom HTTP headers that we need to pay attention to, we're go
use a raw endpoint in Encore. For more information on this check out Slack's documentation
on [Enabling interactivity with Slash Commands](https://api.slack.com/interactivity/slash-commands).

🥐 In your Encore app, create a new directory named `slack` and create a file `slack/slack.ts` with the following contents:
🥐 In your Encore app, create a directory named `slack` containing a file named `encore.service.ts`.

```shell
$ mkdir slack
$ touch slack/encore.service.ts
```

🥐 Add the following code to `slack/encore.service.ts`:

```ts
-- slack/encore.service.ts --
import { Service } from "encore.dev/service";
export default new Service("slack");
```

This is how you create microservices with Encore. Encore will now consider files in the `slack` directory and all its subdirectories as part of the `slack` service.

🥐 Create a file `slack/slack.ts` with the following contents:

```ts
import { api } from "encore.dev/api";
Expand Down
53 changes: 46 additions & 7 deletions docs/tutorials/uptime.md
Original file line number Diff line number Diff line change
Expand Up @@ -912,13 +912,26 @@ Let's start by creating the functionality to check if a website is currently up
Later we'll store this result in a database so we can detect when the status changes and
send alerts.

🥐 Create an Encore service named `monitor` containing a file named `ping.ts`.
🥐 Create a directory named `monitor` containing a file named `encore.service.ts`.

```shell
$ mkdir monitor
$ touch monitor/ping.ts
$ touch monitor/encore.service.ts
```

🥐 Add the following code to `monitor/encore.service.ts`:

```ts
-- monitor/encore.service.ts --
import { Service } from "encore.dev/service";

export default new Service("monitor");
```

This is how you create microservices with Encore. Encore will now consider files in the `monitor` directory and all its subdirectories as part of the `monitor` service.

🥐 In the `monitor` directory, create a file named `ping.ts`.

🥐 Add an Encore API endpoint named `ping` that takes a URL as input and returns a response
indicating whether the site is up or down.

Expand Down Expand Up @@ -1032,13 +1045,25 @@ PASS Waiting for file changes...

Next, we want to keep track of a list of websites to monitor.

Since most of these APIs will be simple "CRUD" (Create/Read/Update/Delete) endpoints, let's build this service using [Knex.js](https://knexjs.org/), an ORM
library that makes building CRUD endpoints really simple.
Since most of these APIs will be simple "CRUD" (Create/Read/Update/Delete) endpoints, let's build this service using [Knex.js](https://knexjs.org/), an ORM library that makes building CRUD endpoints really simple.

🥐 Let's create a new service named `site` with a SQL database. To do so, create a new directory `site` in the application root with `migrations` folder inside that folder:
🥐 Let's start with creating a new service named `site`:

```shell
$ mkdir site # Create a new directory in the application root
$ touch site/encore.service.ts
```

```ts
-- site/encore.service.ts --
import { Service } from "encore.dev/service";

export default new Service("site");
```

🥐 Now we want to add a SQL database to the `site` service. To do so, create a new directory named `migrations` folder inside the `site` folder:

```shell
$ mkdir site
$ mkdir site/migrations
```

Expand Down Expand Up @@ -1467,7 +1492,21 @@ a Pub/Sub subscriber that posts these events to Slack.

## 7. Send Slack notifications when a site goes down

🥐 Start by creating a Slack service containing the following:
🥐 Start by creating a new service named `slack`:

```shell
$ mkdir slack # Create a new directory in the application root
$ touch slack/encore.service.ts
```

```ts
-- slack/encore.service.ts --
import { Service } from "encore.dev/service";

export default new Service("slack");
```

🥐 Add a `slack.ts` file containing the following:

```ts
-- slack/slack.ts --
Expand Down

0 comments on commit 87b76d5

Please sign in to comment.