Tech Stack Walkthrough

Servers, routers and endpoints

Summary

In this video, I conclude our tech stack walkthrough by discussing servers, routers, and endpoints in Go. I explore web framework options like Gorilla, Fiber, and Echo, explaining why I prefer Echo for its balance of functionality and standard library compatibility. I demonstrate creating a simple server using Echo, showing how to set up a go.mod file, add dependencies, and create a basic endpoint that returns "hello world." While this is a basic example that omits features like graceful shutdown, I explain that we'll cover more advanced topics like middleware and security as the course progresses.

Transcript

We're almost done with the tech stack walkthrough. I just want to touch on one more thing, which is that of servers, routers, and endpoints. Mainly, I want to provide you with some options and also give you some terminology or at least make certain that we understand each other whenever I speak about servers and routers and endpoints. We already used a server and a router and some endpoints with the standard library where we had the listener and serve and the handler functs. We could stick with those and be fine. However, there are some options in the Go ecosystem for web frameworks that I think makes building web applications in Go a bit easier, and it provides us with some extra features that we will need in any regard. Some of those options are Gorilla, Fiber, Echo, and a lot more. For a long time, Gorilla was the default choice, but there ran into some issue with not being able to find maintainers and was overtaking in popularity during that time. They have since found maintainers, so the project is back on track. As you can see here, they have a bunch of packages and have done a lot of contribution to the Go ecosystem, and we'll be using some of these packages like the session package or the CRF package. Another option is Fiber, which if you come from a Node.js background, you'll find this one very familiar since it's inspired by Express. It is also, according to themselves, the fastest HTTP engine in Go. However, they have chosen to stray away from the standard library in terms of how to implement what is known as a context. In Go, you pass around a context struct that contains some information about the current request operation that's going on. Since they have done their own implementation, it makes it harder to incorporate it with other parts of the ecosystem in Go. So in general, you will see Go developers tending to rely as much as possible on the standard library because it is so vast and it does so much for you. So we will be using a library called Echo, which is a high-performance and minimalistic web framework. It does just enough that we don't have to reinvent the wheel, but also not too much so that it gets in our way. It sticks to the standard library, so it incorporates very well with the rest of the ecosystem. So let's create a simple server and router using Echo. To start any new project in Go, you need to create a go.mod file. And to do that, you type go mod init, give it a name. In this case, we're going to call it SRE for servers, routers, and endpoints. This creates a go.mod file. Now, we need to add some dependencies to this mod file, Go mod file, and this is how we manage dependencies in Golang. And the way to do that is with the go git command. And here, we need to go git labstack slash echo slash v4. So if we run this, we add some dependencies to our go.mod file. And if we open up here, you can see that we have some dependencies, right? Next up, let's create a main.go file. Package main. Let's just create our main function. I can't spell. Right. Now, to create our router, we say echo new, and we get some imports, and it needs to be v4. So this right here is our router, and we add endpoints to our router. So let's just add a similar one, but I get just a forward slash, takes in a handler, which is simply gonna return a string that says HTTP status, or code, and then let's just do the classic hello world. Hello world, there we go. So now we have a router, and we now, we also have an endpoint. If you had checked the echo documentation, you will see that what we have right now is very similar to that getting started example, where what they do next is called the .start method. I like to create a server struct using the HTTP package from the standard library. So simply just say HTTP dash server, not dash dot, give it an address, which is gonna be 0008080. The handler is gonna be our router. You're gonna provide it with a read timeout of 30 seconds, and a write timeout of 30 seconds. You can adjust these if you want to. This is just my standards, if you wanna say like, this is what I normally do. Finally, we can call listen and serve on our server struct. So let's do, if error equals server, listen and serve, and error does not, equal HTTP server closed, error server closed, then we panic with the error. If I jump out now and say, go run main.go, and I pull up a terminal, not a terminal, sorry, a browser, you can see that we have hello world, right? And this is like, this is of course a very verbal example. I have left out some stuff like, gracefully shutting down your server, which means that whenever the server receives a stop signal, it will try and finish any ongoing requests and shut down after those are completed, which is very nice when we do deployments. But all of these things we will get to when we start on the going live module. This was a very fast introduction to servers and routers and endpoints in Go. We still need to cover stuff like middleware and security and all of these things, right? And they will slowly come as the course progresses. But for now, whenever I talk about these things, I hope you have a better idea of what I mean.

Early Access

$95 $65 USD

Get access

Invoices and receipts available for easy company reimbursement