Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dvovk/onelink #61

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 15 additions & 239 deletions api/api_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api

Check failure on line 1 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

: # github.com/ledgerwatch/diagnostics/api

import (
"encoding/json"
Expand Down Expand Up @@ -66,106 +66,8 @@
w.Write(jsonData)
}

func (h *APIHandler) Versions(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

versions, err := client.Version(r.Context())

if err != nil {
api_internal.EncodeError(w, r, err)
return
}

jsonData, err := json.Marshal(versions)

if err != nil {
fmt.Fprintf(w, "Unable to get version: %v", err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) CMDLine(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

cmdLineArgs, err := client.CMDLineArgs(r.Context())

if err != nil {
api_internal.EncodeError(w, r, err)
}

jsonData, err := json.Marshal(cmdLineArgs)

if err != nil {
fmt.Fprintf(w, "Unable to get version: %v", err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) Flags(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

flags, err := client.Flags(r.Context())

if err != nil {
api_internal.EncodeError(w, r, err)
}

jsonData, err := json.Marshal(flags)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) Logs(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

logs, err := client.LogFiles(r.Context())

if err != nil {
api_internal.EncodeError(w, r, err)
return
}

jsonData, err := json.Marshal(logs)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) Log(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)
client, err := h.findNodeClient(r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -220,31 +122,6 @@
client.Log(r.Context(), w, file, offset, limit, len(download) > 0)
}

func (h *APIHandler) DBs(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

dbs, err := client.DBs(r.Context())

if err != nil {
api_internal.EncodeError(w, r, err)
return
}

jsonData, err := json.Marshal(dbs)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) Tables(w http.ResponseWriter, r *http.Request) {
db, tables := path.Split(chi.URLParam(r, "*"))

Expand All @@ -253,7 +130,7 @@
return
}

client, err := h.findNodeClient(w, r)
client, err := h.findNodeClient(r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -278,7 +155,7 @@
}

func (h *APIHandler) ReOrg(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)
client, err := h.findNodeClient(r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -301,7 +178,7 @@
}

func (h *APIHandler) BodiesDownload(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)
client, err := h.findNodeClient(r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -312,7 +189,7 @@
}

func (h *APIHandler) HeadersDownload(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)
client, err := h.findNodeClient(r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -323,7 +200,7 @@
}

func (h *APIHandler) SyncStages(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)
client, err := h.findNodeClient(r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -347,109 +224,7 @@
w.Write(jsonData)
}

func (h *APIHandler) Peers(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

peers, err := client.FindPeers(r.Context())

if err != nil {
http.Error(w, fmt.Sprintf("Unable to fetch peers: %v", err), http.StatusInternalServerError)
return
}

jsonData, err := json.Marshal(peers)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)

}

func (h *APIHandler) Bootnodes(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

bootnodes, err := client.Bootnodes(r.Context())

if err != nil {
http.Error(w, fmt.Sprintf("Unable to fetch bootnodes: %v", err), http.StatusInternalServerError)
return
}

jsonData, err := json.Marshal(bootnodes)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)

}

func (h *APIHandler) ShanphotSync(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

snapSync, err := client.ShanphotSync(r.Context())

if err != nil {
http.Error(w, fmt.Sprintf("Unable to fetch snapshot sync data: %v", err), http.StatusInternalServerError)
return
}

jsonData, err := json.Marshal(snapSync)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) ShanphotFilesList(w http.ResponseWriter, r *http.Request) {
client, err := h.findNodeClient(w, r)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

snapSync, err := client.ShanphotFiles(r.Context())

if err != nil {
http.Error(w, fmt.Sprintf("Unable to fetch snapshot files list: %v", err), http.StatusInternalServerError)
return
}

jsonData, err := json.Marshal(snapSync)

if err != nil {
api_internal.EncodeError(w, r, err)
}

w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func (h *APIHandler) findNodeClient(w http.ResponseWriter, r *http.Request) (erigon_node.Client, error) {
func (h *APIHandler) findNodeClient(r *http.Request) (erigon_node.Client, error) {
sessionId := chi.URLParam(r, SessionId)
nodeId := chi.URLParam(r, NodeId)

Expand All @@ -471,7 +246,11 @@
func (h *APIHandler) UniversalRequest(w http.ResponseWriter, r *http.Request) {
apiStr := chi.URLParam(r, "*")

<<<<<<< HEAD

Check failure on line 249 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected <<, expected }

Check failure on line 249 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

expected statement, found '<<' (typecheck)

Check failure on line 249 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected <<, expected }
client, err := h.findNodeClient(r)
=======
client, err := h.findNodeClient(w, r)
>>>>>>> main

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -508,23 +287,20 @@
r.Get("/sessions/{sessionId}", r.GetSession)

// Erigon Node data
r.Get("/sessions/{sessionId}/nodes/{nodeId}/versions", r.Versions)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/cmdline", r.CMDLine)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/flags", r.Flags)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/logs", r.Logs)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/logs/{file}", r.Log)

r.Get("/sessions/{sessionId}/nodes/{nodeId}/dbs", r.DBs)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/dbs/*", r.Tables)

r.Get("/sessions/{sessionId}/nodes/{nodeId}/reorgs", r.ReOrg)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/bodies/download-summary", r.BodiesDownload)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/headers/download-summary", r.HeadersDownload)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/sync-stages", r.SyncStages)
<<<<<<< HEAD

Check failure on line 296 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected <<, expected } (typecheck)

Check failure on line 296 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

expected statement, found '<<' (typecheck)

Check failure on line 296 in api/api_handler.go

View workflow job for this annotation

GitHub Actions / build

syntax error: unexpected <<, expected }) (typecheck)

=======
r.Get("/sessions/{sessionId}/nodes/{nodeId}/peers", r.Peers)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/bootnodes", r.Bootnodes)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/snapshot-sync", r.ShanphotSync)
r.Get("/sessions/{sessionId}/nodes/{nodeId}/snapshot-files-list", r.ShanphotFilesList)
>>>>>>> main
r.Get("/v2/sessions/{sessionId}/nodes/{nodeId}/*", r.UniversalRequest)

return r
Expand Down
Loading
Loading