-
Notifications
You must be signed in to change notification settings - Fork 7
/
api.go
80 lines (65 loc) · 2.04 KB
/
api.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
package main
import (
"net/http"
"strconv"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
// restAPI() This is the main API that is activated when isCoord == true
func restAPI(keyCollection *ED25519Keys) {
// CORS
corsAllowedHeaders := []string{
"Access-Control-Allow-Headers",
"Access-Control-Allow-Methods",
"Access-Control-Allow-Origin",
"Cache-Control",
"Content-Security-Policy",
"Feature-Policy",
"Referrer-Policy",
"X-Requested-With"}
corsOrigins := []string{
"*",
"127.0.0.1"}
corsMethods := []string{
"GET",
"HEAD",
"POST",
"PUT",
"OPTIONS"}
headersCORS := handlers.AllowedHeaders(corsAllowedHeaders)
originsCORS := handlers.AllowedOrigins(corsOrigins)
methodsCORS := handlers.AllowedMethods(corsMethods)
// Init API
r := mux.NewRouter()
api := r.PathPrefix("/api/v1").Subrouter()
// Home
api.HandleFunc("/", home).Methods(http.MethodGet)
// Version
api.HandleFunc("/version", returnVersion).Methods(http.MethodGet)
// Stats
api.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
returnStatsWeb(w, r, keyCollection)
}).Methods(http.MethodGet)
// Transaction by ID
api.HandleFunc("/transaction/{hash}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hash := vars["hash"]
returnSingleTransaction(w, r, hash)
}).Methods(http.MethodGet)
// Transaction by qty
api.HandleFunc("/transactions/{number}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
number := vars["number"]
returnNTransactions(w, r, number)
}).Methods(http.MethodGet)
// Channel Socket
api.HandleFunc("/channel", func(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, _ := upgrader.Upgrade(w, r, nil)
defer conn.Close()
// fmt.Printf(brightgreen+"\n[%s] [%s] Peer socket opened!"+white, timeStamp(), conn.RemoteAddr())
socketAuthAgent(conn, keyCollection)
})
// Serve via HTTP
http.ListenAndServe(":"+strconv.Itoa(karaiAPIPort), handlers.CORS(headersCORS, originsCORS, methodsCORS)(api))
}