kopia lustrzana https://github.com/bugout-dev/moonstream
Write user data in request context
rodzic
ca5bca10ae
commit
06d16457a0
|
@ -4,6 +4,7 @@ Server API middlewares.
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -109,6 +110,8 @@ func authMiddleware(next http.Handler) http.Handler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
ctxUser := context.WithValue(r.Context(), "user", userResponse)
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctxUser))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,13 @@ func pingRoute(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// lbHandler load balances the incoming requests to nodes
|
// lbHandler load balances the incoming requests to nodes
|
||||||
func lbHandler(w http.ResponseWriter, r *http.Request) {
|
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)
|
attempts := GetAttemptsFromContext(r)
|
||||||
if attempts > configs.NB_CONNECTION_RETRIES {
|
if attempts > configs.NB_CONNECTION_RETRIES {
|
||||||
log.Printf("Max attempts reached from %s %s, terminating\n", r.RemoteAddr, r.URL.Path)
|
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)
|
node.StatusReverseProxy.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)):
|
case strings.HasPrefix(r.URL.Path, fmt.Sprintf("/nb/%s/jsonrpc", blockchain)):
|
||||||
r.URL.Path = "/"
|
if user.ID == configs.BUGOUT_INTERNAL_CRAWLERS_USER_ID {
|
||||||
node.GethReverseProxy.ServeHTTP(w, r)
|
r.URL.Path = "/"
|
||||||
|
node.GethReverseProxy.ServeHTTP(w, r)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Fetch from db")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest)
|
http.Error(w, fmt.Sprintf("Unacceptable path for %s blockchain %s", blockchain, r.URL.Path), http.StatusBadRequest)
|
||||||
|
|
|
@ -110,3 +110,6 @@ var NB_CLIENT_NODE_KEEP_ALIVE = int64(5) // How long to store node in hot list f
|
||||||
|
|
||||||
// Humbug config
|
// Humbug config
|
||||||
var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN")
|
var HUMBUG_REPORTER_NODE_BALANCER_TOKEN = os.Getenv("HUMBUG_REPORTER_NODE_BALANCER_TOKEN")
|
||||||
|
|
||||||
|
// Database config
|
||||||
|
var MOONSTREAM_DB_URI = os.Getenv("MOONSTREAM_DB_URI")
|
||||||
|
|
|
@ -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 MOONSTREAM_NODES_SERVER_PORT="<node_status_server_port>"
|
||||||
export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="<bugout_humbug_token_for_crash_reports>"
|
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
|
# Ethereum nodes depends variables
|
||||||
export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="<node_geth_http_ip_addr>"
|
export MOONSTREAM_NODE_ETHEREUM_A_IPC_ADDR="<node_geth_http_ip_addr>"
|
||||||
export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="<node_geth_http_ip_addr>"
|
export MOONSTREAM_NODE_ETHEREUM_B_IPC_ADDR="<node_geth_http_ip_addr>"
|
||||||
|
|
Ładowanie…
Reference in New Issue