forked from abhishekkr/goshare
/
goshare.go
102 lines (87 loc) · 2.6 KB
/
goshare.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
100
101
102
package goshare
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"strconv"
"time"
golerror "github.com/abhishekkr/gol/golerror"
abkleveldb "github.com/abhishekkr/levigoNS/leveldb"
"github.com/jmhodges/levigo"
)
type Config map[string]*string
var (
db *levigo.DB
)
/* just a banner print */
func banner() {
fmt.Println("**************************************************")
fmt.Println(" ___ ____ ___ __ _ __")
fmt.Println(" | | | | | | / \\ | ) |")
fmt.Println(" | =| | | =~ |==| |==| |==| |= |=")
fmt.Println(" |__| |__| ___| | | | | | \\ |__")
fmt.Println("")
fmt.Println("**************************************************")
}
/* checking if you still wanna keep the goshare up */
func do_you_wanna_continue() {
var input string
for {
fmt.Println("Do you wanna exit. (yes|no):\n\n")
fmt.Scanf("%s", &input)
if input == "yes" || input == "y" {
break
}
}
}
/* config from flags */
func ConfigFromFlags() Config {
var config Config
config = make(Config)
config["dbpath"] = flag.String("dbpath", "/tmp/GO.DB", "the path to DB")
config["httpuri"] = flag.String("uri", "0.0.0.0", "what Port to Run HTTP Server at")
config["httpport"] = flag.String("port", "9999", "what Port to Run HTTP Server at")
config["req_port"] = flag.String("req-port", "9797", "what PORT to run ZMQ REQ at")
config["rep_port"] = flag.String("rep-port", "9898", "what PORT to run ZMQ REP at")
config["cpuprofile"] = flag.String("cpuprofile", "", "write cpu profile to file")
//flag.Parse()
return config
}
/*
putting together base engine for GoShare
dbpath, httpuri, httpport, rep_port, req_port *string
*/
func GoShareEngine(config Config) {
runtime.GOMAXPROCS(runtime.NumCPU())
// remember it will be same DB instance shared across goshare package
db = abkleveldb.CreateDB(*config["dbpath"])
if *config["cpuprofile"] != "" {
f, err := os.Create(*config["cpuprofile"])
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
go func() {
time.Sleep(100 * time.Second)
pprof.StopCPUProfile()
}()
}
_httpport, err_httpport := strconv.Atoi(*config["httpport"])
_req_port, err_req_port := strconv.Atoi(*config["req_port"])
_rep_port, err_rep_port := strconv.Atoi(*config["rep_port"])
if err_httpport == nil && err_rep_port == nil && err_req_port == nil {
go GoShareHTTP(*config["httpuri"], _httpport)
go GoShareZMQ(_req_port, _rep_port)
} else {
golerror.Boohoo("Port parameters to bind, error-ed while conversion to number.", true)
}
}
/* GoShare DB */
func GoShare() {
banner()
GoShareEngine(ConfigFromFlags())
do_you_wanna_continue()
}