Write user data in request context

pull/559/head
kompotkot 2022-03-09 11:07:53 +00:00
rodzic ca5bca10ae
commit 06d16457a0
4 zmienionych plików z 23 dodań i 3 usunięć

Wyświetl plik

@ -4,6 +4,7 @@ Server API middlewares.
package cmd
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
@ -109,6 +110,8 @@ func authMiddleware(next http.Handler) http.Handler {
return
}
next.ServeHTTP(w, r)
ctxUser := context.WithValue(r.Context(), "user", userResponse)
next.ServeHTTP(w, r.WithContext(ctxUser))
})
}

Wyświetl plik

@ -22,6 +22,13 @@ func pingRoute(w http.ResponseWriter, r *http.Request) {
// lbHandler load balances the incoming requests to nodes
func lbHandler(w http.ResponseWriter, r *http.Request) {
userRaw := r.Context().Value("user")
user, ok := userRaw.(BugoutUserResponse)
if !ok {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
attempts := GetAttemptsFromContext(r)
if attempts > configs.NB_CONNECTION_RETRIES {
log.Printf("Max attempts reached from %s %s, terminating\n", r.RemoteAddr, r.URL.Path)
@ -73,8 +80,12 @@ func lbHandler(w http.ResponseWriter, r *http.Request) {
node.StatusReverseProxy.ServeHTTP(w, r)
return
case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)):
r.URL.Path = "/"
node.GethReverseProxy.ServeHTTP(w, r)
if user.ID == configs.BUGOUT_INTERNAL_CRAWLERS_USER_ID {
r.URL.Path = "/"
node.GethReverseProxy.ServeHTTP(w, r)
} else {
fmt.Println("Fetch from db")
}
return
default:
http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest)

Wyświetl plik

@ -110,3 +110,6 @@ var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // How long to store node in hot list f
// Humbug config
var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN")
// Database config
var MOONSTREAM_DB_URI = os.Getenv("MOONSTREAM_DB_URI")

Wyświetl plik

@ -5,6 +5,9 @@ export BUGOUT_INTERNAL_CRAWLERS_USER_ID="<application_user_id_of_moonstream_inte
export MOONSTREAM_NODES_SERVER_PORT="<node_status_server_port>"
export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="<bugout_humbug_token_for_crash_reports>"
# Database variables
export MOONSTREAM_DB_URI="postgresql://<username>:<password>@<db_host>:<db_port>/<db_name>"
# Ethereum nodes depends variables
export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="<node_geth_http_ip_addr>"
export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="<node_geth_http_ip_addr>"