Tested version of new db status server version

pull/359/head
kompotkot 2021-11-03 10:27:44 +00:00
rodzic 96af47c86f
commit 33affe4e9f
4 zmienionych plików z 18 dodań i 9 usunięć

Wyświetl plik

@ -1,13 +1,12 @@
package cmd
import (
"fmt"
"log"
"net/http"
"strings"
"strings"
"time"
settings "github.com/bugout-dev/moonstream/db/server/configs"
settings "github.com/bugout-dev/moonstream/db/server/configs"
)
// Handle panic errors to prevent server shutdown
@ -29,7 +28,7 @@ func logsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
fmt.Printf("[%s] %s %s %s\n", time.Since(start), r.Method, r.URL.Path, r.RemoteAddr)
log.Printf("%s %s %s %s\n", time.Since(start), r.Method, r.URL.Path, r.RemoteAddr)
})
}
@ -38,7 +37,7 @@ func corsMiddleware(next http.Handler) http.Handler {
// Iterate over list of allowed origins
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, allowedOrigin := range strings.Split(settings.MOONSTREAM_CORS_ALLOWED_ORIGINS, ",") {
if r.Header.Get("Origin") == allowedOrigin {
if r.Header.Get("Origin") == allowedOrigin {
w.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
}
}

Wyświetl plik

@ -1,7 +1,9 @@
package cmd
import (
"database/sql"
"encoding/json"
"log"
"net/http"
)
@ -16,8 +18,17 @@ func (es *extendedServer) blocksLatestRoute(w http.ResponseWriter, req *http.Req
w.Header().Set("Content-Type", "application/json")
var latestBlock BlockNumberResponse
query := "SELECT block_number FROM ethereum_blocks ORDER BY block_number DESC LIMIT 1"
es.db.Raw(query, 1).Scan(&latestBlock.BlockNumber)
row := es.db.QueryRow("SELECT block_number FROM ethereum_blocks ORDER BY block_number DESC LIMIT 1")
err := row.Scan(&latestBlock.BlockNumber)
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "Row not found", http.StatusNotFound)
} else {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
log.Printf("An error occurred during sql operation: %s", err)
return
}
json.NewEncoder(w).Encode(latestBlock)
}

Wyświetl plik

@ -5,7 +5,7 @@ import (
"flag"
"log"
"net/http"
"time"
"time"
)
type extendedServer struct {

Wyświetl plik

@ -1,3 +1,2 @@
export MOONSTREAM_DB_SERVER_PORT="8080"
export MOONSTREAM_DB_URI="postgresql://<username>:<password>@<db_host>:<db_port>/<db_name>"
export MOONSTREAM_CORS_ALLOWED_ORIGINS="http://localhost:3000,https://moonstream.to,https://www.moonstream.to,https://alpha.moonstream.to"