Watch / Tutorial On demand
Overview

About this video

What You'll Learn

  1. Read request headers, default missing names to world, and title-case returned values.
  2. Read the request body safely with IO read all and error handling.
  3. Fetch query parameters, call outbound HTTP, and allowlist hosts in spin.toml.

Walk through the Spin Go SDK by writing an HTTP handler, reading request headers, body, and query parameters, then making outbound HTTP requests and allowlisting hosts in spin.toml.

Chapters

Jump to a chapter

  1. 0:00 Introduction
  2. 0:18 Overview of Tasks
  3. 1:13 Writing the First Endpoint
  4. 2:41 Working with Request Headers
  5. 5:17 Accessing the Request Body
  6. 8:03 Handling Query Parameters
  7. 9:20 Making Outbound HTTP Requests
  8. 10:49 Configuring Outbound Security (spin.toml)
  9. 12:00 Conclusion
Transcript

Full transcript

Generated from the English captions. Timestamps jump the player to that moment.

Read the full transcript

0:00 Introduction

0:00 Hello, and welcome back to the complete guide to spin here at Rawkode Academy. Today, we're gonna be taking a look at the Go SDK. So if you're a Go developer who wants to write WebAssembly microservices, this is the video for you. Let's crack on. Alright. So let's take a look at writing a Spin microservice with Go. For each of the SDK walkthroughs, we're gonna go through five essential tasks. One, write in your first endpoint. Fortunately, this is already done for you with Spin New, but we'll go through the code and understand how the development workflow

0:18 Overview of Tasks

0:42 operates. We'll then take a look at pulling out request headers, the body, and the query parameters, all very common tasks when working with HTTP. And then, of course, the more microservices you have, eventually, one's gonna wanna talk to the big bad Internet. Whether that be talking to some remote API or communicating with other Spin microservices, you have to know how to make outbound HTTP request for Spin. So let's take a look at our first endpoint. We can open main.go, and we will see that we pull in the Spin HTTP SDK. We have an empty main function, which you

1:13 Writing the First Endpoint

1:29 don't need to worry about. Everything is gonna be handled by the spin tool chain, which under the hood is used in tiny go to compile your code to a WebAssembly module. Then in our edit function, we set up an HTTP handler. A handler just takes a request and expects a response. To send that response, we have a response writer. All the spend SDKs are as idiomatic as possible to the language and the community that they cater for. So in Go, we have writers. You can see here, we use a writer to set an h two p header, content

2:09 type text plain before using format f print f to write to our writer, hello, Birmingham. Your local build process is spin build, spin up, and it's usually a good measure to add follow all so that you can get the logs from your module. From here, let's run curl local host 3,000, where we get hello, Birmingham. Let's take a look at grabbing those HTTP headers. So let's go back to our main dot go. Before we do any response stuff, we wanna grab a header. We'll do something trivial with the value, and we'll send it back on the response.

2:41 Working with Request Headers

3:02 So let's set name to be equal to r dot header dot get, and we'll request the x name value. Now there's no error if this key doesn't exist. So we're gonna build on our own safety blanket and if the name does not exist, we're gonna set it to world. And then let's swap the f print f, the f print line to be f print f, where we'll put in our own new line, drop in our interpolation syntax, and your name. Next, let's add a new header. And this time, we'll set x name, and let's do something with the name before

3:58 we return it. Let's pull in strings dot to title case name. We're gonna jump back to the terminal, run spin build up follow all. We'll be seeing that quite a lot. Before running curl http localhost three thousand. Now because we're not providing the x name header just yet, we should see hello world. Perfect. So let's include our header. I'm also going to use dash b so that we can see the headers on the response. We'll provide our x name and a random casing of David. We now get the x name header and a response with David in uppercase,

4:59 and we have hello, David on the body response. Working with headers with the Spin Go SDK, we just call it get function, pass in a name, we get a value back, the job is done. Nice and simple. So let's just crack right on and grab the h t p body. So let's do body equals r dot get body. This returns an IO read closer and an error. So let's make sure we check for the error condition. Now I don't like Copilot suggestion to throw a panic. So instead, we're gonna call HTTP error, where we could pass in our writer

5:17 Accessing the Request Body

5:56 and an error Let's see internal server error. Although, really, I guess, this is a status batch request because there's no body. Let's hover over body and we'll see that we have an IO read closer. So let's do body strength equals IO read all, and we'll pass in our body. Now if we take a look at this, we'll see that it returns bytes with an error. So let's make sure we're good ghost citizens, and we continue to check our error messages. Then we can do body actual string where we cast it like so. And let's just use it in a response

6:59 like so. And don't forget the interpolation marker. So let's jump back to the command line and run spin, build, up, follow. And we're going to do curl and we're gonna pass in a body and I'll just say, Rawkode and we'll send it to local host on 3,000. And we get hello world because we didn't provide the x name header. You said, hi, Rawkode. So that is how you get the body using the spin SDK. So let's clean this up a little bit. Let's just go back to saying hello and we'll get rid of the f for an

7:54 l n. We'll continue to set text plane, and we'll delete all of this. Okay. So let's do query params. This is gonna be just as easy as grabbing headers. In fact, the code is almost identical. Let's say that our name is equal to r .url.query dot get. And here, we can take the name. And just like we did with the headers, we're just gonna propagate it straight back using the x name header. We run spin build up and follow, and we do a curl. And we want to see the response headers. Local host 3,000 question mark. And because of that, we do

8:03 Handling Query Parameters

8:53 need to quote this, and we want to do name equals Rawkode. So the query helpers with the Go SDK are super convenient just like fetching the headers. We're dropping ours straight back in to HTTP response header. And as you can see, x name is Rawkode. So that just leaves us with one more thing. How do we do outbound HTTP? Let's take a look. Let's tidy up the query parameter stuff and create a new line. Don't know why I said that so slow. Next, we're going to use our spend HTTP SDK, and you can already see it has a

9:20 Making Outbound HTTP Requests

9:45 get function. From here, you can call any URL. This returns a response or an error. So like a good citizen, we'll do some error check-in. And as much as Copilot really wants me to throw a panic and going to ignore it, And we're going to return a helpful internal server error error. Now we could use IO read all on the response much like we did with the body, but it's not going to show us anything new. We can run spin, build, app, follow all. You got it. Like so. We can then run curl on local host

10:37 once the compiler is ready. So if you've seen these other SDK videos, you'll have seen that it always fails at this stage, and that's because Spin is secured by default. It will only allow you to make outbound request to domains that you've explicitly allowed. So let's update our Spin.TOML with Google.com. And so to Spin.TOML, we have the component, the trigger, and the build. Each component can individually specify the allowed HTTP host that you're allowed to communicate with. Here, we can do google.com like so. We can run back and run a spin, build, up, follow. And once that's online,

10:49 Configuring Outbound Security (spin.toml)

11:29 we hit it with the curl and the HTTP request is made without an error. Now if you don't want to be quite as secure, you don't need to specify google.com. You can also set insecure allow all. This will allow you to make outbound requests to any domain on the Internet. And that is outbound HTTP requests with the Go SDK for Vermeion Spin. Hopefully, you enjoyed this video. If so, you can let us know in the comments. If you need help, you can also let us know in the comments. We'll be back soon with more Fermion Spin

12:00 Conclusion

12:10 videos helping you build WebAssembly microservices. Until next time. We'll see you later.

Technologies featured

Weekly Cloud Native insights

Stay ahead in cloud native

Tutorials, deep dives, and curated events. No fluff.

Comments, transcript, and resources

Spin

More about Spin

View all 20 videos