WEBVTT 00:02.100 --> 00:07.470 So let's get started writing some code and we'll start with a very simple Web application, and that'll 00:07.470 --> 00:10.910 be the basis for pretty much everything we do in the remainder of this course. 00:11.370 --> 00:14.670 So I have Visual Studio code open and I don't have a project folder. 00:14.670 --> 00:22.230 So I'll open the Explorer, click open folder, go to my Visual Studio Cotes project folder and create 00:22.230 --> 00:24.290 a new directory, which I'll call Goldstrike. 00:24.990 --> 00:26.130 You can call it whatever you want. 00:26.850 --> 00:28.500 Just remember where you created it. 00:29.130 --> 00:37.700 OK, so I'll open that folder and the first thing I'll do is open a terminal window and type go mode 00:37.710 --> 00:39.930 in it and I'll give it some name. 00:39.930 --> 00:45.750 Not typically would go GitHub dot com or whatever your name is and whatever your project name is. 00:46.290 --> 00:50.710 But I'm just going to go my app because I have a different version I've committed to get out. 00:51.450 --> 00:53.130 OK, so that creates a Gombauld file. 00:54.000 --> 01:00.450 Now I'm going to create a folder at the root level of my project called CMD, and that will hold the 01:00.570 --> 01:03.210 front end, the main entry point to our application. 01:03.690 --> 01:07.770 And I'll create another folder which I'll call Internal and we'll use that one. 01:08.100 --> 01:08.970 Oh, I don't want it there. 01:09.630 --> 01:10.230 Delete that. 01:13.140 --> 01:19.860 I want it beside CMT Internal there and inside my command folder. 01:20.040 --> 01:22.020 I'll create a new folder which I'll call with. 01:22.560 --> 01:28.230 And I'm also going to create another one called API, which we won't get to for a while. 01:28.800 --> 01:35.550 But I'm going to use this single code base to produce two different binaries, one for the front end 01:35.550 --> 01:36.690 and one for the back end. 01:37.110 --> 01:40.620 And in a project as small as this, that's not absolutely necessary. 01:40.860 --> 01:42.660 But this project will grow over time. 01:42.660 --> 01:47.100 And at some point I might want to split the load between two applications, perhaps on two different 01:47.100 --> 01:47.700 servers. 01:48.090 --> 01:53.850 And this is also a great opportunity to show how you can produce multiple binaries from the same code 01:53.850 --> 01:54.210 base. 01:55.260 --> 02:00.330 So inside my Web folder, I'll create a new file, which I'll call Mango. 02:00.810 --> 02:06.120 And this will be the entry point to our Web version, the front end of our application, and I'll give 02:06.120 --> 02:07.770 it a package declaration, Maine. 02:08.610 --> 02:10.830 And I'm going to set up a couple of constants as well. 02:10.980 --> 02:14.880 The first one I'll call version, and that's just going to be a string and I'll set it to one point 02:14.880 --> 02:15.650 zero point zero. 02:16.590 --> 02:20.910 And the second one will be another constant, which I'll call txt version. 02:21.600 --> 02:23.000 And that's going to be equal to one. 02:23.010 --> 02:29.130 And what I'm going to use that for is I'm going to append that to any external txt files or JavaScript 02:29.130 --> 02:29.610 files. 02:30.000 --> 02:34.410 And when I increment it, that will force most browsers to bring the new version down. 02:34.410 --> 02:39.450 And it just saves me the trouble of having to clear the cache when things aren't working the way that 02:39.450 --> 02:40.230 I expect them to. 02:40.350 --> 02:41.610 Just a simple little trick. 02:42.780 --> 02:46.740 The next thing I'll do is I'll create a type and I'm going to call that config. 02:46.740 --> 02:49.800 And this will hold configuration information for our application. 02:50.460 --> 02:54.270 And I'm going to put a bunch of fields in here, some that will use in this section of the course and 02:54.270 --> 02:55.930 some of that we won't use until later on. 02:56.430 --> 02:59.190 So first of all, port in what port do I want to listen? 02:59.490 --> 03:00.390 And I'll make that an int. 03:01.860 --> 03:04.860 And then what environment are we in EMV? 03:04.860 --> 03:11.070 And I'll make that a string that will be like production or development and also have API. 03:11.070 --> 03:14.100 What you, Earl, do I call to my backend API. 03:14.100 --> 03:15.390 So that'll be a string as well. 03:16.740 --> 03:20.010 And I'll also create one that I'm not going to use this time around. 03:20.010 --> 03:22.890 But it's a database DB and it's just a struct. 03:22.890 --> 03:27.590 And it has one field which is called D and the data source name. 03:27.600 --> 03:29.130 How do we connect to the database? 03:29.130 --> 03:29.970 And that's just a string. 03:31.290 --> 03:35.190 We're also going to have to have stroke information and I'll make that a struct as well. 03:35.220 --> 03:36.750 And there's only two things to worry about. 03:36.750 --> 03:42.270 What is my secret key, which I'll call secret and it's a string and what is my publishable key, which 03:42.270 --> 03:44.130 I'll just call key and it's also a string. 03:45.180 --> 03:46.380 So there's my config type. 03:46.740 --> 03:52.770 Now I'll create another type that I'll use as the receiver for the various parts of my application and 03:52.770 --> 03:53.850 I'll just call it application. 03:54.150 --> 03:57.360 And again, it's a struct and it has. 03:57.510 --> 04:01.230 Well, first of all, I want to have the config of type config that we just defined. 04:02.100 --> 04:08.790 We're also going to have a couple of logger's or call one info log and it's a type pointer to log logger 04:10.760 --> 04:13.590 and I'll let the studio code do the import. 04:13.590 --> 04:19.710 And it did and I'll have an error log and it's a type log longer a pointer to it again. 04:20.790 --> 04:25.680 I'll also have, say, a template cache and we'll get to that in a while. 04:25.680 --> 04:33.210 But this is going to be a map of type string and the content of each entry will be a pointer to template 04:33.600 --> 04:38.130 template and make sure you get the one from HTML template, not text template. 04:38.130 --> 04:39.600 You want HTML templates. 04:40.230 --> 04:43.800 OK, and finally I'll just put version which is a string. 04:44.340 --> 04:46.110 OK, so there are my two types. 04:46.140 --> 04:53.010 Now let's get our main function going function and the first thing I'll do is create a variable and 04:53.010 --> 04:59.920 I'll call that variable cfg ver ciggy of type config and I want to populate that variable. 05:00.460 --> 05:06.430 With some information and I'll get some of it at least from the command line Fleck's, so we'll use 05:06.430 --> 05:14.170 the flight package flag DOD and I want first of all and infer right there, then that should have done 05:14.170 --> 05:15.680 the import for flag for me. 05:15.700 --> 05:16.570 Let's make sure it did. 05:16.570 --> 05:18.630 Yes, it did good so far. 05:18.680 --> 05:26.680 And I want to read this command line flag into my config variable and I want port and I'll call it port. 05:26.770 --> 05:28.330 That'll be the command line flag. 05:28.870 --> 05:36.450 I'll give it a default value of four thousand and a little description server port to listen on. 05:37.180 --> 05:40.660 Now duplicate that line and change this one to a stringer. 05:42.700 --> 05:50.320 And what I'm going to read in next is my environment and I'll use EMV and I'll give it a default value 05:50.320 --> 05:56.770 of, say, development and I'll give the description just application environment 06:00.640 --> 06:04.600 with a default of development or production. 06:04.960 --> 06:06.250 Those are the possible values. 06:06.640 --> 06:08.140 And I also want the API. 06:08.350 --> 06:18.640 So config API call the command line Fleg API and I'll make the default HDB colon slash slash localhost 06:19.360 --> 06:20.860 port four thousand and more 06:23.680 --> 06:28.150 and I'll call this new URL to API. 06:29.200 --> 06:31.750 OK, so there's the three things I want to read in right now. 06:31.750 --> 06:34.810 So let's pass the flags flagged up first. 06:37.060 --> 06:42.820 And now I also need to get that stripe secret key and the stripe publishable key, but I don't want 06:42.820 --> 06:44.110 to read those from the command line. 06:44.620 --> 06:53.440 The reason being when someone actually is logged into the server, they could type dash X or a U X and 06:53.440 --> 06:55.420 possibly get that secret key. 06:55.420 --> 06:57.100 And I don't want that to be available anywhere. 06:57.200 --> 07:01.990 So instead, I'll read both the stripe key and the stripe secret from environment variables. 07:01.990 --> 07:05.140 And that's pretty CFT stripe. 07:06.600 --> 07:14.920 The key is equal to and I'll use the built in OS package from the standard library OS don't get on it 07:14.920 --> 07:15.190 again. 07:15.940 --> 07:20.620 OS get on right there and I'll get stripe 07:23.380 --> 07:26.680 key and duplicate that and get the secret. 07:36.130 --> 07:41.560 So now let's set up our lawyers and I'll set up, first of all, info log and I'll just make that assign 07:41.560 --> 07:47.590 the value of from the log package dot new and the first parameters just os start standard. 07:47.800 --> 07:48.850 That's where I want to write. 07:50.980 --> 08:02.390 And I'll prefix it with info and a tab and I'll use logged on a date and a pipe and logged all time. 08:04.340 --> 08:05.720 OK, so that's my info log. 08:06.310 --> 08:07.490 Now let's create our error. 08:07.850 --> 08:17.470 Look, I'll call this one error with a tab, and I'll have logged our date logged all time and also 08:17.470 --> 08:18.850 where the error take place. 08:18.850 --> 08:26.590 So I'll add another pipe and put log dot l short file and that will give me some meaningful feedback 08:26.590 --> 08:27.370 when we have problems. 08:28.960 --> 08:32.620 Now I'm going to create a map for my template cache, which we're not using yet, but we'll get to that 08:32.620 --> 08:33.150 shortly. 08:33.340 --> 08:33.910 And that's certain. 08:33.910 --> 08:35.800 The value of make map. 08:37.870 --> 08:46.960 String and pointer to template, template and a very simple template, right template. 08:48.280 --> 08:48.730 OK. 08:51.750 --> 08:57.270 Now, let's create an application variable app to sign the value of and I'm going to use a reference 08:57.270 --> 09:03.470 to an application and I'll populate the necessary fields, the config will be equal to CFG. 09:05.520 --> 09:08.880 The info log will be equal to the full log, which we just created. 09:10.650 --> 09:20.340 The analog will be equal to error along the template cache will be equal to Tsay and final version will 09:20.340 --> 09:23.280 be equal to version. 09:24.630 --> 09:26.710 OK, so that's our app variable. 09:27.510 --> 09:33.180 Now we want to create an actual web server and I could just start the web server right here. 09:33.180 --> 09:38.010 But what I'm going to do instead is come up here and create another function and I'm going to call that 09:39.510 --> 09:45.870 func and it's going to have the receiver of app of type pointer to application and I'm going to call 09:45.870 --> 09:48.000 it server. 09:50.280 --> 09:53.310 And up here I'll create a variable called Salvy. 09:55.740 --> 10:02.700 Should they should return an error potentially there, serve as a sign the value of and I'm going to 10:02.700 --> 10:07.890 call from the HTP package HTP DOD server. 10:09.990 --> 10:10.500 I thought. 10:12.310 --> 10:22.030 And I'll populated with things like the address, which will be used use format as print F and it will 10:22.030 --> 10:32.870 be a string followed by a column followed by the D and I'm a substitute app config part. 10:33.970 --> 10:37.850 OK, so that's the address handler will get to in a minute. 10:37.870 --> 10:41.020 So I need to have a handler and I'll just put it in. 10:41.020 --> 10:42.540 What's going to be there Abda. 10:43.190 --> 10:48.080 It doesn't exist yet but it will shortly and I'll give it some nice default idle time out. 10:48.130 --> 10:59.230 I'll set that to say 30 seconds, 30 times time dot second and I'll have the read time out and I'll 10:59.230 --> 11:02.580 set that to say 10 seconds time dot second. 11:02.830 --> 11:10.780 That seems logical and I'll give it a read header timeout and I'll set that to five seconds, five times 11:10.780 --> 11:11.830 time dot second 11:14.590 --> 11:16.900 and finally we'll give it right timeout. 11:18.460 --> 11:23.290 Five times timed second, and that's more than adequate for our purposes. 11:23.410 --> 11:31.510 OK, and I'll print something to the log using my app DOT info log and I'll print line and I'll just 11:31.510 --> 11:42.850 say starting HDP server on porked percent and app dot config port. 11:43.360 --> 11:53.590 Actually let's put this as well, HDP server in s mode and we'll prefix that with APTA config dot. 11:55.010 --> 11:56.000 It's on. 11:58.970 --> 12:01.240 And that's in part OK. 12:01.350 --> 12:10.640 So is this a message and I will actually start the server, return served, listen and serve and that 12:10.640 --> 12:16.370 will return no error if everything starts the way it's supposed to and it'll return an error if something 12:16.370 --> 12:17.850 fails to start up for some reason. 12:18.410 --> 12:20.510 Now we need to set up some application routes. 12:20.870 --> 12:25.510 So in my Web folder, I'll create a new file, which I'll call Routes Duco. 12:28.130 --> 12:35.120 And it's in Package Maine, and what I'm going to do is open my terminal and import a nice rooting package 12:35.120 --> 12:39.140 and I want I'm going to use is Pachi Road, so I'll just go get that. 12:39.250 --> 12:42.800 Go get GitHub dot com slash. 12:42.800 --> 12:45.350 Go Ducci slash. 12:46.610 --> 12:47.110 Cheese. 12:47.750 --> 12:51.950 And I'm going to get version five if there's a later version out, you may want to use that. 12:51.950 --> 12:53.180 But if you want to follow along. 12:53.180 --> 13:00.710 Exactly, use version five that goes and gets it adds it to our government file or close the terminal 13:01.130 --> 13:04.520 and up here or create a new function roots. 13:05.630 --> 13:08.270 And it has the receiver of application. 13:13.940 --> 13:22.040 And it returns as it has to an http dot handler and I'll create a new router, which I'll call Mux for 13:22.040 --> 13:26.000 multiplexed, and that's a sign the value of Ketut new router. 13:31.880 --> 13:36.640 And I'll just say return mux for now and make sure that everything looks the way that it should. 13:36.680 --> 13:42.200 So we have this GOCE like this version five. 13:44.540 --> 13:45.080 That's fair. 13:45.250 --> 13:50.480 OK, so now if I go back to my main dock, go, is there an error now? 13:53.130 --> 14:00.030 There is I'm not using the app variable down here, I simply say there is a sign, the value of app 14:00.180 --> 14:00.840 to surf. 14:02.850 --> 14:11.820 And check for error, and this has to be an assignment operator, if error is not equal to nil, then 14:11.820 --> 14:16.800 I'll say Abdol error log dot, print one. 14:19.710 --> 14:25.350 The air print the air and I'll just say logged off, we'll get to log entries, but that's fine. 14:26.500 --> 14:31.600 OK, so this should actually compile, shouldn't do anything, but it should actually run. 14:31.620 --> 14:32.190 So let's try. 14:32.190 --> 14:39.090 It will open our terminal window, clear the screen and type go run dot com slash web. 14:41.410 --> 14:42.080 And there it is. 14:42.100 --> 14:43.720 It started up the application server. 14:43.780 --> 14:49.240 OK, so that's enough to give us a framework that we can put some roots in and start serving some pages 14:49.420 --> 14:51.840 and we'll get started on that in the next election.