-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #2 Signed-off-by: talwinder.kaur <talwinder.kaur@securekey.com>
- Loading branch information
1 parent
b49615a
commit 4ec9046
Showing
19 changed files
with
1,137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module github.com/trustbloc/edge-service/cmd/vc-rest | ||
|
||
replace github.com/trustbloc/edge-service => ../.. | ||
|
||
require ( | ||
github.com/gorilla/mux v1.7.3 | ||
github.com/spf13/cobra v0.0.5 | ||
github.com/stretchr/testify v1.4.0 | ||
github.com/trustbloc/edge-service v0.0.0 | ||
) | ||
|
||
go 1.13 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/trustbloc/edge-service/cmd/vc-rest/startcmd" | ||
) | ||
|
||
func main() { | ||
rootCmd := &cobra.Command{ | ||
Use: "vc-rest", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.HelpFunc()(cmd, args) | ||
}, | ||
} | ||
|
||
rootCmd.AddCommand(startcmd.GetStartCmd(&startcmd.HTTPServer{})) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatalf("Failed to run vc-rest: %s", err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
// Correct behaviour is for main to finish with exit code 0. | ||
// This test fails otherwise. However, this can't be checked by the unit test framework. The *testing.T argument is | ||
// only there so that this test gets picked up by the framework but otherwise we don't need it. | ||
func TestWithoutUserAgs(t *testing.T) { //nolint - see above | ||
setUpArgs() | ||
main() | ||
} | ||
|
||
// Strips out the extra args that the unit test framework adds | ||
// This allows main() to execute as if it was called directly from the command line | ||
func setUpArgs() { | ||
os.Args = os.Args[:1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package startcmd | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/trustbloc/edge-service/pkg/restapi/vc/operation" | ||
cmdutils "github.com/trustbloc/edge-service/pkg/utils/cmd" | ||
) | ||
|
||
const ( | ||
hostURLFlagName = "host-url" | ||
hostURLFlagShorthand = "u" | ||
hostURLFlagUsage = "URL to run the vc-rest instance on. Format: HostName:Port." | ||
hostURLEnvKey = "VC_REST_HOST_URL" | ||
) | ||
|
||
var errMissingHostURL = errors.New("host URL not provided") | ||
|
||
type vcRestParameters struct { | ||
srv server | ||
hostURL string | ||
} | ||
|
||
type server interface { | ||
ListenAndServe(host string, router http.Handler) error | ||
} | ||
|
||
// HTTPServer represents an actual HTTP server implementation. | ||
type HTTPServer struct{} | ||
|
||
// ListenAndServe starts the server using the standard Go HTTP server implementation. | ||
func (s *HTTPServer) ListenAndServe(host string, router http.Handler) error { | ||
return http.ListenAndServe(host, router) | ||
} | ||
|
||
// GetStartCmd returns the Cobra start command. | ||
func GetStartCmd(srv server) *cobra.Command { | ||
startCmd := createStartCmd(srv) | ||
|
||
createFlags(startCmd) | ||
|
||
return startCmd | ||
} | ||
|
||
func createStartCmd(srv server) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "start", | ||
Short: "Start vc-rest", | ||
Long: "Start vc-rest inside the edge-service", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
hostURL, err := cmdutils.GetUserSetVar(cmd, hostURLFlagName, hostURLEnvKey) | ||
if err != nil { | ||
return err | ||
} | ||
parameters := &vcRestParameters{ | ||
srv: srv, | ||
hostURL: hostURL, | ||
} | ||
return startEdgeService(parameters) | ||
}, | ||
} | ||
} | ||
|
||
func createFlags(startCmd *cobra.Command) { | ||
startCmd.Flags().StringP(hostURLFlagName, hostURLFlagShorthand, "", hostURLFlagUsage) | ||
} | ||
|
||
func startEdgeService(parameters *vcRestParameters) error { | ||
if parameters.hostURL == "" { | ||
return errMissingHostURL | ||
} | ||
|
||
vcService := operation.New() | ||
|
||
handlers := vcService.GetRESTHandlers() | ||
router := mux.NewRouter() | ||
|
||
for _, handler := range handlers { | ||
router.HandleFunc(handler.Path(), handler.Handle()).Methods(handler.Method()) | ||
} | ||
|
||
err := parameters.srv.ListenAndServe(parameters.hostURL, router) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package startcmd | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockServer struct{} | ||
|
||
func (s *mockServer) ListenAndServe(host string, handler http.Handler) error { | ||
return nil | ||
} | ||
|
||
func TestStartCmdContents(t *testing.T) { | ||
startCmd := GetStartCmd(&mockServer{}) | ||
|
||
require.Equal(t, "start", startCmd.Use) | ||
require.Equal(t, "Start vc-rest", startCmd.Short) | ||
require.Equal(t, "Start vc-rest inside the edge-service", startCmd.Long) | ||
|
||
checkFlagPropertiesCorrect(t, startCmd, hostURLFlagName, hostURLFlagShorthand, hostURLFlagUsage) | ||
} | ||
|
||
func TestStartCmdWithBlankHostArg(t *testing.T) { | ||
startCmd := GetStartCmd(&mockServer{}) | ||
|
||
args := []string{"--" + hostURLFlagName, ""} | ||
startCmd.SetArgs(args) | ||
|
||
err := startCmd.Execute() | ||
|
||
require.Equal(t, errMissingHostURL.Error(), err.Error()) | ||
} | ||
|
||
func TestStartCmdWithMissingHostArg(t *testing.T) { | ||
startCmd := GetStartCmd(&mockServer{}) | ||
|
||
err := startCmd.Execute() | ||
|
||
require.Equal(t, | ||
"Neither host-url (command line flag) nor VC_REST_HOST_URL (environment variable) have been set.", | ||
err.Error()) | ||
} | ||
|
||
func TestStartEdgeStoreWithBlankHost(t *testing.T) { | ||
parameters := &vcRestParameters{hostURL: ""} | ||
|
||
err := startEdgeService(parameters) | ||
require.NotNil(t, err) | ||
require.Equal(t, errMissingHostURL, err) | ||
} | ||
|
||
func TestStartCmdValidArgs(t *testing.T) { | ||
startCmd := GetStartCmd(&mockServer{}) | ||
|
||
args := []string{"--" + hostURLFlagName, "localhost:8080"} | ||
startCmd.SetArgs(args) | ||
|
||
err := startCmd.Execute() | ||
|
||
require.Nil(t, err) | ||
} | ||
|
||
func TestStartCmdValidArgsEnvVar(t *testing.T) { | ||
startCmd := GetStartCmd(&mockServer{}) | ||
|
||
err := os.Setenv(hostURLEnvKey, "localhost:8080") | ||
require.Nil(t, err) | ||
|
||
err = startCmd.Execute() | ||
|
||
require.Nil(t, err) | ||
} | ||
|
||
func checkFlagPropertiesCorrect(t *testing.T, cmd *cobra.Command, flagName, flagShorthand, flagUsage string) { | ||
flag := cmd.Flag(flagName) | ||
|
||
require.NotNil(t, flag) | ||
require.Equal(t, flagName, flag.Name) | ||
require.Equal(t, flagShorthand, flag.Shorthand) | ||
require.Equal(t, flagUsage, flag.Usage) | ||
require.Equal(t, "", flag.Value.String()) | ||
|
||
flagAnnotations := flag.Annotations | ||
require.Nil(t, flagAnnotations) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.