-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (86 loc) · 2.09 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
gojsonrpc "github.com/filecoin-project/go-jsonrpc"
"github.com/gorilla/mux"
"github.com/urfave/cli/v2"
conn "gojsonrpc/DBConnection"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
)
type RPC struct {
n int
}
func (r *RPC) GetMessage(key string) ([]string, error) {
fmt.Println("***GetMessage***")
message, err := conn.GetFromDB([]byte(key))
if err != nil {
return nil, err
}
str := strings.Split(string(message), "|")
//removing the last element bcz empty above line will give empty value at last index
str = str[:len(str)-1]
return str, nil
}
func (r *RPC) AddKeyMessage(key, msg string) (string, error) {
fmt.Println("***AddMessage*** ")
msg = msg + "|"
err := conn.AddToDB([]byte(key), []byte(msg))
if err != nil {
return "Unable to receive message", err
}
fmt.Printf("\n%s\n", "****called add message****")
return "Successfully received the message", nil
}
func (R *RPC) DeleteALl() error {
fmt.Println("***DRoping All*** ")
err := conn.DropAll()
if err != nil {
return err
}
return nil
}
var port string
var DbFlag = false
var ptr = &DbFlag
func main() {
app := cli.NewApp()
app.Name = "GO_JSONRPC_CLI_BADGER"
app.Authors = []*cli.Author{
{Name: "Tikaryan", Email: "[email protected]"},
}
// Receiving port number to run the server at that port
app.Flags = []cli.Flag{
&cli.StringFlag{Destination: &port, Name: "port", Usage: "To get the specific ports which needs to run"},
}
//Calling server based on command
app.Commands = []*cli.Command{
{Name: "start", Aliases: []string{"st"}, Action: serverStart, Description: "running server"},
}
err := app.Run(os.Args)
if err != nil {
log.Fatalln(err)
}
if *ptr {
defer conn.CloseDB()
}
}
func serverStart(ctx *cli.Context) error {
var gorpc = &RPC{}
rpcServer := gojsonrpc.NewServer()
rpcServer.Register("GORPC", gorpc)
route := mux.NewRouter()
fmt.Println("**starting-server***")
route.Handle("/", rpcServer)
httptest.NewServer(route)
//COnnect DB
ptr = conn.DbConnect(ptr)
err := http.ListenAndServe(":"+port, route)
if err != nil {
return err
}
return nil
}