-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (108 loc) · 3.41 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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/djmarrerajr/common-lib/app"
"github.com/djmarrerajr/common-lib/observability/tracing"
"github.com/djmarrerajr/common-lib/shared"
"github.com/djmarrerajr/common-lib/utils"
)
var (
cfgPath = flag.String("cfg", "./config", "path in which to find the .env files")
)
var (
dbServer *Database
emailServer *EmailServer
emailVendor *EmailVendor
)
type Greeting struct {
Name string `json:"name" xml:"name"`
Age int `json:"age" xml:"age" validate:"numeric,gt=10"`
}
func main() {
env, err := utils.LoadEnv(*cfgPath)
if err != nil {
log.Fatalf("unable to parse environment: %v", err)
}
app, err := app.NewWithApiFromEnv(
env,
app.WithRouteHandler("/time", timeHandler, http.MethodGet),
app.WithRequestHandler("/hello", helloHandler, nil, http.MethodGet),
app.WithRequestHandler("/greet", greetHandler, Greeting{}, http.MethodPost),
)
if err != nil {
log.Fatalf("unable to instantiate application: %v", err)
}
dbServer = &Database{appCtx: *app.AppContext}
emailServer = &EmailServer{appCtx: *app.AppContext}
emailVendor = &EmailVendor{appCtx: *app.AppContext}
if err = app.Run(); err != nil {
log.Fatalf("application terminated in error: %v", err)
}
log.Print("application terminated successfully")
os.Exit(0)
}
func timeHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
// nolint: errcheck
w.Write([]byte(time.Now().UTC().Format(time.RFC3339)))
}
func helloHandler(ctx context.Context, appCtx *shared.ApplicationContext, req any) (any, int) {
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
return "Hello World!", http.StatusOK
}
func greetHandler(ctx context.Context, appCtx *shared.ApplicationContext, req any) (any, int) {
type HelloResponse struct {
Message string
}
rqst := req.(*Greeting)
dbServer.PerformQuery(ctx, randomString(12))
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
emailServer.SendEmail(ctx, rqst.Name)
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
return HelloResponse{
Message: fmt.Sprintf("Hello %s!", rqst.Name),
}, http.StatusOK
}
type Database struct {
appCtx shared.ApplicationContext
}
func (d Database) PerformQuery(ctx context.Context, queryName string) {
span, _ := tracing.StartChildSpan(ctx, "PerformQuery")
defer tracing.FinishChildSpan(span)
span.SetTag("queryName", queryName)
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
}
type EmailServer struct {
appCtx shared.ApplicationContext
}
func (e EmailServer) SendEmail(ctx context.Context, recipient string) {
span, parentCtx := tracing.StartChildSpan(ctx, "SendEmail")
defer tracing.FinishChildSpan(span)
emailVendor.SendEmail(parentCtx, recipient)
span.SetTag("recipient", recipient)
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
}
type EmailVendor struct {
appCtx shared.ApplicationContext
}
func (e EmailVendor) SendEmail(ctx context.Context, recipient string) {
span, _ := tracing.StartChildSpan(ctx, "TransmitEmail")
defer tracing.FinishChildSpan(span)
span.SetTag("recipient", recipient)
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
}
func randomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}