Skip to content

Commit 9e998a5

Browse files
committed
Log using goa
1 parent e307f87 commit 9e998a5

File tree

15 files changed

+187
-55
lines changed

15 files changed

+187
-55
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
bufferflow_tinyg_old.md
33

4-
serial-port-json-server
4+
arduino-create-agent
55

66
snapshot/*
77
public/

design/tools.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import . "goa.design/goa/dsl"
55
var _ = Service("tools", func() {
66
Description("The tools service managed the tools installed in the system.")
77
Method("list", func() {
8-
Result(Tool)
8+
Result(CollectionOf(Tool))
99
HTTP(func() {
1010
GET("/tools")
1111
Response(StatusOK)

gen/http/openapi.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"swagger":"2.0","info":{"title":"Arduino Create Agent","description":"A companion of Arduino Create. \n\tAllows the website to perform operations on the user computer, \n\tsuch as detecting which boards are connected and upload sketches on them.","version":""},"host":"localhost:80","basePath":"/v2","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/tools":{"get":{"tags":["tools"],"summary":"list tools","operationId":"tools#list","responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/ToolsListResponseBody"}}},"schemes":["http"]}}},"definitions":{"ToolsListResponseBody":{"title":"Mediatype identifier: application/vnd.arduino.tool; view=default","type":"object","properties":{"name":{"type":"string","description":"The name of the tool","example":"avrdude"},"packager":{"type":"string","description":"The packager of the tool","example":"arduino"},"version":{"type":"string","description":"The version of the tool","example":"6.3.0-arduino9"}},"description":"ListResponseBody result type (default view)","example":{"name":"avrdude","packager":"arduino","version":"6.3.0-arduino9"},"required":["name","version","packager"]}}}
1+
{"swagger":"2.0","info":{"title":"Arduino Create Agent","description":"A companion of Arduino Create. \n\tAllows the website to perform operations on the user computer, \n\tsuch as detecting which boards are connected and upload sketches on them.","version":""},"host":"localhost:80","basePath":"/v2","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/tools":{"get":{"tags":["tools"],"summary":"list tools","operationId":"tools#list","responses":{"200":{"description":"OK response.","schema":{"$ref":"#/definitions/ToolsToolResponseCollection"}}},"schemes":["http"]}}},"definitions":{"ToolResponse":{"title":"Mediatype identifier: application/vnd.arduino.tool; view=default","type":"object","properties":{"name":{"type":"string","description":"The name of the tool","example":"avrdude"},"packager":{"type":"string","description":"The packager of the tool","example":"arduino"},"version":{"type":"string","description":"The version of the tool","example":"6.3.0-arduino9"}},"description":"A tool is an executable program that can upload sketches. (default view)","example":{"name":"avrdude","packager":"arduino","version":"6.3.0-arduino9"},"required":["name","version","packager"]},"ToolsToolResponseCollection":{"title":"Mediatype identifier: application/vnd.arduino.tool; type=collection; view=default","type":"array","items":{"$ref":"#/definitions/ToolResponse"},"description":"ListResponseBody is the result type for an array of ToolResponse (default view)","example":[{"name":"avrdude","packager":"arduino","version":"6.3.0-arduino9"},{"name":"avrdude","packager":"arduino","version":"6.3.0-arduino9"}]}}}

gen/http/openapi.yaml

+18-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ paths:
2626
"200":
2727
description: OK response.
2828
schema:
29-
$ref: '#/definitions/ToolsListResponseBody'
29+
$ref: '#/definitions/ToolsToolResponseCollection'
3030
schemes:
3131
- http
3232
definitions:
33-
ToolsListResponseBody:
33+
ToolResponse:
3434
title: 'Mediatype identifier: application/vnd.arduino.tool; view=default'
3535
type: object
3636
properties:
@@ -46,7 +46,8 @@ definitions:
4646
type: string
4747
description: The version of the tool
4848
example: 6.3.0-arduino9
49-
description: ListResponseBody result type (default view)
49+
description: A tool is an executable program that can upload sketches. (default
50+
view)
5051
example:
5152
name: avrdude
5253
packager: arduino
@@ -55,3 +56,17 @@ definitions:
5556
- name
5657
- version
5758
- packager
59+
ToolsToolResponseCollection:
60+
title: 'Mediatype identifier: application/vnd.arduino.tool; type=collection; view=default'
61+
type: array
62+
items:
63+
$ref: '#/definitions/ToolResponse'
64+
description: ListResponseBody is the result type for an array of ToolResponse
65+
(default view)
66+
example:
67+
- name: avrdude
68+
packager: arduino
69+
version: 6.3.0-arduino9
70+
- name: avrdude
71+
packager: arduino
72+
version: 6.3.0-arduino9

gen/http/tools/client/encode_decode.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/http/tools/client/types.go

+29-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/http/tools/server/encode_decode.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/http/tools/server/types.go

+16-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/tools/client.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/tools/endpoints.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/tools/service.go

+36-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/tools/views/view.go

+21-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/http.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
package v2
22

33
import (
4+
"context"
45
"net/http"
56

7+
"github.com/Sirupsen/logrus"
68
toolssvr "github.com/arduino/arduino-create-agent/gen/http/tools/server"
79
toolssvc "github.com/arduino/arduino-create-agent/gen/tools"
810
"github.com/arduino/arduino-create-agent/v2/tools"
911
goahttp "goa.design/goa/http"
12+
"goa.design/goa/http/middleware"
1013
)
1114

1215
func Server() http.Handler {
1316
mux := goahttp.NewMuxer()
1417

18+
// Instantiate logger
19+
logger := logrus.New()
20+
logAdapter := LogAdapter{Logger: logger}
21+
1522
// Mount tools
1623
toolsSvc := tools.Tools{}
1724
toolsEndpoints := toolssvc.NewEndpoints(&toolsSvc)
1825

19-
toolsServer := toolssvr.New(toolsEndpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, nil)
26+
toolsServer := toolssvr.New(toolsEndpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, errorHandler(logger))
2027
toolssvr.Mount(mux, toolsServer)
2128

22-
return mux
29+
// Mount middlewares
30+
handler := middleware.Log(logAdapter)(mux)
31+
handler = middleware.RequestID()(handler)
32+
33+
return handler
34+
}
35+
36+
// errorHandler returns a function that writes and logs the given error.
37+
// The function also writes and logs the error unique ID so that it's possible
38+
// to correlate.
39+
func errorHandler(logger *logrus.Logger) func(context.Context, http.ResponseWriter, error) {
40+
return func(ctx context.Context, w http.ResponseWriter, err error) {
41+
id := ctx.Value(middleware.RequestIDKey).(string)
42+
w.Write([]byte("[" + id + "] encoding: " + err.Error()))
43+
logger.Printf("[%s] ERROR: %s", id, err.Error())
44+
}
2345
}

0 commit comments

Comments
 (0)