Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 3a8777a

Browse files
committed
Update go docs and examples
1 parent fe8f50a commit 3a8777a

40 files changed

+383
-689
lines changed

src/pages/apis.mdx

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ package main
4343
import (
4444
"fmt"
4545

46-
"github.com/nitrictech/go-sdk/handler"
46+
"github.com/nitrictech/go-sdk/nitric/apis"
4747
"github.com/nitrictech/go-sdk/nitric"
4848
)
4949

@@ -54,17 +54,13 @@ func main() {
5454
return
5555
}
5656

57-
galaxyApi.Get("/moon", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
57+
galaxyApi.Get("/moon", func(ctx *apis.Ctx) {
5858
ctx.Response.Body = []byte("that's no moon, it's a space station.")
59-
60-
return next(ctx)
6159
})
6260

6361
if err := nitric.Run(); err != nil {
6462
fmt.Println(err)
6563
}
66-
67-
fmt.Println("Service has started")
6864
}
6965
```
7066

@@ -128,7 +124,7 @@ package main
128124
import (
129125
"fmt"
130126

131-
"github.com/nitrictech/go-sdk/handler"
127+
"github.com/nitrictech/go-sdk/nitric/apis"
132128
"github.com/nitrictech/go-sdk/nitric"
133129
"github.com/your-org/your-repo/planets"
134130
)
@@ -140,25 +136,19 @@ func main() {
140136
return
141137
}
142138

143-
galaxyApi.Get("/planets", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
139+
galaxyApi.Get("/planets", func(ctx *apis.Ctx) {
144140
ctx.Response.Headers = map[string][]string{"Content-Type": {"application/json"}}
145141
ctx.Response.Body = []byte(GetPlanetList())
146-
147-
return next(ctx)
148142
})
149143

150-
galaxyApi.Post("/planets", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
144+
galaxyApi.Post("/planets", func(ctx *apis.Ctx) {
151145
CreatePlanet(ctx.Request.Data())
152146
ctx.Response.Status = 201
153-
154-
return next(ctx)
155147
})
156148

157149
if err := nitric.Run(); err != nil {
158150
fmt.Println(err)
159151
}
160-
161-
fmt.Println("Service has started")
162152
}
163153

164154
```
@@ -222,7 +212,7 @@ package main
222212
import (
223213
"fmt"
224214

225-
"github.com/nitrictech/go-sdk/handler"
215+
"github.com/nitrictech/go-sdk/nitric/apis"
226216
"github.com/nitrictech/go-sdk/nitric"
227217
"github.com/your-org/your-repo/planets"
228218
)
@@ -235,19 +225,15 @@ func main() {
235225
}
236226

