-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (67 loc) · 1.54 KB
/
main.go
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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"github.com/ardanlabs/conf/v3"
myLog "github.com/eduardobcolombo/colombostips/config-05/pkg/log"
"github.com/eduardobcolombo/colombostips/config-05/pkg/newrelic"
"go.uber.org/zap"
)
// Config represents a configuration type to receive env variables
type Config struct {
Log myLog.Config
NewRelic newrelic.Config
Port string `conf:"default:8000"`
}
type App struct {
log *zap.Logger
}
func main() {
exit, err := run()
if err != nil {
log.Fatal(err)
}
os.Exit(exit)
}
func run() (int, error) {
var cfg Config
const prefix = ""
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return 1, nil
}
return 1, fmt.Errorf("parsing config: %w", err)
}
logger := myLog.New(cfg.Log)
defer logger.Sync()
nrApp, err := newrelic.New(cfg.NewRelic)
if err != nil {
log.Fatal(err)
return 1, err
}
app := App{log: logger}
txn := newrelic.StartTransaction(nrApp, "Example transaction")
defer txn.End()
txn.AddAttribute("traceID", txn.GetTraceMetadata().TraceID)
http.HandleFunc(newrelic.WrapHandleFunc(nrApp, "/", app.hello))
port := fmt.Sprintf(":%s", cfg.Port)
err = http.ListenAndServe(port, nil)
if err != nil {
log.Fatal(err)
}
return 0, nil
}
func (a App) hello(w http.ResponseWriter, req *http.Request) {
// Write in the response
fmt.Fprintf(w, "hello\n")
// Write in the log
a.log.Debug("Hello Debug")
a.log.Info("Hello Info")
a.log.Warn("Hello Warn")
a.log.Error("Hello Error")
}