React Native & Golang tutorial | #1 Fiber setup

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so let's start by building our web server I'm going to initialize a new mod in here go mod in it go gab.com that but I'm going to use my name but you can use yours obviously so let's just say Che it booking project vew one let's now create a couple of folders let's see uh Bin it's going to be where we dump our binaries let's see uh CMD and under CMD API because that's where we are going to put our entry point which is the main. go let's also create repositories both the toies handlers and modeles all right so let's create a couple of files now so let's touch CMD API main. go and we are going to start building everything related to the events handlers event. go models event go and repository repositories event go so let's go to oh yeah now we should also uh create the make file because that's what we are going to use to make our life easier when it comes to building the binaries and running the binaries so let's let's put a two commands in here let's say build and for the build we want go build the output is going to be bin and the entry point the the source is going to be CMG API main. and we also want to say start that's going to be being Main so let's now maybe start with the modules let's say well package models and we're going to have the event in here each event will obviously have a 90 and it's going to be a string it's going to have a name location it's going to have a date we just be time and we also have uh created at and updated that for the next lessons we will obviously do the object mapping like that and also for gorm because we are going to use gorm to in order to use post grass but that's not necessary for this lesson let's do in on the next one okay so let's just say type event reposit am I writing this correctly repository yeah this is going to be an interface and we are going to have a couple of methods in here let's start with G manyu we need to pass a context to here so for the G manyu we don't have to pass anything else and this one is going to return slice of pointers to event and an error oh yeah we don't need this so let's copy this we also will have get one but for this one we need to pass an event ID will be just say string and we just return an event this will also return an event because this one is to create one and to create one we just pass an event all right so for the for the model that's fine that's all we need for now so let's go to maybe our Handler on repositories all right so for our repositories let's say package repositories and we want to initialize a function here which will be new event repository so let's say on the event repository which will just initialize all of the methods that we interact with the database we need to pass obviously uh the database which will be a pointer to the database but as we don't have any configuration for the database right now let's just say DB any we will obviously change this later and this function will return an event repository we need to we need to return uh the initializer for the event repository if you remember we need to we need to Define these functions in here so what we want to do is the following type event repository is going to be this struct and this is going to receive ADB we will obviously change this later but let's just say any just for now this is going to be a pointer to event repository which will receive a GB which is just the GB we passing here and this is going to complain now Cu uh this should have this these are this is missing the methods we we have defined on the EV repository so we need to create them so let's do this now they will obviously be method receivers so what we need to do is the following let's just say R for repository this is going to be appointed to the event repository let's say get many and this is going to have context let's just say context and this is going to return my slice of pointers to the to event and to return new for now and let's create the other two ones which are G one and we we need to pass the event ID in here and this is going to return this uh pointer to the event and create one which will return the same all right so that's complaining oh yeah guys so we have something wrong in here we are missing the event we need to have the events to create that so let's defining here the event and this needs to be modeles event so that's good for the repository still on this lesson we are going to test uh one of these end points just to make sure we can this is working fine uh with some mock data obviously because we are not yet connected to the database but let's uh go now to the handlers all right so for the handlers we do need to have fiber installed so make sure you go to fiber and just cop this string let's install fiber so we have fiber now let's say package handlers and we are going to have something very similar to what we have in here and this is going to be new event handler and this needs to return this needs to receive a fiber router let's say just routering here and this will also need to receive a repository which is going to from the modules the event repository and we want to create in here the Handler and similarly to what we have in here let's create a new type in here which will just be event handler which is a stct and this is going to have a repository models event repository so we can use that in here oops event handler and let's pass the repository in here and now we can as we have fiber in here the router we can start defining our endpoints router get and this one we need to pass Handler and what we want to do in here is basically but the functions that our Handler we have as method receivers we don't have those defined yet we do that in a moment let's just type those in here for now let's see get many and we want two more this one is going to be for post post one which is let's call create one actually and for this one it's going to be a get but we're going to have access to the event ID from the per so we can get one all right so now let's create our method receivers which will be handlers app pointer to event handler let's say get many and each of these functions in here as they are uh handlers under this endpoint they will always receive uh fiber context so let's see context fiber. context and we do need to say that this is a a pointer to fiber context and they will always return an error for now let's just say new uh uh we are going to change this in a moment to test but let's just get in place our other uh two handlers let's say get one let's say create one let's say context cancel and this is going to be context with timeout and for the time for the base context actually let's just pass context. background and we want to pass the duration here let's say time. duration this can be five time the second and we obviously need to defer cancel cancel so what we're going to do is the following events error let's see error from our Handler we will have access to repository which will have access to get many and we need to pass context and if error is not new error some extra spacing here we're going to say the context this one one is for is from fiber status let's say um fiber bad getaway status bed getaway yeah that's enough and we want to return a Json and for the Json we do need to pass fiber map and let's say status fail and a message uh what can we see here let's just say error all right that's if something goes wrong but if everything goes right what we're going to do is return context status status okay and we're going to send adjon just like we did before fiber. map map and this is going to be success we don't need any messaging here and for the data let's return the event all right so this is not going to return anything for now cuz we don't have anything being returned from G many from the repository so what we're going to do is just return some mock data in here so let's say events and this is going to be a slice of pointers to model event and our event let's just append a new event models event and it's an ID let's just give a mock ID and it's a name let's say my favorite band uh location obviously I don't know somewhere there and we need a date which can be time. now and for the created at let's just give the same created at updated at all right so now we can just return the event and no errors so that's basically how everything is going to work throughout this project I mean reg related to the the to the handlers and the repositories but this is not going to work cuz we have one thing missing which is the entry point we did not Define the entry point yet so let's do that now let's see package main we are going to have a main function here which is the entry point to our application let's see app fiber new and we want to passeng here a new config so for the app name let's say to get booking uh yeah that's fine let's say also server header fiber all right so we want to have our repositories we also want to put our handlers in here our initializers obviously so let's create a new event repository all right so we need our routing obviously we want to initialize a new event handler which needs to receive this server we need a router in here but what we're going to do is basically server we want to group that under the event in here if you remember in here uh we have this structure so this slashes in here they need to be uh under the event end point so that's what we are doing we can just Nest routers in fiber and we also need to P the repository which is just the event repository all right so let's now photo app to to listen and with our make file we can run now uh start which is going to build and then run the binary so let's do it and we have an issue oh yeah we forgot to put building here when we start we want first to build and then to run the binary let's run that again all right so f is running let's just grab this end point all right so let's let's do a mark requesting here uh this needs to be API all right so we have that in here API event which will return for us where is that again that's under the stories we should we should have this on the response so let's test it out all right so that's looking good for now and on the next lesson we are going to start using Docker because we want to set up our database uh with Docker and then also start using goang air because we don't want to to have to always stop the process and then start it again every time we do any changes in the code base so that's it for now see you on the next one
Info
Channel: Vaillant
Views: 153
Rating: undefined out of 5
Keywords: reactnative, android, ios, programming, go, golang, mobile development, software engineering, app development, programming tutorials
Id: waedNC25CBQ
Channel Id: undefined
Length: 20min 22sec (1222 seconds)
Published: Fri Jun 21 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.