-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
82 lines (71 loc) · 1.94 KB
/
app.go
File metadata and controls
82 lines (71 loc) · 1.94 KB
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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
dalle "github.com/TrueBlocks/trueblocks-dalle/v2"
"github.com/TrueBlocks/trueblocks-dalle/v2/pkg/storage"
)
type App struct {
ValidSeries []string
Config Config
}
func NewApp() *App {
app := App{Config: MustLoadConfig()}
_ = os.MkdirAll(storage.OutputDir(), 0o750)
app.ValidSeries = dalle.ListSeries()
return &app
}
type Request struct {
series string
address string
generate bool
remove bool
app *App
requestID string
}
func (r *Request) String() string {
return fmt.Sprintf(`{
"series": "%s",
"address": "%s",
"generate": %t,
"remove": %t,
"request_id": "%s"
}`, r.series, r.address, r.generate, r.remove, r.requestID)
}
func (a *App) parseRequest(r *http.Request) (Request, *APIError) {
requestID := GenerateRequestID()
path := strings.TrimPrefix(r.URL.Path, "/dalle/")
segments := strings.SplitN(path, "/", 2)
if len(segments) < 2 {
return Request{}, NewAPIError(
ErrorInvalidRequest,
"Invalid request path",
fmt.Sprintf("Path '%s' does not match expected format /dalle/{series}/{address}", r.URL.Path),
).WithRequestID(requestID)
}
series := strings.ToLower(segments[0])
if len(series) == 0 {
return Request{}, ErrorMissingRequiredParameter("series").WithRequestID(requestID)
}
if !dalle.IsValidSeries(series, a.ValidSeries) {
return Request{}, ErrorInvalidSeriesName(series).WithRequestID(requestID)
}
address := strings.ToLower(segments[1])
if len(address) == 0 {
return Request{}, ErrorMissingRequiredParameter("address").WithRequestID(requestID)
}
if !base.IsValidAddress(address) {
return Request{}, ErrorInvalidAddressFormat(address).WithRequestID(requestID)
}
return Request{
series: series,
address: address,
generate: r.URL.Query().Get("generate") == "1",
remove: r.URL.Query().Has("remove"),
app: a,
requestID: requestID,
}, nil
}