-
Notifications
You must be signed in to change notification settings - Fork 43
/
blockwebserver.go
60 lines (49 loc) · 1.53 KB
/
blockwebserver.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
)
func blockWebSendBlock(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
blockHeight, err := strconv.Atoi(vars["height"])
if err != nil {
log.Println(vars)
w.WriteHeader(http.StatusBadRequest)
return
}
blockFilename := blockchainGetFilename(blockHeight)
if _, err := os.Stat(blockFilename); os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
log.Println("Block file not found:", blockFilename)
return
}
log.Println("HTTP serving block", blockHeight, "to", r.RemoteAddr)
w.Header().Set("Content-Type", "application/x-sqlite3")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%08x.db\"", blockHeight))
http.ServeFile(w, r, blockFilename)
// log.Println("Done serving block", blockHeight)
}
func blockWebSendChainParams(w http.ResponseWriter, r *http.Request) {
log.Println("HTTP serving chainparams.json to", r.RemoteAddr)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", "attachment; filename=\"chainparams.json\"")
_, err := w.Write(jsonifyWhateverToBytes(chainParams))
if err != nil {
log.Println(err)
}
}
func blockWebServer() {
r := mux.NewRouter()
r.HandleFunc("/block/{height}", blockWebSendBlock)
r.HandleFunc("/chainparams.json", blockWebSendChainParams)
serverAddress := fmt.Sprintf(":%d", cfg.httpPort)
log.Println("HTTP listening on", serverAddress)
err := http.ListenAndServe(serverAddress, r)
if err != nil {
panic(err)
}
}