Lesson Description

The "Creating an HTTP Server" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey creates an HTTP server and starts it on port 8080. A "/health" route is created as a utility to verify that the server is up and running. A HealthCheck function is created and added to the HTTP server as a handler function for the health route. The current code can be found in the first commit in the repo.

Preview
Close

Transcript from the "Creating an HTTP Server" Lesson

[00:00:00]
>> Melkey: And now we're going to declare our server. So here we're going to do server, okay? And we are going to bring in a new package from the standard library, which will be a net/http, okay? And in here we're going to do http.server and get the struck. Okay, we define properties our HTTP server here.

[00:00:26]
Okay, so the first problem is gonna be the address. This takes a string, I'm gonna do colon, 8080, okay, so now this, this is going to run on port 8080, we're gonna have our idle timeout, okay? So I'll time it is the maximum amount of time to wait for the next request when Keepalives are enabled.

[00:00:44]
Okay, so idle timeout, we can make this to like time dot minute. So it should bring in the time package for you and it will have two more timeouts, our read timeout. So it's the maximum duration for reading the entire request, including the body. I think these are always kind of good to implement.

[00:01:05]
We can do ten times time.second, okay? So the time packaging go is a very powerful package. It allows us to specify seconds, minutes, hours, and even days. And then we have our right timeout, we'll this something like 30 times time.second, cause if anything takes longer than this, we know something is going on.

[00:01:29]
>> Melkey: Okay, so this is a definition of our server. Now, if you want to run our server, we can do error equals server.listen, and serve here. That's a method on the server struct which we just used. We can say if our error does not equal to nil, now we can do app.logger.fatal and it passes in the error like so, cool.

[00:01:55]
So let's go ahead, open up another terminal, let's go into CD, into that project. Okay, if you run, go, run main.go, if everything is working well, you should see this statement. This basically means our, we have a running HTTP server, panda back, and back. This doesn't really mean much, because you can't do anything with it just yet.

[00:02:18]
Let's go ahead and change that, let's go back to our code and let's add a health check function. Something that just tells, hey, yes, I'm receiving your request, here's the answer. We can do this very easily, we can create a new function. This is gonna be called our handler, and it's gonna be called health check, okay?

[00:02:37]
Now because we know this function itself is going to be called by our client, this could be us in our second terminal, could be someone calling it from a UI, could be another service, we don't know, we don't really care. But because we know this function is going to be an HTTP handler, it has to have a specific signature and by that, I mean it's gonna accept two arguments.

[00:03:05]
The first one is typically WW and it is HTTP response writer.
>> Melkey: And the response writer is an interface used by an HTTP handler to construct an HTTP response. So this is how we communicate back to the caller if we have to, we use w. The next argument is r, which is gonna be a pointer to HTTP request.

[00:03:31]
A request represents an HTTP request received by a server or to be sent by a client. So this is what we get, this is what the caller is sending us. And this has a bunch of metadata the ones we typically are curious about are like body, the body of the JSON or whatever they want, right?

[00:03:46]
Or whatever they're sending us, and then here, all we're going to do is fmt.Fprint, okay? Fprint is specifically used to send back data to our HTTP client, and we can pass in W our HTTP responses writer, and here we can just say status is available.
>> Melkey: Very simple function, nothing too crazy.

[00:04:14]
Now my question is, how do we bind this? We have some random function called health check, how do we bind it to our HTTP server? The answer is using the built-in HTTP function. So we can use HTTP, okay? And we can say handler func like so or handlefunc, excuse me, handlefunc.

[00:04:35]
And handlefunc is going to receive two arguments. One is going to be the path of where this function is, and then the second argument is what the function is itself. So here, I'm gonna do /health, okay? And in go, functions are first class citizens, meaning you can pass them as variables.

[00:04:56]
So we just declared our function health check. I'm gonna write this here, and instead of actually calling it, I'm just gonna remove those brackets and just pass it as a very passive as a first class citizen into our handle func.
>> Speaker 2: Why is R in the health check, a pointer, but not W?

[00:05:14]
>> Melkey: Yeah, so the reason why R is a pointers and not W, R is a pointer because R is going to contain data from our client. And in this particular instance, it doesn't matter, right? But we'll see as we go on with the use of, let's say, middleware, the use of modifying what R could possibly hold with the body and like encoding it.

[00:05:39]
We want it to be a pointer to persist the data that they send versus data that we respond with through R, whereas W doesn't actually come from our client call. W is something that we get through our HTTP server, our through our handlefunc though, we are allowed to modify and respond with whatever we want, so it doesn't have to be a pointer.

[00:05:59]
It's going to essentially live in the scope of func HealthCheck.
>> Melkey: So if we save this now, go back, let's bring down and go back up to run main.go, you can see that we are running our app. I have a typo here, and quickly open up another window, I'm gonna zoom in.

[00:06:19]
And here we can do curl, local host, 8080, and then we called it what health
>> Melkey: Can see here, now we have statuses available. So this is pretty much at this point, all green lights. We now know we have a function in go or a server that is connected to function and we are able to call it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now