So let's get started building an invoicing micro service, and we could, if we wanted to just start
an entirely new project and build our service that way.
But since this is actually part of a larger application, we're going to have one loosely coupled micro
service that only generates invoices as a PDF and mails them somewhere.
I'll use the same code base and what I'll do is go over to my CMD folder right here and I'll create
a new folder, which I'll call Micro.
And this is where I'll put all my micro services and we're just going to have one over time.
Of course, you might have more inside of that or create a new folder called Invoice, because that's
the one thing that our micro service is going to do.
And inside of that, I'll create a new file, which I'll call Invoice Dutko.
And of course, that will be the main entry point of our application.
So it has a package and it's the main package.
And we'll do similar things that we did in the API one, but much less so.
Let's open up the API folder and open up its API Dongo, which is its main entry point.
And what I'll do is hide my files and split this screen and have invoice on the left and API on the
right.
So I probably want a version.
So I'll put CONSED version equals I'll make it one point zero point zero.
That's just a version of this particular application.
And we're going to want to type we're also going to want to configure.
It'll be not as sophisticated as the one for API.
Then I'll just copy it as a starting point and paste it in.
I'm definitely going.
I want a part.
I don't care about the environment so I'm not going to read anything from environment variables.
I'm not even going to use the database in this one.
So I'll delete that entirely.
And I'm also not going to be charging credit cards or anything like that, but I am going to be sending
email and that email is probably going to have a link to the front end.
So the front end.
But I don't care about the secret key, so our config type is much, much simpler.
Now, we're also going to have a type for application so we can share data between handlers and so forth,
so I'll copy and paste that and I will want log let me do the import here just by typing log and hitting
return.
And that was the import for me.
But I'm not going to be using the database, so we'll get rid of that.
So already this is a much simpler application.
OK, and we're going to have a main function.
So me, we have to have that.
There's no question and I will be reading certain things from command line parameters.
So let me copy all of this and just remove the parts I don't need, so I'll pass that in there.
So we need to import flag
that again.
That flag now apparently is not going to import it for me.
Let's try this one flight.
Well, we'll mandoline for it.
That's no problem.
So up here in our imports, we'll just wrap this whole thing in parentheses.
And put a flag in the.
But.
Now, we don't need this environment one, so we'll delete that because we're not going to care about
whether it's in development or production, it will either be on or off.
And we don't have a database.
We're not going to be using one with this micro service.
Many micro services do use a database, but we're just demonstrating how to build a very simple one
that does one thing and one thing only.
So I'll delete that.
We obviously need all the mail stuff because we'll be sending email and we don't need a secret key,
but we do need the front end.
And these are all suitable defaults except for this one, because we can't have two things listening
on for four thousand and one.
Now, one of the things you should be aware of with respect to micro services is that very rarely are
they exposed to the public Internet in this situation where we're going to be generating an invoice
and mailing it.
There's absolutely no reason why this particular MICRA service should ever have its listening port exposed
to the public Internet.
Instead, our application or some part of our application will simply call this on the local network
behind the firewall.
So I'll just put this at Port five thousand.
That'll be my default.
So we have that next.
What do we do next here, what we set up our lawyers, so let's copy those and we'll have to import
a log, of course.
And OS so long has been imported, it looks like you let's import os os and that takes care of that.
So we have our info log in an error log.
We're not getting a database connection, so that doesn't matter.
But we want this.
So let's copy all of that and paste that down here and get rid of the DB because we're not using that.
And finally we call app serve.
So I'll copy this and paste it in here.
And of course, we don't have a serve function, so let's go find that right up here and copy it and
I'll post it down here and we'll modify it as necessary.
So we need to import HTTP and that takes care of that.
We need format.
And we don't have a route's will create that in a moment, and we do import time.
Right.
OK, so the only thing missing now is we have to fix this.
We're not starting back in server.
We're starting, say, invoicing, starting invoice, micro service.
We don't have a mode.
So let's delete that and we're starting it on port, whatever.
So we just get rid of the part.
We're referring to the environment variable and that takes care of that.
So now we need to restore function.
So let's close this window, open up our files and inside micro invoice, I'll create another file called
Route's Don't Go.
And while I'm at it, I mean, we'll create another one called New File.
We'll call it invoice handlers.
And actually, I want to rename this one to to invoice route's rename invoice route's.
OK, so let's go to the Roots first and we'll have package made
and let's open up roots Dasch API just as a starting point.
So what I'll do is select all copy Close's and I'll paste it right here in invoice dash and delete pretty
much everything right up here.
So I'll get rid of all of these.
And look what we have here, so now we have a package meeting with these imports, we're setting up
a Rotar, we set up our course rules, which are fine for this case.
So we just need one route.
So let's create route.
So we'll create a route to a handler that doesn't exist yet and we'll make it dead simple just to make
sure everything works so much.
Get and we'll go to say invoice, slash, create and send and we'll make that point to app dot create
and send invoice which does a handler.
We have to go create an invoice handler's package mean we'll create a handler and we'll make this very
simple to start with func with the receiver of appointed to application and it's called Create and send
invoice and it takes a response writer and a pointer to a request.
And for right now we'll just say format dot f print f I better let it import format format dot f, print
f and we'll just say W because it takes the response rate or as its first parameter then Hillo percent
s world.
I don't know why I did f print f, but there you go.
And world needs to be in quotation marks.
OK, so this should actually run and do something now to make life easier.
Let's open up our makefile.
And it will be different on Windows than it is on a Mac, but I'll post a Windows version on the course
resources here that you can modify as required.
So let's open this up.
So the first thing I want to do is come down here and say build invoice.
So I'll copy this one
and I'll paste it here.
And just modify it as necessary, so we'll say build, underscore, enforce, builds the invoice micro
service
and we'll say building, invoice, micro service and we're going to build we'll call it invoice.
And we're not going to build command web.
We're going to build command web, micro slash invoice.
And this changes to invoice micro service DOT.
OK, so we want that.
And this needs to be named build invoice, of course.
So it's copied and pasted here.
And we want to start start Macall, start from start back and start invoice, which doesn't exist yet.
So let's create that.
We'll copy start front.
Get rid of this tab, put in a blank line and paste and another blank line and call this start invoice
starts the invoice micro service and this is called start invoice.
And it depends upon build invoice.
All right.
So starting the invoice micro service and the bill for this, we're not using environment variables
so we can get rid of that entirely.
And we're just saying got dissed and we built it to invoice and we don't require anything else because
our defaults are fun and change that to invoice micro service running.
So we have to start in voice, we also want to stop everything, so we need to create a stop in the
voice
and we'll copy this
and paste it in and call this stop invoice
and paste it here.
Stops the invoice.
Micro service, stopping the.
Invoice, micro service, and we're looking for invoice.
And get rid of this,
stop the invoice
micro service, is there anything else that looks like that should be?
So at this point, I should be able to open my terminal and type make start invoice and see if we get
everything right.
Nope, we have an area here somewhere, so ghostwrote Sumed Web Microbe's invoice, let's see, start
invoice.
Built in voice.
And build invoices, build to dist invoice, see web micro invoice.
It's not Web.
It's called micro invoice, and it looks like I have an error here, an invoice Dongo.
Let's see what that is while we're at it before we run anything else.
And of course, it says error equals app don't serve.
This should be an assignment.
Let's try that again.
Make start invoice.
So we're built it and it's starting.
Let's go try it out, so our route, let's go look at that just to make sure I get it right.
It's on Port five thousand and we're going here and we should see Hello world.
So let's go to our Web browser and open a new new browser tab and we'll go to HTP Colon's local post
Port five thousand and then paste in invoice created said Hello World.
OK, so there is a Stubbe Micra service.
Of course we want to do more than just print Hello World.
We actually want to receive some kind of data from anywhere.
We don't care where it comes from because this is a loosely coupled micro service.
We want to receive that, generate a PDF invoice and mail it somewhere and we'll get started on that
in the next lecture.