-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-rest.slide
398 lines (205 loc) · 9.32 KB
/
server-rest.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
REST, Sockets em Golang
Xml-RPC e WebSocket
Tags: go, restful, rest,xml-rpc,socket, Websocket
Jefferson Otoni Lima
*CEO* & *Co-Fundador* | S3wf & S3commerce entrepreneur 2007
*Full-Stack* *programmer*
* Camadas OSI!
#.image images/camada-osi-wiki.png
.image images/Osi-tcpip.png
* Arquiteturas, protocolos para troca de informações
- Web Services {http/https} / [[https://github.com/gorilla/mux][Rest]] e [[https://github.com/fiorix/wsdl2go][Soap]]
- Sockets {tcp/udp} / [[https://golang.org/pkg/net/][pkg net]]
- WebSocket {http/https} / [[https://godoc.org/golang.org/x/net/websocket][pkg websocket]]
- Rpc {http/tcp} / [[https://golang.org/pkg/net/rpc/][pkg rpc]]
- Codigos de exemplos [[https://github.com/jeffotoni/gotalks][Golang]]
- Apresentação sobre Socket [[http://go-talks.appspot.com/github.com/jeffotoni/gotalks/server-rest.slide][Abrir aqui]]
.image images2/golang8.png
* Socket communication {"everything is a file"}
.image images/socket.png
* Socket / server main()
.code -edit socket/socket-server-0.go /^func main/,/^}/
.image images/go-aviator.png 600 600
* Socket / server func RedWriteSocket()
.code -edit socket/socket-server-0.go /^func RedWriteSocket/,/^}/
.image images/go-aviator.png 600 600
* Socket / client main()
.code -edit socket/socket-client-0.go /^func main/,/^}/
.image images/go-aviator.png 600 600
* Socket / client readerServer()
.code -edit socket/socket-client-0.go /^func readerServer/,/^}/
.image images/go-aviator.png 600 600
* O que é Socket TCP ?
- net.Listen("tcp", PORT)
- net.ListenTCP("tcp", tcpAddr)
.image images/go-aviator.png 600 600
* Socket Tcp / server main()
.code -edit socketcp/socket-tcp-server.go /^func main/,/^}/
.image images/go-aviator.png 600 600
* Socket Tcp / client main()
.code -edit socketcp/socket-tcp-client.go /^func main/,/^}/
.image images/go-aviator.png 600 600
* O que é WebSockets ?
- http.ListenAndServe(":1234", nil)
.image images/go-aviator.png 600 600
* WebSockets Server main
.code -edit websocket/websocker-server.go /^func main/,/^}/
.image images/go-aviator.png 600 600
* WebSockets ListenWebSocker()
.code -edit websocket/websocker-server.go /^func ListenWebSocker/,/^}/
.image images/go-aviator.png 600 600
* WebSockets Client
var wsuri = "ws://127.0.0.1:1234/chat"; // Url
window.onload = function() {
sock = new WebSocket(wsuri); // Connecting in our websocket
sock.onopen = function() {
console.log("connected to " + wsuri);
document.getElementById("connect").innerHTML = "online"
}
// Receiving message from the server
sock.onmessage = function(e) {
console.log("message received: " + e.data);
document.getElementById('msg-server').value = e.data
}
};
function send() { // Sending msg to server
var msg = document.getElementById('message').value;
sock.send(msg);
};
.image images/go-aviator.png 600 600
* RPC Como funciona ?
.image images/rpc-4.png 500 800
* Tipos de RCP
- [[https://pt.wikipedia.org/wiki/Chamada_de_procedimento_remoto][acrônimo de Remote Procedure Call]] / [[https://github.com/valyala/gorpc][GoRpc]]
- CORBA — padrão RPC independente de plataforma
- Sun RPC — RPC para as plataformas Unix e Linux
- DCOM — RPC para Windows
- RMI — RPC para Java
- SOAP — padrão RPC para web service
- JRES - Java Remote Execution
.image images/go-aviator.png 600 600
* RPC http/https Server
.code -edit rpc/rpc-http-server.go /^func main/,/^}/
* RPC http/https Call Stop server
.code -edit rpc/rpc-http-server.go /^type Stop/,/^}/
* RPC http/https Client
.code -edit rpc/rpc-http-client.go /^func main/,/^}/
* RPC Tcp / Json Server
.code -edit rpc/rpc-tcp-server.go /^func main/,/^}/
* RPC Tcp / Json Client
.code -edit rpc/rpc-tcp-client.go /^func main/,/^}/
* Tipos de Web Services / http/https {json/xml}
- [[https://pt.wikipedia.org/wiki/REST][REST]] / [[https://github.com/gorilla/mux][Framework Mux]]
- [[https://pt.wikipedia.org/wiki/SOAP][SOAP]] / [[https://github.com/fiorix/wsdl2go][framework {wsdl2Go]][[https://github.com/kylewolfe/soaptrip][ , Soaptrip}]]
- [[https://pt.wikipedia.org/wiki/XML-RPC][XML-RPC]] / [[https://github.com/kolo/xmlrpc][XmlRpc]]
.image images/go-aviator.png 600 600
.caption _Backend_Developer_at_ [[https://www.s3wf.com.br/][s3wf]]
* Como funciona o protocolo HTTP ?
- [[https://pt.wikipedia.org/wiki/Hypertext_Transfer_Protocol][Hypertext Transfer Protocol]]
- Mensagem HTTP [[https://pt.wikipedia.org/wiki/Lista_de_c%C3%B3digos_de_estado_HTTP][códigos http]]
- Cabeçalho da mensagem [[https://tools.ietf.org/html/rfc2616][*RFC* *2616*]]
- Corpo da mensagem
- Requisição (Request-Line,Request-header,Method, URI e HTTP-Version)
- Métodos (GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS e CONNECT)
.image images/go-aviator.png 600 600
* O que é REST ?
- [[https://pt.wikipedia.org/wiki/REST][*REST*]] (Representational State Transfer)
- É uma *abstração* da *arquitetura* da World Wide Web [pattern]
- *2000* por [[https://en.wikipedia.org/wiki/Roy_Fielding][Roy Fielding]]
- [[https://pt.wikipedia.org/wiki/Chamada_de_procedimento_remoto][RPC]] não é um exemplo de REST
- RESTful (_São_ _as_ _aplicações_ _ou_ _sistemas_ _que_ _usam_ _princípio_ _REST_)
.image images/go-aviator.png 600 600
* Concepções Rest
- Resources = /insert, /update , /search [URI]
- Representation = Html, Json, Xml, ... / Content-Type
- State Transfer = GET, POST, PUT ...
.image images/go-aviator.png 600 600
* Implementação REST
.image images/rest-1.png
* Scaling REST
.image images/rest-2.png
* Implementação RESTFul
.image images/rest-3.png
* Clientes REST
- [[https://ec.haxx.se][cURL (_criar_ _requisições_ _em_ _diversos_ _protocolos_)]]
- [[https://www.getpostman.com/][Postman]]
- [[https://httpie.org/][HTTPie]]
- [[https://www.hurl.it/][Hurl.it]]
.image images/go-aviator.png 600 600
* API Layers & Serviços REST
- [[https://console.aws.amazon.com/apigateway][Api Gateway]] (_Amazon_)
- [[https://docs.microsoft.com/en-us/azure/api-management/][Azure]] (_Microsoft_)
- [[https://www.dreamfactory.com/][Dreamfactory]] (_Dreamfactory_)
- [[https://firebase.google.com/docs/database/rest/start?hl=pt-br][Firebase]] (_Google_)
- [[https://getkong.org/][kong]] (_Gerenciador_ _de_ _APIs_ _e_ _Microserviços_ _open_-_source_)
.image images/go-aviator.png 600 600
* Frameworks REST
- [[https://github.com/sogko/slumber]][Slumer]]
- [[https://github.com/avelino/awesome-go/blob/master/README.md][awesome-go]] (_links_ _diversos_)
- [[https://github.com/gorilla/mux][Mux]] (_simples_ _roteador_ _url_)
- [[https://github.com/julienschmidt/httprouter][HttpRouter]] (_poderoso_ _roteador_ _url_)
- [[https://github.com/verdverm/frisby][Frisby]] (_REST_ _API_ _TEST_)
- [[https://github.com/pressly/chi][Pressly chi]] (_Http_ _Service_ _REST_)
- [[https://github.com/rs/formjson][Go JSON handler]]
- [[https://github.com/alioygur/gores][gores]] (_handles_ _HTML_, _JSON_, _XML_)
.image images/golang1.png
* Opa.. Golang now!
.image images/golang1.png
* Construindo nosso server REST / ListenAndServe ( )
// http
//
http.ListenAndServe(":9999", nil)
// servindo um site estático
//
http.ListenAndServe(":9999", http.FileServer(http.Dir("/static/site"))))
// ssl
//
http.ListenAndServeTLS(":443", server.crt, yourkey.key, nil)
// Criando chaves ssl
//
~/user-linux$ openssl req -nodes -newkey rsa:2048 -keyout yourkey.key -out serve.csr
.image images2/golang6.jpg
* Definindo Rotas Handlers!
* Server httpRouter {Criando rotas}
.code -edit httprouter/rest-httprouter-server.go /^func main/,/^}/
* Tipos de Uploads
.code -edit mux/server-mux-upload.go /^func main/,/^}/
* Copy para servidor único arquivio
.code -edit mux/server-mux-upload.go /^func uploadFormFile/,/^}/
* Copy para servidor multipolos arquivos
.code -edit mux/server-mux-upload.go /^func uploadFormFileMulti/,/^}/
* Uploads Binary
.code -edit mux/server-mux-upload.go /^func uploadBinary/,/^}/
.image images2/golang4.png
* cURL testando nosso REST
* Upload Form / multipart/form-data
curl -i -X POST http://localhost:9999/upload-form \
-u "API_KEY:383883jef903xxxx838xxxx" \
--form "fileupload=@files/file2.pdf"
.image images2/golang7.png
* Upload Form multi / multipart/form-data
curl -i --request POST localhost:9999/upload-form-multi \
-u "API_KEY:383883jef903xxxx838xxxx" \
--form 'email=jefferson&password=3838373773' \
--form "fileupload[]=@files/file1.jpg" \
--form "fileupload[]=@files/file2.pdf"
.image images2/images11.png
* Upload Binary
// http://https://xxxxxx.execute-api.us-xxx-x.amazonaws.com/upload-bin \
curl -i --request POST http://localhost:9999/upload-bin \
-u "API_KEY:383883jef903xxxx838xxxx" \
-H "Accept: binary/octet-stream" \
-H "Content-Type: binary/octet-stream" \
-H "Name-File: file2.pdf" \
--data-binary "@files/file2.pdf"
.image images2/golang1.jpg
* Referências
.link https://github.com/avelino/awesome-go/blob/master/README.md
.link https://golang.org/pkg/net
.link https://godoc.org/golang.org/x/net/websocket
.link https://golang.org/pkg/net/rpc
.link https://github.com/gorilla/mux
.link https://github.com/fiorix/wsdl2go
.link https://github.com/julienschmidt/httprouter
* Dúvidas?
.image images/help.png