Skip to content

Commit

Permalink
Lcd (#11)
Browse files Browse the repository at this point in the history
* add lcd/api server
  • Loading branch information
baabeetaa authored Mar 29, 2023
1 parent 0c7efec commit 0756378
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
15 changes: 15 additions & 0 deletions api_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"log"
"net/http"
)

func StartApiServer() {
handler := func(w http.ResponseWriter, r *http.Request) {

}
// handle all requests to your server using the proxy
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":1337", nil))
}
2 changes: 2 additions & 0 deletions cmd_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ func startCmd() *cobra.Command {
Short: "start subnode server",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
Init()
StartRpcServer()
StartApiServer()
return nil
},
}
Expand Down
20 changes: 3 additions & 17 deletions rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,11 @@ import (
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
)

func StartRpcServer() {
hostProxy := make(map[string]*httputil.ReverseProxy)

InitPool()

for _, s := range Pool {
target, err := url.Parse(s.Name)
if err != nil {
panic(err)
}
hostProxy[s.Name] = httputil.NewSingleHostReverseProxy(target)
}

handler := func(w http.ResponseWriter, r *http.Request) {
prunedNode := SelectPrunedNode()
selectedHost := prunedNode.Backend.Rpc // default to pruned node
Expand Down Expand Up @@ -76,7 +62,7 @@ func StartRpcServer() {
}

r.Host = r.URL.Host
hostProxy[selectedHost].ServeHTTP(w, r)
ProxyMap[selectedHost].ServeHTTP(w, r)
} else if r.Method == "POST" { // JSONRPC over HTTP
body, err := io.ReadAll(r.Body)
if err != nil {
Expand Down Expand Up @@ -157,15 +143,15 @@ func StartRpcServer() {

r.Body = io.NopCloser(bytes.NewBuffer(body)) // assign a new body with previous byte slice
r.Host = r.URL.Host
hostProxy[selectedHost].ServeHTTP(w, r)
ProxyMap[selectedHost].ServeHTTP(w, r)
} else {
SendError(w)
}
}

// handle all requests to your server using the proxy
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
log.Fatal(http.ListenAndServe(":26657", nil))
}

func SendError(w http.ResponseWriter) {
Expand Down
17 changes: 16 additions & 1 deletion state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"time"
)
Expand All @@ -19,9 +21,22 @@ type BackendState struct {
}

var (
Pool []*BackendState
Pool []*BackendState
ProxyMap = make(map[string]*httputil.ReverseProxy)
)

func Init() {
InitPool()

for _, s := range Pool {
target, err := url.Parse(s.Name)
if err != nil {
panic(err)
}
ProxyMap[s.Name] = httputil.NewSingleHostReverseProxy(target)
}
}

func InitPool() {
Pool = Pool[:0] // Remove all elements

Expand Down

0 comments on commit 0756378

Please sign in to comment.