Build Go Apps That Scale on AWS

Creating the App struct

Melkey

Melkey

Twitch
Build Go Apps That Scale on AWS

Check out a free preview of the full Build Go Apps That Scale on AWS course

The "Creating the App struct" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:

Melkey creates the App struct, initializing a new DynamoDB client and API handler. The App is initialized in the top-level main.go file. The project structure is now completely bootstrapped.

Preview
Close

Transcript from the "Creating the App struct" Lesson

[00:00:00]
>> Now we're still gonna see that same error in the API. It's still gonna be the exact same error, I actually did not solve that error whatsoever. But I'm gonna walk us through keeping that error and then going through app and passing that error down. Okay, so here, we're gonna have the exact same DynamoDBClient.

[00:00:16]
So, here our dbStore is database.DynamoDBClient, which is this struct here. Can go back, and here, instead of giving our dbStore as a string, could do database DBClient. And here, if we open up dbStore, there you go, now it's satisfied. With the typing from dbStore initializing that DynamoDBClient here, okay?

[00:00:46]
But then the question remains of where do we actually call this new API handler to pass in the dbStore actual initialization and the actual declaration of it, right? And that's all gonna happen in the app.go. So if you go up here, go all the way up to app.go.

[00:01:02]
I'm gonna do package app, okay? And I'm gonna do an App type, App is a struct, and this App struct is gonna contain one thing. If we kinda continue our scheme of layers, right, our API layer is calling our database layer, then our app layer is gonna call our API layer.

[00:01:21]
So here we're gonna do ApiHandler, and it's important you do capitalize it. I'm gonna do import, it's gonna be lambda-func/api. And this type's gonna be api.ApiHandler. Okay, so you can see, we basically are doing the exact same thing three times. But every layer has a particular purpose. DB layer, API layer and the actual app initialization layer.

[00:01:47]
Let's do a func NewApp, it's not gonna take any arguments, it's gonna return instead, an App struct. And in here, this is where we actually initialize our dbStore. And that initialization gets passed down into the API handler. Cool, so let's go ahead and do that, so first we need to not only bring in the API, let's go ahead and bring in the lambda-func/database, okay?

[00:02:20]
What's going on here? Typo, cool. We go down here, let's initialize our DB, so we're gonna do dp is gonna be database.NewDBClient. I'm gonna call this function, okay? Not DBClient, NewDynamoDBClient, cool. I'm gonna put apiHandler is going to be api.NewAPIHandler, and expects an argument, which is our db, okay?

[00:02:55]
So you can see here, we're kinda doing this piping down our db initialization into our API handler, and the type satisfies what we expect. Our database.DynamoDBClient is what we expect this type to be. And because we're initializing it and it's returning this DynamoDBClient, that's why we can do this, okay?

[00:03:17]
And then lastly, we need to turn app, and in here is ApiHandler to apiHandler. So now we've basically constructed our project structure. We have this app, and our app is going to have access to one field, which is ApiHandler. And that ApiHandler is gonna have access to our database field.

[00:03:42]
So we can do .dbStore and modify our values in our DB or any kind of interaction we have with our database. But the question remains what do we do with app? Where do we actually call, we've called everything else. We've called the client, we've called the ApiHandler, where do we call app?

[00:03:59]
So we'll actually call this in our function main. And right now it's not gonna do anything. But you can do myApp. You can do, let's say you have to bring it in, so let's pull it in. So it's lambda-func/app, can go all the way down. And here, you can put app.NewApp, okay?

[00:04:19]
It's gonna complain that it's unused, that's fine, let's ignore it for now. But we will be definitely using this, right? So now we have everything piped for the rest of our application.

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