Skip to content

Commit

Permalink
implemented RPC as a service (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
andynog committed Jun 20, 2023
1 parent 90f7890 commit 3c9a97a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 106 deletions.
1 change: 1 addition & 0 deletions commands/ingest/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions commands/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ var RpcCmd = &cobra.Command{
}

func init() {
RpcCmd.AddCommand(StartCmd)
}
32 changes: 32 additions & 0 deletions commands/rpc/start.go
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() {
}
106 changes: 0 additions & 106 deletions insert.go

This file was deleted.

98 changes: 98 additions & 0 deletions rpc/service.go
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"))
}
}
}

0 comments on commit 3c9a97a

Please sign in to comment.