1
00:00:00,472 --> 00:00:02,105
- [Narrator] Hi and
welcome back to the course.

2
00:00:02,105 --> 00:00:04,165
In this video we're going
to be running our API

3
00:00:04,165 --> 00:00:06,163
and testing it out.

4
00:00:06,163 --> 00:00:09,845
There are a couple of things
we have to make sure of first.

5
00:00:09,845 --> 00:00:11,807
The first one, we have to make sure

6
00:00:11,807 --> 00:00:15,413
to delete the configuration
property of nginx.

7
00:00:15,413 --> 00:00:17,891
That is the default.

8
00:00:17,891 --> 00:00:22,058
So if we go and do sudo
rm/etc/nginx/sitesenabled/default.

9
00:00:32,496 --> 00:00:35,798
You have to make sure to remove
the default configuration

10
00:00:35,798 --> 00:00:39,965
file for nginx, or else whenever
you try to access your API,

11
00:00:40,889 --> 00:00:42,936
the first thing it's gonna
do is it's gonna give you

12
00:00:42,936 --> 00:00:46,729
a 404 not found, because the
default configuration property

13
00:00:46,729 --> 00:00:49,809
for nginx will be the
first one that gets loaded.

14
00:00:49,809 --> 00:00:53,226
So make sure to delete the default there.

15
00:01:15,041 --> 00:01:17,764
Once we've done that,
which I've done already,

16
00:01:17,764 --> 00:01:21,264
we have to make sure to do sudo systemctl,

17
00:01:22,566 --> 00:01:23,649
reload nginx.

18
00:01:25,736 --> 00:01:28,908
And then sudo systemctl restart nginx.

19
00:01:28,908 --> 00:01:30,857
That will reload the
configuration for nginx

20
00:01:30,857 --> 00:01:32,774
and then restart nginx.

21
00:01:33,809 --> 00:01:37,673
Okay, now that we've restarted nginx,

22
00:01:37,673 --> 00:01:41,840
and make sure to have sudo
systemctl start uwsgi_items_rest

23
00:01:46,153 --> 00:01:47,323
and make sure to have it started.

24
00:01:47,323 --> 00:01:49,363
In my case it's already started.

25
00:01:49,363 --> 00:01:51,668
But make sure to have it started.

26
00:01:51,668 --> 00:01:55,581
Now that nginx is
running uwsgi is running,

27
00:01:55,581 --> 00:01:58,332
uwsgi will load our Flask
app, so we don't have

28
00:01:58,332 --> 00:02:00,771
to start it ourselves,

29
00:02:00,771 --> 00:02:02,043
and everything should be good to go.

30
00:02:02,043 --> 00:02:05,766
And Posdres is also running
in the background as well.

31
00:02:05,766 --> 00:02:06,599
Now okay?

32
00:02:07,743 --> 00:02:10,890
Let's go over to Postman and give it a go.

33
00:02:10,890 --> 00:02:13,942
So I'm going to open the
section six collection

34
00:02:13,942 --> 00:02:16,842
because it's gonna be the same
collection as section six,

35
00:02:16,842 --> 00:02:17,852
we've not changed anything yet,

36
00:02:17,852 --> 00:02:22,022
we've used the same API for
Heroku, and Digital Ocean.

37
00:02:22,022 --> 00:02:24,340
And we're gonna make
sure to go over to the

38
00:02:24,340 --> 00:02:29,238
environments and change the
URL to be our service URL.

39
00:02:29,238 --> 00:02:32,004
You can find out the server
URL in Digital Ocean,

40
00:02:32,004 --> 00:02:34,886
the Digital Ocean console,
sorry the control panel,

41
00:02:34,886 --> 00:02:37,530
or if you're on a Mac
you'll see it up here,

42
00:02:37,530 --> 00:02:39,280
sshjose@95 85 15 100.

43
00:02:42,356 --> 00:02:46,523
So, what I'm gonna do here
is put 95, 85, 15, 100.

44
00:02:48,442 --> 00:02:50,348
Also make sure that
the protocol being used

45
00:02:50,348 --> 00:02:52,265
is http, and not https.

46
00:02:54,038 --> 00:02:56,201
Remove the s if it is there,

47
00:02:56,201 --> 00:03:00,481
because we have not set
up ssl for our server yet,

48
00:03:00,481 --> 00:03:03,268
and therefore if we put
https it wouldn't work, so.

49
00:03:03,268 --> 00:03:04,101
Go ahead.

50
00:03:06,292 --> 00:03:07,914
And then let's give it a go.

51
00:03:07,914 --> 00:03:11,569
I'm gonna try to register first of all.

52
00:03:11,569 --> 00:03:13,038
And it tells me that user name,

53
00:03:13,038 --> 00:03:14,682
a user with that username already exists

54
00:03:14,682 --> 00:03:17,066
because I've been playing around with it.

55
00:03:17,066 --> 00:03:21,223
Let's try to authenticate,
it gives me back a token.

56
00:03:21,223 --> 00:03:24,973
We can retrieve items
and we can create items

57
00:03:25,945 --> 00:03:26,985
and so on.

58
00:03:26,985 --> 00:03:29,843
An error occurred, we can
potentially see the error

59
00:03:29,843 --> 00:03:32,785
probably has something to do with the,

60
00:03:32,785 --> 00:03:35,230
oh yeah, the store ID of course.

61
00:03:35,230 --> 00:03:37,383
Don't know if you guys remember,

