-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"strconv" | ||
) | ||
|
||
// TODO: make this configurable via a config or parameter | ||
const connString = "postgres://postgres:[email protected]:15432/postgres?sslmode=disable" | ||
|
||
// StartCmd start ingest service | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ var RpcCmd = &cobra.Command{ | |
} | ||
|
||
func init() { | ||
RpcCmd.AddCommand(StartCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package rpc | ||
|
||
import ( | ||
"github.com/cometbft/rpc-companion/rpc" | ||
"github.com/spf13/cobra" | ||
"log" | ||
) | ||
|
||
// TODO: make this configurable via a config or parameter | ||
const connString = "postgres://postgres:[email protected]:15432/postgres?sslmode=disable" | ||
|
||
// StartCmd start RPC service | ||
var StartCmd = &cobra.Command{ | ||
Use: "start", | ||
Short: "Start RPC Service", | ||
Long: `The start command runs an instance of the RPC Service`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
//Instantiate a new Ingest Service | ||
rpcSvc := rpc.NewService(connString) | ||
err := rpcSvc.Serve() | ||
if err != nil { | ||
log.Fatalln("There's an error starting the RPC service:", err) | ||
} else { | ||
log.Println("Started RPC service...") | ||
} | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package rpc | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/cometbft/cometbft/rpc/jsonrpc/types" | ||
"github.com/cometbft/rpc-companion/storage" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type Service struct { | ||
Storage storage.IStorage | ||
} | ||
|
||
func NewService(connStr string) Service { | ||
|
||
// Database | ||
db := storage.PostgresStorage{ | ||
ConnectionString: connStr, | ||
} | ||
|
||
// Return an Ingest Service | ||
return Service{ | ||
Storage: &db, | ||
} | ||
} | ||
|
||
func (s *Service) Serve() error { | ||
|
||
// Handler for the block endpoint | ||
http.HandleFunc("/v1/block", s.handleBlock) | ||
|
||
// Start the service | ||
err := http.ListenAndServe(":8080", nil) // TODO: Make the port configurable | ||
if err != nil { | ||
return err | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
// Handles the '/v1/block' endpoint | ||
func (s *Service) handleBlock(writer http.ResponseWriter, request *http.Request) { | ||
writer.Header().Set("Content-Type", "application/json") | ||
conn, err := s.Storage.Connect() | ||
defer conn.Close() | ||
if err != nil { | ||
log.Println("Error connecting to storage in handleBlock: ", err) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
writer.Write([]byte("Internal Server Error")) | ||
} else { | ||
if request.Method == "GET" { | ||
h := request.URL.Query()["height"][0] | ||
height, err := strconv.ParseInt(h, 10, 64) | ||
if err != nil { | ||
writer.WriteHeader(http.StatusBadRequest) | ||
writer.Write([]byte("Bad Request. Invalid height")) | ||
} | ||
fmt.Printf("Block Request. Height: %v\n", height) | ||
block, err := s.Storage.GetBlock(height) | ||
if err != nil { | ||
// TODO: If not records retrieved return a different status | ||
log.Println("Error retrieving record from storage in handleBlock: ", err) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
writer.Write([]byte("Internal Server Error")) | ||
} | ||
// Return response | ||
blockJSON, err := json.Marshal(block) | ||
id := types.JSONRPCStringID("id") | ||
if err != nil { | ||
log.Println("Error marshalling block: ", err) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
writer.Write([]byte("Internal Server Error")) | ||
} else { | ||
RPCResponse := types.RPCResponse{ | ||
JSONRPC: "2.0", | ||
ID: id, | ||
Result: blockJSON, | ||
Error: nil, | ||
} | ||
resp, err := json.Marshal(RPCResponse) | ||
if err != nil { | ||
log.Println("Error marshalling RPCResponse: ", err) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
writer.Write([]byte("Internal Server Error")) | ||
} else { | ||
writer.WriteHeader(http.StatusOK) | ||
writer.Write(resp) | ||
} | ||
} | ||
} else { | ||
writer.WriteHeader(http.StatusBadRequest) | ||
writer.Write([]byte("Bad Request")) | ||
} | ||
} | ||
} |