Skip to content

Commit

Permalink
changed GetRoutes to WriteRoutes in RPC Module, changed config.Get() …
Browse files Browse the repository at this point in the history
…to config.GlobalConfig in Config Module'
  • Loading branch information
andrewnguyen22 committed Feb 4, 2019
1 parent 12268c4 commit ca8930b
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions config/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Build() {
// "dataDir" builds the directory for program files.
func dataDir() {
// attempts to make the data directory.
if err := os.MkdirAll(Get().DD, os.ModePerm); err != nil {
if err := os.MkdirAll(GlobalConfig().DD, os.ModePerm); err != nil {
// doesn't use custom logs, because they may or may not be available at this point
log.Fatalf(err.Error())
}
Expand All @@ -28,7 +28,7 @@ func dataDir() {
// "logsDir" builds the directory for logs.
func logsDir() {
// attempts to make the logs directory
if err := os.MkdirAll(Get().DD+_const.FILESEPARATOR+"logs", os.ModePerm); err != nil {
if err := os.MkdirAll(GlobalConfig().DD+_const.FILESEPARATOR+"logs", os.ModePerm); err != nil {
log.Fatal(err.Error())
}
}
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Init() {
// built in function to parse the flags above.
flag.Parse()
// returns the thread safe c of the client configuration.
Get()
GlobalConfig()
}

// "newConfiguration() is a constructor function of the configuration type.
Expand All @@ -71,8 +71,8 @@ func Print() {
fmt.Println("Pocket Core Configuration:\n", string(data)) // pretty print the pocket configuration
}

// "Get()" returns the configuration object in a thread safe manner.
func Get() *config { // singleton structure to return the configuration object
// "GlobalConfig()" returns the configuration object in a thread safe manner.
func GlobalConfig() *config { // singleton structure to return the configuration object
once.Do(func() { // thread safety.
newConfiguration()
})
Expand Down
2 changes: 1 addition & 1 deletion logs/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewLog(message string, level LogLevel, format LogFormat) {
// "Logger" prints the log to data directory
func Logger(l Log) error {
// open/create the new log file
f, err := os.OpenFile(config.Get().DD+_const.FILESEPARATOR+"logs"+_const.FILESEPARATOR+l.Name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
f, err := os.OpenFile(config.GlobalConfig().DD+_const.FILESEPARATOR+"logs"+_const.FILESEPARATOR+l.Name, os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

func Files() {
// Map.json
if err := ManualPeersFile(config.Get().PFile); err != nil { // add Map from file
if err := ManualPeersFile(config.GlobalConfig().PFile); err != nil { // add Map from file
logs.NewLog(err.Error(), logs.WaringLevel, logs.JSONLogFormat)
}
// chains.json
if err := CFIle(config.Get().CFile); err != nil {
if err := CFIle(config.GlobalConfig().CFile); err != nil {
logs.NewLog(err.Error(), logs.WaringLevel, logs.JSONLogFormat)
}
// whitelists for centralized dispatcher
Expand Down
2 changes: 1 addition & 1 deletion node/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Self() *Node {
if err != nil {
logs.NewLog(err.Error(), logs.FatalLevel, logs.JSONLogFormat)
}
self = &Node{GID: config.Get().GID, RelayPort: config.Get().RRPCPort, IP: ip}
self = &Node{GID: config.GlobalConfig().GID, RelayPort: config.GlobalConfig().RRPCPort, IP: ip}
})
return self
}
4 changes: 2 additions & 2 deletions node/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func (w *Whitelist) Count() int {

// "SWLFile" builds the service white list from a file.
func SWLFile() error {
return SWL().wlFile(config.Get().DWL)
return SWL().wlFile(config.GlobalConfig().DWL)
}

// "DWLFile" builds the develoeprs white list from a file.
func DWLFile() error {
return DWL().wlFile(config.Get().SNWL)
return DWL().wlFile(config.GlobalConfig().SNWL)
}

// "wlFile" builds a whitelist structure from a file.
Expand Down
8 changes: 4 additions & 4 deletions rpc/client/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// "Routes" is a function that returns all of the routes of the API.
func Routes() shared.Routes {
routes := shared.Routes{
shared.Route{Name: "Routes", Method: "GET", Path: "/v1/routes", HandlerFunc: GetRoutes},
shared.Route{Name: "Routes", Method: "GET", Path: "/v1/routes", HandlerFunc: WriteRoutes},
shared.Route{Name: "Register", Method: "POST", Path: "/v1/register", HandlerFunc: Register},
shared.Route{Name: "UnRegister", Method: "POST", Path: "/v1/unregister", HandlerFunc: UnRegister},
shared.Route{Name: "RegisterInfo", Method: "GET", Path: "/v1/register", HandlerFunc: RegisterInfo},
Expand Down Expand Up @@ -56,7 +56,7 @@ func Routes() shared.Routes {
return routes
}

// "GetRoutes" handles the localhost:<client-port>/routes call.
func GetRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
shared.GetRoutes(w, r, ps, Routes())
// "WriteRoutes" handles the localhost:<client-port>/routes call.
func WriteRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
shared.WriteRoutes(w, r, ps, Routes())
}
8 changes: 4 additions & 4 deletions rpc/relay/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Routes() shared.Routes {
routes := shared.Routes{
shared.Route{Name: "Version", Method: "GET", Path: "/v1", HandlerFunc: Version},
shared.Route{Name: "GetRoutes", Method: "GET", Path: "/v1/routes", HandlerFunc: GetRoutes},
shared.Route{Name: "WriteRoutes", Method: "GET", Path: "/v1/routes", HandlerFunc: WriteRoutes},
shared.Route{Name: "Report", Method: "POST", Path: "/v1/report", HandlerFunc: Report},
shared.Route{Name: "ReportInfo", Method: "GET", Path: "/v1/report", HandlerFunc: ReportInfo},
shared.Route{Name: "Dispatch", Method: "POST", Path: "/v1/dispatch", HandlerFunc: Dispatch},
Expand All @@ -22,7 +22,7 @@ func Routes() shared.Routes {
return routes
}

// "GetRoutes" handles the localhost:<relay-port>/routes call.
func GetRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
shared.GetRoutes(w,r,ps,Routes())
// "WriteRoutes" handles the localhost:<relay-port>/routes call.
func WriteRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
shared.WriteRoutes(w,r,ps,Routes())
}
8 changes: 4 additions & 4 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

// "StartServers" executes the specified configuration for the client.
func StartServers() {
if config.Get().CRPC { // if flag set
go StartClientRPC(config.Get().CRPCPort) // run the client rpc in a goroutine
if config.GlobalConfig().CRPC { // if flag set
go StartClientRPC(config.GlobalConfig().CRPCPort) // run the client rpc in a goroutine
}
if config.Get().RRPC { // if flag set
go StartRelayRPC(config.Get().RRPCPort) // run the relay rpc in a goroutine
if config.GlobalConfig().RRPC { // if flag set
go StartRelayRPC(config.GlobalConfig().RRPCPort) // run the relay rpc in a goroutine
}
}

Expand Down
6 changes: 3 additions & 3 deletions rpc/shared/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Route struct {
// "Routes" is a slice that holds all of the routes within one structure.
type Routes []Route

// "GetRoutes" handles the localhost:<relay-port>/routes call.
func GetRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params, routes Routes) {
// "WriteRoutes" handles the localhost:<relay-port>/routes call.
func WriteRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params, routes Routes) {
var paths []string
for _, v := range routes {
if v.Method != "GET" {
Expand All @@ -31,7 +31,7 @@ func GetRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params, rou
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
j, err := json.MarshalIndent(paths, "", " ")
if err != nil {
logs.NewLog("Unable to marshal GetRoutes to JSON", logs.ErrorLevel, logs.JSONLogFormat)
logs.NewLog("Unable to marshal WriteRoutes to JSON", logs.ErrorLevel, logs.JSONLogFormat)
}
WriteRawJSONResponse(w, j)
}
2 changes: 1 addition & 1 deletion tests/config/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLogsDir(t *testing.T) {
func TestDataDir(t *testing.T) {
config.Init()
config.Print()
datadir := config.Get().DD
datadir := config.GlobalConfig().DD
if datadir == _const.DATADIR {
t.Log(datadir)
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/node/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (

func TestDispatchLiveness(t *testing.T) {
// start API servers
go rpc.StartRelayRPC(config.Get().RRPCPort)
go rpc.StartRelayRPC(config.GlobalConfig().RRPCPort)
time.Sleep(time.Second)
// get peer list
pl := node.PeerList()
// create arbitrary nodes
self := node.Node{GID: "self", IP: "localhost", RelayPort: config.Get().RRPCPort}
self := node.Node{GID: "self", IP: "localhost", RelayPort: config.GlobalConfig().RRPCPort}
dead := node.Node{GID: "deadNode", IP: "0.0.0.0", RelayPort: "0"}
// add self to peerlist
pl.Add(self)
Expand Down
4 changes: 2 additions & 2 deletions tests/rpc/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func TestDispatchServe(t *testing.T) {
// json call string for dispatch serve
requestJSON := []byte("{\"DevID\": \"foo\", \"Blockchains\": [{\"name\":\"ethereum\",\"netid\":\"1\",\"version\":\"1.0\"}]}")
// start relay server
go http.ListenAndServe(":"+config.Get().RRPCPort, shared.Router(relay.Routes()))
go http.ListenAndServe(":"+config.GlobalConfig().RRPCPort, shared.Router(relay.Routes()))
// url for the POST request
u := "http://localhost:" + config.Get().RRPCPort + "/v1/dispatch/"
u := "http://localhost:" + config.GlobalConfig().RRPCPort + "/v1/dispatch/"
req, err := http.NewRequest("POST", u, bytes.NewBuffer(requestJSON))
if err != nil {
t.Fatalf(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions tests/rpc/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Unit test for APIReference
*/
func TestApiReference(t *testing.T) {
// Start server instance
go http.ListenAndServe(":"+config.Get().RRPCPort, shared.Router(relay.Routes()))
go http.ListenAndServe(":"+config.GlobalConfig().RRPCPort, shared.Router(relay.Routes()))
// @ Url
u := "http://localhost:" + config.Get().RRPCPort + "/v1/dispatch"
u := "http://localhost:" + config.GlobalConfig().RRPCPort + "/v1/dispatch"
// Send get request
resp, err := http.Get(u)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tests/rpc/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ Unit test for the relay functionality
func TestRelay(t *testing.T) {
node.DWL().Add("DEVID1")
// grab the hosted chains via file
if err := node.CFIle(config.Get().CFile); err != nil {
if err := node.CFIle(config.GlobalConfig().CFile); err != nil {
t.Fatalf(err.Error())
}
node.TestChains()
fmt.Println(node.Chains())
// Start server instance
go http.ListenAndServe(":"+config.Get().RRPCPort, shared.Router(relay.Routes()))
go http.ListenAndServe(":"+config.GlobalConfig().RRPCPort, shared.Router(relay.Routes()))
// @ Url
u := "http://localhost:" + config.Get().RRPCPort + "/v1/relay/"
u := "http://localhost:" + config.GlobalConfig().RRPCPort + "/v1/relay/"
// Setup relay
r := service.Relay{}
// add blockchain value
Expand Down
4 changes: 2 additions & 2 deletions tests/rpc/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
func TestReport(t *testing.T) {
report := service.Report{GID: "test", Message: "foo"}
// Start server instance
go http.ListenAndServe(":"+config.Get().RRPCPort, shared.Router(relay.Routes()))
go http.ListenAndServe(":"+config.GlobalConfig().RRPCPort, shared.Router(relay.Routes()))
// @ Url
u := "http://localhost:" + config.Get().RRPCPort + "/v1/report"
u := "http://localhost:" + config.GlobalConfig().RRPCPort + "/v1/report"
j, err := json.Marshal(report)
if err != nil {
t.Fatalf(err.Error())
Expand Down

0 comments on commit ca8930b

Please sign in to comment.