237227
// create a dynamic route and extract the parameter `name`
238-
galaxyApi.Get("/planets/:name", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
228+
galaxyApi.Get("/planets/:name", func(ctx *apis.Ctx) {
239229
name := ctx.Request.PathParams()["name"]
240230

241231
ctx.Response.Body = []byte(GetPlanet(name))
242-
243-
return next(ctx)
244232
})
245233

246234
if err := nitric.Run(); err != nil {
247235
fmt.Println(err)
248236
}
249-
250-
fmt.Println("Service has started")
251237
}
252238
```
253239

@@ -290,11 +276,9 @@ async def find_alderaan(ctx):
290276

291277
```go
292278
// return a redirect response using status 301
293-
galaxyApi.Get("/planets/alderaan", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
279+
galaxyApi.Get("/planets/alderaan", func(ctx *apis.Ctx) {
294280
ctx.Response.Status = 301
295281
ctx.Response.Location = "https://example.org/debris/alderaan"
296-
297-
return next(ctx)
298282
})
299283
```
300284

@@ -552,15 +536,13 @@ async def get_planet(ctx):
552536

553537
```go
554538
// override top level security to remove security from this route
555-
secureApi.Get("/planets/unsecured-planet", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
539+
secureApi.Get("/planets/unsecured-planet", func(ctx *apis.Ctx) {
556540
// Handle request
557-
return next(ctx)
558541
}, nitric.WithNoMethodSecurity())
559542

560543
// override top level security to require user.write scope to access
561-
secureApi.Get("/planets/unsecured-planet", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
544+
secureApi.Get("/planets/unsecured-planet", func(ctx *apis.Ctx) {
562545
// Handle request
563-
return next(ctx)
564546
}, nitric.WithSecurity(customRule([]string{"users:write"})))
565547
```
566548

@@ -607,7 +589,7 @@ async def validate(ctx, nxt: HttpMiddleware):
607589
```
608590

609591
```go
610-
func validate(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
592+
func validate(ctx *apis.Ctx, next nitric.Handler[apis.Ctx]) (*handler.HttpContext, error) {
611593
if ctx.Request.Headers()["content-type"] != nil {
612594
ctx.Response.Status = 400
613595
ctx.Response.Body = []byte("header Content-Type is required")
@@ -654,11 +636,11 @@ Nitric.run()
654636
import (
655637
"fmt"
656638

657-
"github.com/nitrictech/go-sdk/handler"
639+
"github.com/nitrictech/go-sdk/nitric/apis"
658640
"github.com/nitrictech/go-sdk/nitric"
659641
)
660642

661-
func validate(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
643+
func validate(ctx *apis.Ctx, next nitric.Handler[apis.Ctx]) (*apis.Ctx, error) {
662644
if ctx.Request.Headers()["content-type"] != nil {
663645
ctx.Response.Status = 400
664646
ctx.Response.Body = []byte("header Content-Type is required")
@@ -719,11 +701,11 @@ Route level middleware currently not supported in python
719701
import (
720702
"fmt"
721703

722-
"github.com/nitrictech/go-sdk/handler"
704+
"github.com/nitrictech/go-sdk/nitric/apis"
723705
"github.com/nitrictech/go-sdk/nitric"
724706
)
725707

726-
func validate(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
708+
func validate(ctx *apis.Ctx, next nitric.Handler[apis.Ctx]) (*apis.Ctx, error) {
727709
if ctx.Request.Headers()["content-type"] != nil {
728710
ctx.Response.Status = 400
729711
ctx.Response.Body = []byte("header Content-Type is required")
@@ -742,8 +724,8 @@ func main() {
742724
return
743725
}
744726

745-
customersApi.Get("/customers", handler.ComposeHttpMiddleware(validate, func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
746-
return next(ctx)
727+
customersApi.Get("/customers", nitric.Compose(validate, func(ctx *apis.Ctx) {
728+
// handle request
747729
}))
748730

749731
if err := nitric.Run(); err != nil {
@@ -917,7 +899,7 @@ package main
917899
import (
918900
"fmt"
919901

920-
"github.com/nitrictech/go-sdk/handler"
902+
"github.com/nitrictech/go-sdk/nitric/apis"
921903
"github.com/nitrictech/go-sdk/nitric"
922904
)
923905

@@ -928,9 +910,8 @@ func main() {
928910
return
929911
}
930912

931-
accountsApi.Get("/users/:id", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
913+
accountsApi.Get("/users/:id", func(ctx *apis.Ctx) {
932914
// your logic here
933-
return next(ctx)
934915
})
935916

936917
if err := nitric.Run(); err != nil {
@@ -983,7 +964,7 @@ package main
983964
import (
984965
"fmt"
985966

986-
"github.com/nitrictech/go-sdk/handler"
967+
"github.com/nitrictech/go-sdk/nitric/apis"
987968
"github.com/nitrictech/go-sdk/nitric"
988969
)
989970

@@ -994,9 +975,8 @@ func main() {
994975
return
995976
}
996977

997-
accountsApi.Get("/orgs/:id", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
978+
accountsApi.Get("/orgs/:id", func(ctx *apis.Ctx) {
998979
// your logic here
999-
return next(ctx)
1000980
})
1001981

1002982
if err := nitric.Run(); err != nil {
@@ -1096,15 +1076,14 @@ package main
10961076
import (
10971077
"fmt"
10981078

1099-
"github.com/nitrictech/go-sdk/handler"
1079+
"github.com/nitrictech/go-sdk/nitric/apis"
11001080
"github.com/nitrictech/go-sdk/nitric"
11011081
"github.com/your-org/your-repo/resources"
11021082
)
11031083

11041084
func main() {
1105-
Resources.AccountsApi.Get("/users/:id", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
1085+
Resources.AccountsApi.Get("/users/:id", func(ctx *apis.Ctx) {
11061086
// your logic here
1107-
return next(ctx)
11081087
})
11091088

11101089
if err := nitric.Run(); err != nil {
@@ -1152,15 +1131,14 @@ package main
11521131
import (
11531132
"fmt"
11541133

1155-
"github.com/nitrictech/go-sdk/handler"
1134+
"github.com/nitrictech/go-sdk/nitric/apis"
11561135
"github.com/nitrictech/go-sdk/nitric"
11571136
"github.com/your-org/your-repo/resources"
11581137
)
11591138

11601139
func main() {
1161-
Resources.AccountsApi.Get("/orgs/:id", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) {
1140+
Resources.AccountsApi.Get("/orgs/:id", func(ctx *apis.Ctx) {
11621141
// your logic here
1163-
return next(ctx)
11641142
})
11651143

11661144
if err := nitric.Run(); err != nil {

0 commit comments

Comments
 (0)