/
backend.go
163 lines (141 loc) · 3.93 KB
/
backend.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type backendError struct {
status int
err error
}
func sendBackendError(c *gin.Context, err backendError) {
switch err.status {
case http.StatusNotFound:
c.String(http.StatusNotFound, "not found")
return
}
c.String(http.StatusBadGateway, "error status from backend: %d", err.status)
}
func httpSuccess(status int) bool {
return status == 0 || (status >= 200 && status < 300)
}
func noError(status int, err error) bool {
return err == nil && httpSuccess(status)
}
func newBackendError(status int, err error) error {
if noError(status, err) {
return nil
}
return backendError{
status: status,
err: err,
}
}
func (e backendError) Error() string {
if noError(e.status, e.err) {
return "<nil>"
}
var msg string
if e.err == nil {
msg = "<nil>"
} else {
msg = e.err.Error()
}
return fmt.Sprintf("backendError: status:%d error:%s", e.status, msg)
}
type backend interface {
fetch(ctx context.Context, path string) ([]byte, error)
}
func newBackend(tracer trace.Tracer, address, options string) backend {
if dir := strings.TrimPrefix(address, "dir:"); dir != address {
log.Printf("backend: %s: dir", address)
return newBackendDir(tracer, dir, options)
}
log.Printf("backend: %s: http", address)
return &backendHTTP{tracer: tracer, host: address}
}
type backendDir struct {
tracer trace.Tracer
dir string
flatten bool // strip directory prefixes from requested path
}
func newBackendDir(tracer trace.Tracer, dir, options string) *backendDir {
flatten := strings.Contains(options, "flatten")
return &backendDir{tracer: tracer, dir: dir, flatten: flatten}
}
func (b *backendDir) fetch(ctx context.Context, path string) ([]byte, error) {
_, span := b.tracer.Start(ctx, "backendDir.fetch")
defer span.End()
var status int
var filename string
if b.flatten {
filename = filepath.Base(path)
} else {
filename = path
}
fullpath := filepath.Join(b.dir, filename)
data, err := os.ReadFile(fullpath)
if err != nil && strings.Contains(err.Error(), "no such file or directory") {
status = http.StatusNotFound
}
log.Printf("backendDir: flatten=%t path='%s' filename='%s' fullpath='%s' size=%d status=%d error:%v",
b.flatten, path, filename, fullpath, len(data), status, err)
be := newBackendError(status, err)
if be != nil {
span.SetStatus(codes.Error, be.Error())
}
return data, be
}
type backendHTTP struct {
tracer trace.Tracer
host string
}
func (b *backendHTTP) fetch(ctx context.Context, path string) ([]byte, error) {
newCtx, span := b.tracer.Start(ctx, "backendHTTP.fetch")
defer span.End()
var status int
u, errJoin := url.JoinPath(b.host, path)
if errJoin != nil {
log.Printf("backendHTTP: path='%s' join error: %v", path, errJoin)
be := newBackendError(status, errJoin)
span.SetStatus(codes.Error, be.Error())
return nil, be
}
req, errReq := http.NewRequestWithContext(newCtx, "GET", u, nil)
if errReq != nil {
log.Printf("backendHTTP: path='%s' req error: %v", path, errReq)
be := newBackendError(status, errReq)
span.SetStatus(codes.Error, be.Error())
return nil, be
}
client := http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
resp, errGet := client.Do(req)
if errGet != nil {
log.Printf("backendHTTP: path='%s' url='%s' GET error: %v",
path, u, errGet)
be := newBackendError(status, errGet)
span.SetStatus(codes.Error, be.Error())
return nil, be
}
defer resp.Body.Close()
status = resp.StatusCode
data, errRead := io.ReadAll(resp.Body)
resp.Body.Close()
log.Printf("backendHTTP: path='%s' url='%s' size=%d status=%d error:%v",
path, u, len(data), status, errRead)
be := newBackendError(status, errRead)
if be != nil {
span.SetStatus(codes.Error, be.Error())
}
return data, be
}