WEBVTT 00:01.910 --> 00:07.310 So let's get started building an invoicing micro service, and we could, if we wanted to just start 00:07.310 --> 00:11.140 an entirely new project and build our service that way. 00:11.570 --> 00:17.210 But since this is actually part of a larger application, we're going to have one loosely coupled micro 00:17.210 --> 00:21.550 service that only generates invoices as a PDF and mails them somewhere. 00:22.160 --> 00:27.950 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 00:27.950 --> 00:30.210 a new folder, which I'll call Micro. 00:30.770 --> 00:35.290 And this is where I'll put all my micro services and we're just going to have one over time. 00:35.300 --> 00:42.020 Of course, you might have more inside of that or create a new folder called Invoice, because that's 00:42.020 --> 00:44.530 the one thing that our micro service is going to do. 00:45.110 --> 00:48.950 And inside of that, I'll create a new file, which I'll call Invoice Dutko. 00:50.330 --> 00:53.380 And of course, that will be the main entry point of our application. 00:53.510 --> 00:56.030 So it has a package and it's the main package. 00:57.140 --> 01:01.880 And we'll do similar things that we did in the API one, but much less so. 01:01.880 --> 01:07.550 Let's open up the API folder and open up its API Dongo, which is its main entry point. 01:08.130 --> 01:16.850 And what I'll do is hide my files and split this screen and have invoice on the left and API on the 01:16.850 --> 01:17.110 right. 01:17.360 --> 01:19.100 So I probably want a version. 01:19.310 --> 01:23.770 So I'll put CONSED version equals I'll make it one point zero point zero. 01:24.770 --> 01:26.960 That's just a version of this particular application. 01:27.680 --> 01:30.320 And we're going to want to type we're also going to want to configure. 01:30.350 --> 01:34.730 It'll be not as sophisticated as the one for API. 01:34.850 --> 01:37.480 Then I'll just copy it as a starting point and paste it in. 01:38.090 --> 01:38.780 I'm definitely going. 01:38.780 --> 01:39.380 I want a part. 01:39.530 --> 01:43.820 I don't care about the environment so I'm not going to read anything from environment variables. 01:44.120 --> 01:46.310 I'm not even going to use the database in this one. 01:46.310 --> 01:47.810 So I'll delete that entirely. 01:47.930 --> 01:52.820 And I'm also not going to be charging credit cards or anything like that, but I am going to be sending 01:52.820 --> 01:57.230 email and that email is probably going to have a link to the front end. 01:57.590 --> 01:59.240 So the front end. 01:59.240 --> 02:04.300 But I don't care about the secret key, so our config type is much, much simpler. 02:05.390 --> 02:12.220 Now, we're also going to have a type for application so we can share data between handlers and so forth, 02:12.500 --> 02:18.570 so I'll copy and paste that and I will want log let me do the import here just by typing log and hitting 02:18.590 --> 02:19.090 return. 02:19.460 --> 02:20.750 And that was the import for me. 02:21.530 --> 02:23.690 But I'm not going to be using the database, so we'll get rid of that. 02:24.760 --> 02:27.380 So already this is a much simpler application. 02:27.610 --> 02:30.170 OK, and we're going to have a main function. 02:30.350 --> 02:35.090 So me, we have to have that. 02:35.090 --> 02:40.190 There's no question and I will be reading certain things from command line parameters. 02:40.460 --> 02:46.610 So let me copy all of this and just remove the parts I don't need, so I'll pass that in there. 02:47.210 --> 02:48.770 So we need to import flag 02:52.020 --> 02:52.910 that again. 02:52.960 --> 02:57.020 That flag now apparently is not going to import it for me. 02:57.050 --> 02:59.660 Let's try this one flight. 03:02.330 --> 03:03.540 Well, we'll mandoline for it. 03:03.560 --> 03:04.310 That's no problem. 03:06.290 --> 03:10.250 So up here in our imports, we'll just wrap this whole thing in parentheses. 03:13.120 --> 03:14.290 And put a flag in the. 03:21.610 --> 03:21.990 But. 03:23.440 --> 03:27.610 Now, we don't need this environment one, so we'll delete that because we're not going to care about 03:27.610 --> 03:30.670 whether it's in development or production, it will either be on or off. 03:31.000 --> 03:32.550 And we don't have a database. 03:32.560 --> 03:34.510 We're not going to be using one with this micro service. 03:34.510 --> 03:39.430 Many micro services do use a database, but we're just demonstrating how to build a very simple one 03:39.430 --> 03:41.310 that does one thing and one thing only. 03:41.320 --> 03:42.130 So I'll delete that. 03:43.060 --> 03:48.550 We obviously need all the mail stuff because we'll be sending email and we don't need a secret key, 03:49.180 --> 03:50.350 but we do need the front end. 03:50.350 --> 03:55.210 And these are all suitable defaults except for this one, because we can't have two things listening 03:55.210 --> 03:56.770 on for four thousand and one. 03:57.730 --> 04:02.680 Now, one of the things you should be aware of with respect to micro services is that very rarely are 04:02.680 --> 04:07.780 they exposed to the public Internet in this situation where we're going to be generating an invoice 04:07.780 --> 04:08.400 and mailing it. 04:08.800 --> 04:15.310 There's absolutely no reason why this particular MICRA service should ever have its listening port exposed 04:15.310 --> 04:16.270 to the public Internet. 04:16.630 --> 04:22.900 Instead, our application or some part of our application will simply call this on the local network 04:22.900 --> 04:24.010 behind the firewall. 04:24.130 --> 04:26.610 So I'll just put this at Port five thousand. 04:26.680 --> 04:27.760 That'll be my default. 04:30.360 --> 04:31.840 So we have that next. 04:32.310 --> 04:37.360 What do we do next here, what we set up our lawyers, so let's copy those and we'll have to import 04:37.360 --> 04:38.250 a log, of course. 04:41.810 --> 04:50.470 And OS so long has been imported, it looks like you let's import os os and that takes care of that. 04:50.480 --> 04:52.300 So we have our info log in an error log. 04:52.880 --> 04:55.430 We're not getting a database connection, so that doesn't matter. 04:55.460 --> 04:56.570 But we want this. 04:56.720 --> 05:05.250 So let's copy all of that and paste that down here and get rid of the DB because we're not using that. 05:07.250 --> 05:09.700 And finally we call app serve. 05:09.710 --> 05:13.640 So I'll copy this and paste it in here. 05:17.610 --> 05:26.060 And of course, we don't have a serve function, so let's go find that right up here and copy it and 05:26.070 --> 05:29.260 I'll post it down here and we'll modify it as necessary. 05:29.700 --> 05:33.930 So we need to import HTTP and that takes care of that. 05:34.860 --> 05:35.760 We need format. 05:39.560 --> 05:42.990 And we don't have a route's will create that in a moment, and we do import time. 05:45.460 --> 05:45.760 Right. 05:48.280 --> 05:52.280 OK, so the only thing missing now is we have to fix this. 05:52.300 --> 05:53.940 We're not starting back in server. 05:53.950 --> 06:02.910 We're starting, say, invoicing, starting invoice, micro service. 06:03.370 --> 06:04.630 We don't have a mode. 06:05.140 --> 06:09.390 So let's delete that and we're starting it on port, whatever. 06:09.460 --> 06:10.690 So we just get rid of the part. 06:10.740 --> 06:14.710 We're referring to the environment variable and that takes care of that. 06:15.050 --> 06:16.230 So now we need to restore function. 06:16.570 --> 06:26.800 So let's close this window, open up our files and inside micro invoice, I'll create another file called 06:26.800 --> 06:28.390 Route's Don't Go. 06:29.380 --> 06:33.340 And while I'm at it, I mean, we'll create another one called New File. 06:34.180 --> 06:37.750 We'll call it invoice handlers. 06:40.240 --> 06:49.490 And actually, I want to rename this one to to invoice route's rename invoice route's. 06:50.170 --> 06:55.360 OK, so let's go to the Roots first and we'll have package made 06:58.210 --> 07:01.820 and let's open up roots Dasch API just as a starting point. 07:01.840 --> 07:10.360 So what I'll do is select all copy Close's and I'll paste it right here in invoice dash and delete pretty 07:10.360 --> 07:11.950 much everything right up here. 07:12.370 --> 07:13.900 So I'll get rid of all of these. 07:16.170 --> 07:21.420 And look what we have here, so now we have a package meeting with these imports, we're setting up 07:21.420 --> 07:25.830 a Rotar, we set up our course rules, which are fine for this case. 07:25.830 --> 07:27.360 So we just need one route. 07:27.360 --> 07:28.380 So let's create route. 07:29.220 --> 07:33.450 So we'll create a route to a handler that doesn't exist yet and we'll make it dead simple just to make 07:33.450 --> 07:35.050 sure everything works so much. 07:35.370 --> 07:47.970 Get and we'll go to say invoice, slash, create and send and we'll make that point to app dot create 07:48.090 --> 07:52.200 and send invoice which does a handler. 07:52.200 --> 08:02.520 We have to go create an invoice handler's package mean we'll create a handler and we'll make this very 08:02.520 --> 08:09.600 simple to start with func with the receiver of appointed to application and it's called Create and send 08:09.600 --> 08:16.020 invoice and it takes a response writer and a pointer to a request. 08:19.230 --> 08:28.590 And for right now we'll just say format dot f print f I better let it import format format dot f, print 08:28.590 --> 08:36.630 f and we'll just say W because it takes the response rate or as its first parameter then Hillo percent 08:36.660 --> 08:38.070 s world. 08:38.490 --> 08:40.500 I don't know why I did f print f, but there you go. 08:40.560 --> 08:42.360 And world needs to be in quotation marks. 08:44.490 --> 08:50.500 OK, so this should actually run and do something now to make life easier. 08:50.550 --> 08:51.870 Let's open up our makefile. 08:53.030 --> 08:58.220 And it will be different on Windows than it is on a Mac, but I'll post a Windows version on the course 08:58.220 --> 09:01.280 resources here that you can modify as required. 09:01.340 --> 09:02.080 So let's open this up. 09:03.050 --> 09:07.820 So the first thing I want to do is come down here and say build invoice. 09:07.820 --> 09:08.810 So I'll copy this one 09:12.260 --> 09:13.430 and I'll paste it here. 09:14.570 --> 09:27.650 And just modify it as necessary, so we'll say build, underscore, enforce, builds the invoice micro 09:27.660 --> 09:28.190 service 09:31.220 --> 09:40.940 and we'll say building, invoice, micro service and we're going to build we'll call it invoice. 09:42.110 --> 09:43.760 And we're not going to build command web. 09:43.790 --> 09:49.460 We're going to build command web, micro slash invoice. 09:50.510 --> 09:57.710 And this changes to invoice micro service DOT. 09:58.430 --> 09:59.720 OK, so we want that. 10:00.260 --> 10:03.160 And this needs to be named build invoice, of course. 10:03.170 --> 10:06.260 So it's copied and pasted here. 10:08.550 --> 10:14.760 And we want to start start Macall, start from start back and start invoice, which doesn't exist yet. 10:16.950 --> 10:18.030 So let's create that. 10:18.360 --> 10:19.650 We'll copy start front. 10:22.140 --> 10:27.840 Get rid of this tab, put in a blank line and paste and another blank line and call this start invoice 10:31.770 --> 10:40.230 starts the invoice micro service and this is called start invoice. 10:41.160 --> 10:43.620 And it depends upon build invoice. 10:46.380 --> 10:46.800 All right. 10:46.800 --> 10:56.520 So starting the invoice micro service and the bill for this, we're not using environment variables 10:56.520 --> 10:58.260 so we can get rid of that entirely. 11:02.790 --> 11:10.980 And we're just saying got dissed and we built it to invoice and we don't require anything else because 11:10.980 --> 11:19.440 our defaults are fun and change that to invoice micro service running. 11:21.320 --> 11:27.530 So we have to start in voice, we also want to stop everything, so we need to create a stop in the 11:27.530 --> 11:28.010 voice 11:31.430 --> 11:32.600 and we'll copy this 11:35.960 --> 11:39.260 and paste it in and call this stop invoice 11:43.400 --> 11:44.510 and paste it here. 11:45.200 --> 11:46.670 Stops the invoice. 11:48.830 --> 11:52.760 Micro service, stopping the. 11:55.020 --> 12:01.200 Invoice, micro service, and we're looking for invoice. 12:03.870 --> 12:05.070 And get rid of this, 12:08.280 --> 12:10.140 stop the invoice 12:12.690 --> 12:17.850 micro service, is there anything else that looks like that should be? 12:18.030 --> 12:25.290 So at this point, I should be able to open my terminal and type make start invoice and see if we get 12:25.290 --> 12:25.830 everything right. 12:27.720 --> 12:34.890 Nope, we have an area here somewhere, so ghostwrote Sumed Web Microbe's invoice, let's see, start 12:34.890 --> 12:35.640 invoice. 12:37.690 --> 12:38.920 Built in voice. 12:39.920 --> 12:46.280 And build invoices, build to dist invoice, see web micro invoice. 12:48.200 --> 12:48.950 It's not Web. 12:49.520 --> 12:55.260 It's called micro invoice, and it looks like I have an error here, an invoice Dongo. 12:55.270 --> 12:58.600 Let's see what that is while we're at it before we run anything else. 12:59.570 --> 13:02.540 And of course, it says error equals app don't serve. 13:02.540 --> 13:03.800 This should be an assignment. 13:07.100 --> 13:07.740 Let's try that again. 13:08.490 --> 13:10.640 Make start invoice. 13:14.380 --> 13:16.010 So we're built it and it's starting. 13:16.030 --> 13:20.250 Let's go try it out, so our route, let's go look at that just to make sure I get it right. 13:21.070 --> 13:26.020 It's on Port five thousand and we're going here and we should see Hello world. 13:26.020 --> 13:35.050 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 13:35.710 --> 13:41.860 Port five thousand and then paste in invoice created said Hello World. 13:42.010 --> 13:45.730 OK, so there is a Stubbe Micra service. 13:45.880 --> 13:48.430 Of course we want to do more than just print Hello World. 13:48.460 --> 13:51.930 We actually want to receive some kind of data from anywhere. 13:51.940 --> 13:57.370 We don't care where it comes from because this is a loosely coupled micro service. 13:57.370 --> 14:03.310 We want to receive that, generate a PDF invoice and mail it somewhere and we'll get started on that 14:03.460 --> 14:04.690 in the next lecture.