You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 20, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: src/pages/guides/go/serverless-rest-api-example.mdx
+44-61Lines changed: 44 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,6 @@ export const description =
3
3
4
4
exportconst title_meta ='Building your first API with Go and Nitric'
5
5
6
-
<Note>
7
-
This guide is for v0, v1 is in development. Interested in contributing or
8
-
chatting with us? [Get in touch!](https://nitric.io/chat)
9
-
</Note>
10
-
11
6
# Building your first API with Nitric
12
7
13
8
## What we'll be doing
@@ -44,7 +39,7 @@ export const title_meta = 'Building your first API with Go and Nitric'
44
39
We'll start by creating a new project for our API.
45
40
46
41
```bash
47
-
nitric new my-profile-api "official/Go - Starter (experimental)"
42
+
nitric new my-profile-api go-starter
48
43
```
49
44
50
45
Next, open the project in your editor of choice.
@@ -62,13 +57,14 @@ go mod tidy
62
57
The scaffolded project should have the following structure:
63
58
64
59
```text
65
-
+--functions/
60
+
+--services/
66
61
| +-- hello/
67
62
| +-- main.go
68
63
| ...
69
64
+--nitric.yaml
70
65
+--go.mod
71
66
+--go.sum
67
+
+--golang.dockerfile
72
68
+--.gitignore
73
69
+--README.md
74
70
```
@@ -79,31 +75,23 @@ You can test the project to verify everything is working as expected:
79
75
nitric start
80
76
```
81
77
82
-
and in a separate terminal:
83
-
84
-
```bash
85
-
go run functions/hello
86
-
```
87
-
88
-
<Note>
89
-
The first command starts the Nitric Server using `nitric start`, which
90
-
provides local interfaces to emulate cloud resources. Then `go run
91
-
functions/hello` runs your functions and allows them to connect.
92
-
</Note>
93
-
94
-
If everything is working as expected you can now delete all files in the `functions/` folder, we'll create new functions in this guide.
78
+
If everything is working as expected you can now delete all files/folders in the `services/` folder, we'll create new services in this guide.
95
79
96
80
## Building the Profile API
97
81
98
-
Let's start building the profiles API. Create a file named `main.go` in the functions directory and add the following:
82
+
Let's begin by setting up the Profiles API. First, create a new folder called `profiles` within the services directory. Inside this folder, add a file named `main.go`, and include the following code:
ctx.Response.Body = []byte(fmt.Sprintf("profile with id '%s' not found", id))
@@ -217,6 +210,8 @@ profilesApi.Delete("/profile/:id", func(ctx *faas.HttpContext, next faas.HttpHan
217
210
})
218
211
```
219
212
213
+
Do a quick `go mod tidy` to make sure all new dependencies are resolved.
214
+
220
215
## Ok, let's run this thing!
221
216
222
217
Now that you have an API defined with handlers for each of its methods, it's time to test it locally.
@@ -225,15 +220,9 @@ Now that you have an API defined with handlers for each of its methods, it's tim
225
220
nitric start
226
221
```
227
222
228
-
and in a separate terminal:
229
-
230
-
```bash
231
-
go run functions/hello
232
-
```
233
-
234
223
Once it starts, the application will receive requests via the API port. You can use cURL, Postman or any other HTTP client to test the API.
235
224
236
-
We will keep it running for our tests. If you want to update your functions, just save them, they'll be reloaded automatically.
225
+
We will keep it running for our tests. If you want to update your services, just save them, they'll be reloaded automatically.
237
226
238
227
## Test your API
239
228
@@ -277,17 +266,15 @@ At this point, you can deploy what you've built to any of the supported cloud pr
277
266
-[Azure](/reference/providers/azure)
278
267
-[GCP](/reference/providers/gcp)
279
268
280
-
Next, we'll need to create a `stack`. A stack represents a deployed instance of an application, which is a collection of resources defined in your project. You might want separate stacks for each environment, such as stacks for `dev`, `test` and `prod`. For now, let's start by creating a `dev` stack.
269
+
Next, we'll need to create a `stack`. A stack represents a deployed instance of an application, which is a key value store of resources defined in your project. You might want separate stacks for each environment, such as stacks for `dev`, `test` and `prod`. For now, let's start by creating a `dev` stack.
270
+
271
+
The `stack new` command below will create a stack named `dev` that uses the `aws` provider.
281
272
282
273
```bash
283
-
nitric stack new
274
+
nitric stack new dev aws
284
275
```
285
276
286
-
```
287
-
? What should we name this stack? dev
288
-
? Which provider do you want to deploy with? aws
289
-
? Which region should the stack deploy to? us-east-1
290
-
```
277
+
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region, let's use `us-east-1`.
291
278
292
279
### AWS
293
280
@@ -297,10 +284,6 @@ We called our stack `dev`, let's try deploying it with the `up` command
0 commit comments