diff options
Diffstat (limited to 'pkg/backend')
-rw-r--r-- | pkg/backend/api.go | 18 | ||||
-rw-r--r-- | pkg/backend/init.go | 12 | ||||
-rw-r--r-- | pkg/backend/mocks.go | 15 |
3 files changed, 27 insertions, 18 deletions
diff --git a/pkg/backend/api.go b/pkg/backend/api.go index a19fd40..fea38db 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -45,10 +45,18 @@ func SubscribeToEvent(event string, f unsafe.Pointer) { subscribe(event, f) } -func InitializeBitmaskContext() { +type InitOpts struct { + Provider string + AppName string + SkipLaunch bool +} + +func InitializeBitmaskContext(opts *InitOpts) { p := bitmask.GetConfiguredProvider() + opts.Provider = p.Provider + opts.AppName = p.AppName - initOnce.Do(func() { initializeContext(p.Provider, p.AppName) }) + initOnce.Do(func() { initializeContext(opts) }) runDonationReminder() go ctx.updateStatus() } @@ -62,7 +70,7 @@ func InstallHelpers() { pickle.InstallHelpers() } -func MockUIInteraction() { - log.Println("mocking ui interaction on port 8080. \nTry 'curl localhost:8080/{on|off|failed}' to toggle status.") - go mockUI() +func EnableMockBackend() { + log.Println("[+] Mocking ui interaction on port 8080. \nTry 'curl localhost:8080/{on|off|failed}' to toggle status.") + go enableMockBackend() } diff --git a/pkg/backend/init.go b/pkg/backend/init.go index 5abb05e..79efdc7 100644 --- a/pkg/backend/init.go +++ b/pkg/backend/init.go @@ -12,11 +12,11 @@ import ( // initializeContext initializes an empty connStatus and assigns it to the // global ctx holder. This is expected to be called only once, so the public // api uses the sync.Once primitive to call this. -func initializeContext(provider, appName string) { +func initializeContext(opts *InitOpts) { var st status = off ctx = &connectionCtx{ - AppName: appName, - Provider: provider, + AppName: opts.AppName, + Provider: opts.Provider, TosURL: config.TosURL, HelpURL: config.HelpURL, DonateURL: config.DonateURL, @@ -28,7 +28,7 @@ func initializeContext(provider, appName string) { errCh := make(chan string) go trigger(OnStatusChanged) go checkErrors(errCh) - initializeBitmask(errCh) + initializeBitmask(errCh, opts) } func checkErrors(errCh chan string) { @@ -39,14 +39,14 @@ func checkErrors(errCh chan string) { } } -func initializeBitmask(errCh chan string) { +func initializeBitmask(errCh chan string, opts *InitOpts) { if ctx == nil { log.Println("bug: cannot initialize bitmask, ctx is nil!") os.Exit(1) } bitmask.InitializeLogger() - b, err := bitmask.InitializeBitmask() + b, err := bitmask.InitializeBitmask(opts.SkipLaunch) if err != nil { log.Println("error: cannot initialize bitmask") errCh <- err.Error() diff --git a/pkg/backend/mocks.go b/pkg/backend/mocks.go index a8ede73..226fa4e 100644 --- a/pkg/backend/mocks.go +++ b/pkg/backend/mocks.go @@ -9,6 +9,14 @@ import ( * should also show a good way of writing functionality tests just for the Qml * layer */ +func enableMockBackend() { + log.Println("[+] You should not use this in production!") + http.HandleFunc("/on", mockUIOn) + http.HandleFunc("/off", mockUIOff) + http.HandleFunc("/failed", mockUIFailed) + http.ListenAndServe(":8080", nil) +} + func mockUIOn(w http.ResponseWriter, r *http.Request) { log.Println("changing status: on") setStatus(on) @@ -23,10 +31,3 @@ func mockUIFailed(w http.ResponseWriter, r *http.Request) { log.Println("changing status: failed") setStatus(failed) } - -func mockUI() { - http.HandleFunc("/on", mockUIOn) - http.HandleFunc("/off", mockUIOff) - http.HandleFunc("/failed", mockUIFailed) - http.ListenAndServe(":8080", nil) -} |