-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdc-go-web-apis.html
373 lines (348 loc) · 12 KB
/
tdc-go-web-apis.html
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
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.5.0/css/reveal.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.5.0/css/theme/white.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/zenburn.min.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Desenvolvendo Web APIs</h1>
<span>em Go</span>
</section>
<section>
<h1>Bianca Rosa</h1>
<img class="plain" height="300" src="images/profile/bia-gopher.png" />
<ul>
<li class="fragment">Python / Go / Lua / JS</li>
<li class="fragment">PythOnRio / Gophers RJ </li>
<li class="fragment">Pyladies / Rails Girls RJ / WWG RJ</li>
</ul>
</section>
<section>
<ul>
<li>Sobre frameworks</li>
<li>Escrita de código: padrões e clean code</li>
<li>Estrutura de pastas</li>
<li>Testes</li>
<li>Gerenciando dependências</li>
<li>Dockerfile</li>
<li>Erros comuns</li>
<li>Quando usar Go</li>
</ul>
</section>
<section>
<h1>Devo ou não usar um framework web em Go?</h1>
</section>
<section>
<p>A standard library do Go é bastante poderosa.</p>
<p class="fragment">
Portanto, não utilizar um framework é perfeitamente aceitável.
</p>
<p class="fragment">
#leitura:
<a href="https://medium.com/code-zen/why-i-don-t-use-go-web-frameworks-1087e1facfa4">Why I Don’t Use Go Web Frameworks</a>
</p>
<p class="fragment">
#disclaimer: eu gosto de utilizar gin / echo.
</p>
</section>
<section>
<pre class="highlight"><code>package main
import (
"fmt"
"io"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, TDC!")
}
func main() {
http.HandleFunc("/", hello)
fmt.Println("Serving http...")
http.ListenAndServe(":8000", nil)
}</code></pre>
</section>
<section>
<pre class="highlight"><code>package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Greeting struct {
Message string `json:"msg"`
}
func hello(w http.ResponseWriter, r *http.Request) {
person := Greeting{Message: "Hello"}
jsonPerson, err := json.Marshal(person)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonPerson)
}
func main() {
http.HandleFunc("/", hello)
fmt.Println("Serving http...")
http.ListenAndServe(":8000", nil)
}</code></pre>
</section>
<section>
<p>
<a href="https://github.com/biancarosa/golang-http-server">github.com/biancarosa/golang-http-server</a>
</p>
</section>
<section>
<h2>Frameworks</h2>
<ul>
<li>Echo</li>
<li>Gin</li>
<li>Martini</li>
<li>Beego</li>
<li>Revel</li>
<li>Traffic</li>
<li>... e outros que eu não conheço</li>
</ul>
</section>
<section>
<h2>Ou ainda...</h2>
<p>
<a href="http://www.gorillatoolkit.org/">gorilla</a>
</p>
</section>
<section>
<ul>
<ol>1. Você é uma pessoa livre.</ol>
<ol class="fragment">2. Apenas baseie sua decisão em bons argumentos.</ol>
<ol class="fragment">3. Muito do que está escrito na Internet são opiniões.</ol>
</ul>
</section>
<section>
<p>
<a href="https://github.com/biancarosa/goasync">github.com/biancarosa/goasync</a>
</p>
</section>
<section>
<pre class="highlight"><code>package main
import (
"os"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
log "github.com/sirupsen/logrus"
"github.com/biancarosa/goasync/routes"
)
func main() {
e := echo.New()
// Setup Logrus
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/", routes.HealthCheck)
e.POST("/async", routes.ExecuteTask)
e.GET("/tasks/:uuid", routes.RetrieveTask)
e.Logger.Fatal(e.Start(":1323"))
}</code></pre>
</section>
<section>
<pre class="highlight"><code>package routes
import (
"net/http"
"os"
"github.com/labstack/echo"
log "github.com/sirupsen/logrus"
)
func init() {
// Setup Logrus
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
//HealthCheck is the route that prints a sucessful message when the application is fine.
func HealthCheck(c echo.Context) error {
return c.String(http.StatusOK, "I seem to be perfectly fine.")
}</code></pre>
</section>
<section>
<h1>Escreva bons códigos</h1>
</section>
<section>
<p>
#leitura:
<a href="https://teivah.io/blog/good-code-vs-bad-code-in-golang/">Good Code vs Bad Code in Golang</a>
</p>
<p>
#slides:
<a href="http://biancarosa.com.br/slides/go-clean-code.html">Boas Práticas & Clean Code com Golang</a>
</p>
</section>
<section>
<a href="https://golang.org/doc/effective_go.html">
Effective Go
</a>
<br/>
<a href="https://github.com/golang/go/wiki">
Go wiki
</a>
</section>
<section>
<h1>Use Interfaces</h1>
</section>
<section>
<h1>Estrutura de pastas</h1>
<p>
<small>* totalmente minha opinião</small>
</p>
<ul>
<li>routes</li>
<li>services</li>
<li>models</li>
<li>configuration</li>
</ul>
</section>
<section>
<h1>Testes de Unidade</h1>
<p>Se o código é dificil de testar, então provavelmente merece ser reescrito.</p>
</section>
<section>
<p>
"SÓ QUERIA UM MONKEY PATCH IGUAL AO DO PYTHON"
</p>
<p>
<small>Rosa, Bianca, 3am, sábado</small>
</p>
<p class="fragment"><a href="https://github.com/bouk/monkey">github.com/bouk/monkey</a></p>
</section>
<section>
<img src="images/uncle-ben.jpg"/>
</section>
<section>
<h1>Testes de Integração</h1>
<p>Separação de testes unitários e de integração através de build tags.</p>
<pre class="highlight"><code>// +build integration</code></pre>
</section>
<section>
<h1>Testes de Carga</h1>
<p>
<a href="https://github.com/tsenart/vegeta">github.com/tsenart/vegeta</a>
</p>
</section>
<section>
<h1>Dependências</h1>
<p>
<a href="https://github.com/golang/dep">github.com/golang/dep</a>
</p>
</section>
<section>
<h1>Dockerfile</h1>
<p>
<pre class="highlight"><code>FROM golang:latest AS build
WORKDIR $GOPATH/{your-repo}
COPY . ./
RUN go get github.com/tools/godep
RUN godep get
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /app .
FROM scratch
COPY --from=build /app ./
ENTRYPOINT ["./app"]</code></pre>
</p>
<p>
<a href="https://gist.github.com/biancarosa/c08c603fc9d55e2408a09aea4695e96b">gist</a>
</a>
</p>
</section>
<section>
<h1>Erros comuns</h1>
</section>
<section>
<p><pre class="highlight"><code>//Task is the structure that contains all relevant information for a task
type Task struct {
Method string
URL string
name string
UUID uuid.UUID
}</code></pre></p>
</section>
<section>
<p><pre class="highlight"><code>{
"Method" : "GET",
"URL" : "http://google.com",
"UUID": "3b4f73b3-ee18-46b5-b9ed-b1e26573794f"
}</code></pre></p>
</section>
<section>
<h2>Campos privados de uma struct não são serializados nem deserializados!</h2>
</section>
<section>
<p><pre class="highlight"><code>//Task is the structure that contains all relevant information for a task
type Task struct {
Method string
URL string
Name string
UUID uuid.UUID
}</code></pre></p>
</section>
<section>
<p><pre class="highlight"><code>{
"Method" : "GET",
"Name": "Get Google",
"URL" : "http://google.com",
"UUID": "3b4f73b3-ee18-46b5-b9ed-b1e26573794f"
}</code></pre></p>
</section>
<section>
<h1>Não tente reinventar a roda</h1>
<p>Ex: <a href="https://github.com/Sirupsen/logrus">logrus</a></p>
</section>
<section>
<h1>Nunca ignore erros!</h1>
</section>
<section>
<pre class="highlight"><code>session, e_ := mgo.Dial("db:27017")
defer session.Close()</code></pre>
</section>
<section>
<pre class="highlight"><code>session, err := mgo.Dial("db:27017")
if err != nil {
return
}
defer session.Close()</code></pre>
</section>
<section>
<h1>Quando usar Go</h1>
<ul>
<li class="fragment">Performance</li>
<li class="fragment">Go Routines</li>
<li class="fragment">Confiabilidade</li>
</ul>
</section>
<section>
<p>twitter:
<a href="http://twitter.com/__biancarosa">@__biancarosa</a>
</p>
<p>slides:
<a href="http://biancarosa.com.br/slides/tdc-go-web-apis.html">biancarosa.com.br/slides/tdc-go-web-apis.html</a>
</p>
<p>tks :)</p>
</section>
</div>
</section>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/headjs/1.0.3/head.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.5.0/js/reveal.min.js"></script>
<script>
Reveal.initialize({
dependencies: [
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.5.0/plugin/notes/notes.js' },
{ src: 'https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.5.0/plugin/highlight/highlight.js', callback: function () { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>