62
00:03:37,383 --> 00:03:40,496
I mentioned earlier on in
the course that Sequel Light

63
00:03:40,496 --> 00:03:43,829
doesn't enforce foreign key constraints,

64
00:03:43,829 --> 00:03:45,579
whereas Posdres does.

65
00:04:25,704 --> 00:04:29,859
But, what we can do is make
sure to create the store

66
00:04:29,859 --> 00:04:31,556
before we create the item.

67
00:04:31,556 --> 00:04:34,618
That's what a constraint is.

68
00:04:34,618 --> 00:04:37,634
In a foreign key constraint,
we're saying that

69
00:04:37,634 --> 00:04:38,882
this item we're creating here

70
00:04:38,882 --> 00:04:42,095
is related to the store with ID one,

71
00:04:42,095 --> 00:04:43,931
but that store doesn't exist.

72
00:04:43,931 --> 00:04:45,517
So Sequel Light was saying, you know what,

73
00:04:45,517 --> 00:04:48,162
whatever, I don't care, whereas Posdres

74
00:04:48,162 --> 00:04:52,329
is a bit more advanced, and it
checks that the store exists.

75
00:04:53,181 --> 00:04:55,066
So let's create this store first,

76
00:04:55,066 --> 00:04:58,268
we're gonna post this store there,

77
00:04:58,268 --> 00:05:00,251
and as you can see that works,

78
00:05:00,251 --> 00:05:02,819
and now we can go back to posting the item

79
00:05:02,819 --> 00:05:04,319
with store ID one.

80
00:05:05,750 --> 00:05:07,583
And that gets created.

81
00:05:09,135 --> 00:05:11,071
If we then go and get
the store for example,

82
00:05:11,071 --> 00:05:13,165
remember this is gonna
send a list of stores

83
00:05:13,165 --> 00:05:17,165
and their items back, now
we get the store name,

84
00:05:18,039 --> 00:05:21,021
which is test, and the
items which is also test,

85
00:05:21,021 --> 00:05:22,152
which is not a very good name,

86
00:05:22,152 --> 00:05:25,841
but nevertheless, that's that there.

87
00:05:25,841 --> 00:05:27,722
So as you can see, everything works,

88
00:05:27,722 --> 00:05:30,321
but deploying to your own server

89
00:05:30,321 --> 00:05:33,167
is not as easy as it
is deploying to Heroku.

90
00:05:33,167 --> 00:05:35,834
You have to do a lot more stuff.

91
00:05:36,860 --> 00:05:39,334
You have to set up
Posdress, you have to set up

92
00:05:39,334 --> 00:05:43,167
nginx, you have to
understand how uwsgi works.

93
00:05:43,167 --> 00:05:44,687
You have to understand
a bit about permissions,

94
00:05:44,687 --> 00:05:45,520
and things like that.

95
00:05:45,520 --> 00:05:47,608
It's not he easiest thing.

96
00:05:47,608 --> 00:05:50,324
So if you want to develop
a product quickly,

97
00:05:50,324 --> 00:05:54,491
you want to get it out on
the market ASAP essentially,

98
00:05:55,568 --> 00:05:57,568
Heroku is a good option.

99
00:05:59,249 --> 00:06:01,480
You don't have to deal
with any server stuff

100
00:06:01,480 --> 00:06:03,695
or anything like that.

101
00:06:03,695 --> 00:06:06,921
However it reaches a
point where Heroku gets

102
00:06:06,921 --> 00:06:08,771
really expensive really quickly,

103
00:06:08,771 --> 00:06:11,286
so it's worth considering deploying

104
00:06:11,286 --> 00:06:14,958
to your own servers once
you have a bit more traffic

105
00:06:14,958 --> 00:06:19,132
and a bit more of a need for performance.

106
00:06:19,132 --> 00:06:22,376
However as you grow more and more,

107
00:06:22,376 --> 00:06:24,206
controlling all these servers does become

108
00:06:24,206 --> 00:06:25,956
quite time consuming,

109
00:06:26,911 --> 00:06:31,328
so at that point, you
may consider migrating

110
00:06:31,328 --> 00:06:34,876
to either something like
Amazon web services,

111
00:06:34,876 --> 00:06:37,843
or hiring somebody to deal
with these servers for you.

112
00:06:37,843 --> 00:06:40,865
Many companies have their
infrastructure teams

113
00:06:40,865 --> 00:06:42,403
and things like that.

114
00:06:42,403 --> 00:06:45,849
Which do this sort of job as
well as many other things.

115
00:06:45,849 --> 00:06:49,766
So for development, not
only about programming,

116
00:06:50,937 --> 00:06:53,940
but also about a lot of
other things related to it.

117
00:06:53,940 --> 00:06:58,432
So something to keep in mind
as you develop more and more.

118
00:06:58,432 --> 00:07:00,117
So that's it for this section.

119
00:07:00,117 --> 00:07:03,313
We've learned how to
deploy to our own server,

120
00:07:03,313 --> 00:07:06,515
and now we have to look at performance,

121
00:07:06,515 --> 00:07:09,850
which we're gonna look at using Varnish.

122
00:07:09,850 --> 00:07:13,843
And we can look at creating
our own final project

123
00:07:13,843 --> 00:07:16,297
as well, which is gonna
be very interesting.

124
00:07:16,297 --> 00:07:18,516
So that's it for this section,

125
00:07:18,516 --> 00:07:20,623
and I'll see you in the very next one.

