This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httphandler.go
249 lines (242 loc) · 7.35 KB
/
httphandler.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright 2019 Aletheia Ware LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bcnetgo
import (
"aletheiaware.com/bcgo"
"aletheiaware.com/netgo"
"encoding/base64"
"fmt"
"html/template"
"log"
"net/http"
)
func BlockHandler(cache bcgo.Cache, template *template.Template) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL.Path, r.Header)
switch r.Method {
case "GET":
hash := netgo.GetQueryParameter(r.URL.Query(), "hash")
log.Println("Hash", hash)
if len(hash) > 0 {
hashBytes, err := base64.RawURLEncoding.DecodeString(hash)
if err != nil {
log.Println(err)
return
}
// Read block
block, err := cache.GetBlock(hashBytes)
if err != nil {
log.Println(err)
return
}
type TemplateReference struct {
Timestamp string
Channel string
BlockHash string
RecordHash string
}
type TemplateAccess struct {
Alias string
SecretKey string
EncryptionAlgorithm string
}
type TemplateEntry struct {
Hash string
Timestamp string
Creator string
Access []TemplateAccess
Payload string
CompressionAlgorithm string
EncryptionAlgorithm string
Signature string
SignatureAlgorithm string
Reference []TemplateReference
Meta map[string]string
}
entries := make([]TemplateEntry, 0)
for _, e := range block.Entry {
accesses := make([]TemplateAccess, 0)
for _, a := range e.Record.Access {
accesses = append(accesses, TemplateAccess{
Alias: a.Alias,
SecretKey: base64.RawURLEncoding.EncodeToString(a.SecretKey),
EncryptionAlgorithm: a.EncryptionAlgorithm.String(),
})
}
references := make([]TemplateReference, 0)
for _, r := range e.Record.Reference {
references = append(references, TemplateReference{
Timestamp: bcgo.TimestampToString(r.Timestamp),
Channel: r.ChannelName,
BlockHash: base64.RawURLEncoding.EncodeToString(r.BlockHash),
RecordHash: base64.RawURLEncoding.EncodeToString(r.RecordHash),
})
}
entries = append(entries, TemplateEntry{
Hash: base64.RawURLEncoding.EncodeToString(e.RecordHash),
Timestamp: bcgo.TimestampToString(e.Record.Timestamp),
Creator: e.Record.Creator,
Access: accesses,
Payload: base64.RawURLEncoding.EncodeToString(e.Record.Payload), // TODO allow override for custom rendering
CompressionAlgorithm: e.Record.CompressionAlgorithm.String(),
EncryptionAlgorithm: e.Record.EncryptionAlgorithm.String(),
Signature: base64.RawURLEncoding.EncodeToString(e.Record.Signature),
SignatureAlgorithm: e.Record.SignatureAlgorithm.String(),
Reference: references,
Meta: e.Record.Meta,
})
}
data := struct {
Hash string
Timestamp string
Channel string
Length string
Previous string
Miner string
Nonce string
Entry []TemplateEntry
}{
Hash: hash,
Timestamp: bcgo.TimestampToString(block.Timestamp),
Channel: block.ChannelName,
Length: fmt.Sprintf("%d", block.Length),
Previous: base64.RawURLEncoding.EncodeToString(block.Previous),
Miner: block.Miner,
Nonce: fmt.Sprintf("%d", block.Nonce),
Entry: entries,
}
if err := template.Execute(w, data); err != nil {
log.Println(err)
return
}
} else {
w.WriteHeader(http.StatusNotFound)
}
default:
log.Println("Unsupported method", r.Method)
}
}
}
func ChannelHandler(cache bcgo.Cache, template *template.Template) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL.Path, r.Header)
switch r.Method {
case "GET":
channel := netgo.GetQueryParameter(r.URL.Query(), "channel")
log.Println("Channel", channel)
if len(channel) > 0 {
reference, err := cache.GetHead(channel)
if err != nil {
log.Println(err)
return
}
data := struct {
Channel string
Timestamp string
Hash string
}{
Channel: reference.ChannelName,
Timestamp: bcgo.TimestampToString(reference.Timestamp),
Hash: base64.RawURLEncoding.EncodeToString(reference.BlockHash),
}
if err := template.Execute(w, data); err != nil {
log.Println(err)
return
}
} else {
w.WriteHeader(http.StatusNotFound)
}
default:
log.Println("Unsupported method", r.Method)
}
}
}
func ChannelListHandler(cache bcgo.Cache, template *template.Template, list func() []*bcgo.Channel) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL.Path, r.Header)
switch r.Method {
case "GET":
type TemplateChannel struct {
Name string
Timestamp string
Hash string
}
channels := make([]TemplateChannel, 0)
for _, channel := range list() {
reference, err := cache.GetHead(channel.Name)
if err != nil {
log.Println(err)
} else {
channels = append(channels, TemplateChannel{
Name: reference.ChannelName,
Timestamp: bcgo.TimestampToString(reference.Timestamp),
Hash: base64.RawURLEncoding.EncodeToString(reference.BlockHash),
})
}
}
data := struct {
Channel []TemplateChannel
}{
Channel: channels,
}
if err := template.Execute(w, data); err != nil {
log.Println(err)
return
}
default:
log.Println("Unsupported method", r.Method)
}
}
}
func PeriodicValidationHandler(channel *bcgo.Channel, cache bcgo.Cache, template *template.Template) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL.Path, r.Header)
switch r.Method {
case "GET":
hash := netgo.GetQueryParameter(r.URL.Query(), "hash")
log.Println("Hash", hash)
hashBytes := channel.Head
var err error
if len(hash) > 0 {
hashBytes, err = base64.RawURLEncoding.DecodeString(hash)
if err != nil {
log.Println(err)
return
}
}
block, err := cache.GetBlock(hashBytes)
if err != nil {
log.Println(err)
return
}
if block != nil {
data := struct {
// TODO
}{
// TODO
}
if err := template.Execute(w, data); err != nil {
log.Println(err)
return
}
} else {
w.WriteHeader(http.StatusNotFound)
}
default:
log.Println("Unsupported method", r.Method)
}
}
}