-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (56 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"path"
"github.com/g00dv1n/sol-rpc-router/pkg/router"
)
type ProxyServerConfig struct {
Port int `json:"port"`
Host string `json:"host,omitempty"`
RegularRpc router.Route `json:"regularRpc"`
DasRpc router.Route `json:"dasRpc"`
}
var logger *slog.Logger
func main() {
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
exDir, dirErr := os.Getwd()
if dirErr != nil {
log.Fatal(dirErr)
}
// Get Config Path
var configPath string
defaultConfigPath := path.Join(exDir, "proxy_config.json")
flag.StringVar(&configPath, "c", defaultConfigPath, "config file path")
flag.Parse()
configFileRaw, fileErr := os.ReadFile(configPath)
if fileErr != nil {
log.Fatalf("Can't open config file: %s", fileErr)
}
var proxyServerConfig ProxyServerConfig
jsonErr := json.Unmarshal(configFileRaw, &proxyServerConfig)
if jsonErr != nil {
log.Fatalf("Can't parse config: %s \n", jsonErr)
}
logger.Info("Config file loaded", "path", path.Base(configPath))
addr := fmt.Sprintf("%s:%d", proxyServerConfig.Host, proxyServerConfig.Port)
serverErr := NewProxyServer(addr, proxyServerConfig.RegularRpc, proxyServerConfig.DasRpc)
if serverErr != nil {
log.Fatal(serverErr)
}
}
func NewProxyServer(addr string, regular router.Route, das router.Route) error {
server := http.NewServeMux()
router, err := router.NewRouter(regular, das)
if err != nil {
return err
}
server.Handle("/", router)
logger.Info("Running proxy server", "addr", addr)
return http.ListenAndServe(addr, server)
}