Skip to content

Commit

Permalink
Decouple public packages (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 authored Jun 23, 2023
1 parent 80ce036 commit d94cee0
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 56 deletions.
12 changes: 10 additions & 2 deletions service/client.go → public/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package service
package public

import (
"bytes"
Expand All @@ -15,7 +15,7 @@ import (
"net/url"
"time"

"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/public/job"
)

var (
Expand Down Expand Up @@ -431,3 +431,11 @@ func (c *Client) GetVersionInfo() (VersionInfo, error) {

return info, nil
}

func (c *Client) AuthToken() string {
return c.authToken
}

func (c *Client) URL() string {
return c.cfg.httpURL
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions public/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package public

import (
"github.com/mattermost/mattermost/server/public/shared/mlog"
)

type VersionInfo struct {
BuildDate string `json:"buildDate"`
BuildVersion string `json:"buildVersion"`
BuildHash string `json:"buildHash"`
GoVersion string `json:"goVersion"`
}

func (v VersionInfo) LogFields() []mlog.Field {
return []mlog.Field{
mlog.String("buildDate", v.BuildDate),
mlog.String("buildVersion", v.BuildVersion),
mlog.String("buildHash", v.BuildHash),
mlog.String("goVersion", v.GoVersion),
}
}
41 changes: 21 additions & 20 deletions service/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"testing"

"github.com/mattermost/calls-offloader/public"
"github.com/mattermost/calls-offloader/service/auth"
"github.com/mattermost/calls-offloader/service/random"

Expand All @@ -16,42 +17,42 @@ import (

func TestNewClient(t *testing.T) {
t.Run("empty config", func(t *testing.T) {
c, err := NewClient(ClientConfig{})
c, err := public.NewClient(public.ClientConfig{})
require.Error(t, err)
require.Equal(t, "failed to parse config: invalid URL value: should not be empty", err.Error())
require.Nil(t, c)
})

t.Run("invalid url", func(t *testing.T) {
c, err := NewClient(ClientConfig{URL: "not_a_url"})
c, err := public.NewClient(public.ClientConfig{URL: "not_a_url"})
require.Error(t, err)
require.Equal(t, "failed to parse config: invalid url host: should not be empty", err.Error())
require.Nil(t, c)
})

t.Run("invalid scheme", func(t *testing.T) {
c, err := NewClient(ClientConfig{URL: "ftp://invalid"})
c, err := public.NewClient(public.ClientConfig{URL: "ftp://invalid"})
require.Error(t, err)
require.Equal(t, `failed to parse config: invalid url scheme: "ftp" is not valid`, err.Error())
require.Nil(t, c)
})

t.Run("success http scheme", func(t *testing.T) {
apiURL := "http://localhost"
c, err := NewClient(ClientConfig{URL: apiURL})
c, err := public.NewClient(public.ClientConfig{URL: apiURL})
require.NoError(t, err)
require.NotNil(t, c)
require.NotEmpty(t, c)
require.Equal(t, apiURL, c.cfg.httpURL)
require.Equal(t, apiURL, c.URL())
})

t.Run("success https scheme", func(t *testing.T) {
apiURL := "https://localhost"
c, err := NewClient(ClientConfig{URL: apiURL})
c, err := public.NewClient(public.ClientConfig{URL: apiURL})
require.NoError(t, err)
require.NotNil(t, c)
require.NotEmpty(t, c)
require.Equal(t, apiURL, c.cfg.httpURL)
require.Equal(t, apiURL, c.URL())
})

t.Run("custom dialing function", func(t *testing.T) {
Expand All @@ -62,11 +63,11 @@ func TestNewClient(t *testing.T) {
}

apiURL := "http://localhost"
c, err := NewClient(ClientConfig{URL: apiURL}, WithDialFunc(dialFn))
c, err := public.NewClient(public.ClientConfig{URL: apiURL}, public.WithDialFunc(dialFn))
require.NoError(t, err)
require.NotNil(t, c)
require.NotEmpty(t, c)
require.Equal(t, apiURL, c.cfg.httpURL)
require.Equal(t, apiURL, c.URL())

_ = c.Register("", "")

Expand All @@ -78,7 +79,7 @@ func TestClientRegister(t *testing.T) {
th := SetupTestHelper(t, nil)
defer th.Teardown()

c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey,
})
Expand Down Expand Up @@ -116,7 +117,7 @@ func TestClientRegister(t *testing.T) {
})

t.Run("unauthorized", func(t *testing.T) {
c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey + "_",
})
Expand All @@ -126,11 +127,11 @@ func TestClientRegister(t *testing.T) {

err = c.Register("", "")
require.Error(t, err)
require.Equal(t, ErrUnauthorized, err)
require.Equal(t, public.ErrUnauthorized, err)
})

t.Run("self registering", func(t *testing.T) {
c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
})
require.NoError(t, err)
Expand All @@ -141,7 +142,7 @@ func TestClientRegister(t *testing.T) {
require.NoError(t, err)
err = c.Register("clientB", authKey)
require.Error(t, err)
require.Equal(t, ErrUnauthorized, err)
require.Equal(t, public.ErrUnauthorized, err)

th.srvc.cfg.API.Security.AllowSelfRegistration = true
err = c.Register("clientB", authKey)
Expand All @@ -153,7 +154,7 @@ func TestClientUnregister(t *testing.T) {
th := SetupTestHelper(t, nil)
defer th.Teardown()

c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey,
})
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestClientUnregister(t *testing.T) {
})

t.Run("unauthorized", func(t *testing.T) {
c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey + "_",
})
Expand All @@ -195,15 +196,15 @@ func TestClientUnregister(t *testing.T) {

err = c.Unregister("clientA")
require.Error(t, err)
require.Equal(t, ErrUnauthorized, err)
require.Equal(t, public.ErrUnauthorized, err)
})
}

func TestClientLogin(t *testing.T) {
th := SetupTestHelper(t, nil)
defer th.Teardown()

c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey,
})
Expand All @@ -220,7 +221,7 @@ func TestClientLogin(t *testing.T) {

err = c.Login("clientA", authKey)
require.NoError(t, err)
require.NotEmpty(t, c.authToken)
require.NotEmpty(t, c.AuthToken())
})

t.Run("not found", func(t *testing.T) {
Expand All @@ -246,7 +247,7 @@ func TestClientGetVersionInfo(t *testing.T) {
th := SetupTestHelper(t, nil)
defer th.Teardown()

c, err := NewClient(ClientConfig{
c, err := public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey,
})
Expand Down
2 changes: 1 addition & 1 deletion service/docker/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"runtime"
"time"

"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/public/job"
"github.com/mattermost/calls-offloader/service/random"

recorder "github.com/mattermost/calls-recorder/cmd/recorder/config"
Expand Down
2 changes: 1 addition & 1 deletion service/docker/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/public/job"

"github.com/mattermost/mattermost/server/public/shared/mlog"

Expand Down
12 changes: 7 additions & 5 deletions service/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
package service

import (
"github.com/mattermost/calls-offloader/logger"
"github.com/mattermost/calls-offloader/service/api"
"github.com/mattermost/calls-offloader/service/auth"
"net"
"os"
"testing"

"github.com/mattermost/calls-offloader/logger"
"github.com/mattermost/calls-offloader/public"
"github.com/mattermost/calls-offloader/service/api"
"github.com/mattermost/calls-offloader/service/auth"

"github.com/stretchr/testify/require"
)

type TestHelper struct {
srvc *Service
adminClient *Client
adminClient *public.Client
cfg Config
tb testing.TB
apiURL string
Expand Down Expand Up @@ -49,7 +51,7 @@ func SetupTestHelper(tb testing.TB, cfg *Config) *TestHelper {
require.NoError(th.tb, err)
th.apiURL = "http://localhost:" + port

th.adminClient, err = NewClient(ClientConfig{
th.adminClient, err = public.NewClient(public.ClientConfig{
URL: th.apiURL,
AuthKey: th.srvc.cfg.API.Security.AdminSecretKey,
})
Expand Down
2 changes: 1 addition & 1 deletion service/job_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"fmt"

"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/public/job"
)

const jobKeyPrefix = "job_"
Expand Down
2 changes: 1 addition & 1 deletion service/jobs_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/http"
"time"

"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/public/job"

"github.com/gorilla/mux"

Expand Down
2 changes: 1 addition & 1 deletion service/jobs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"fmt"
"io"

"github.com/mattermost/calls-offloader/public/job"
"github.com/mattermost/calls-offloader/service/docker"
"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/service/kubernetes"

"github.com/mattermost/mattermost/server/public/shared/mlog"
Expand Down
2 changes: 1 addition & 1 deletion service/kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"time"

"github.com/mattermost/calls-offloader/service/job"
"github.com/mattermost/calls-offloader/public/job"
"github.com/mattermost/calls-offloader/service/random"

recorder "github.com/mattermost/calls-recorder/cmd/recorder/config"
Expand Down
2 changes: 1 addition & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func New(cfg Config) (*Service, error) {
return nil, fmt.Errorf("failed to init logger: %w", err)
}

s.log.Info("starting up", getVersionInfo().logFields()...)
s.log.Info("starting up", getVersionInfo().LogFields()...)

s.store, err = store.New(cfg.Store.DataSource)
if err != nil {
Expand Down
22 changes: 4 additions & 18 deletions service/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"runtime"

"github.com/mattermost/calls-offloader/public"

"github.com/mattermost/mattermost/server/public/shared/mlog"
)

Expand All @@ -17,31 +19,15 @@ var (
buildDate string
)

type VersionInfo struct {
BuildDate string `json:"buildDate"`
BuildVersion string `json:"buildVersion"`
BuildHash string `json:"buildHash"`
GoVersion string `json:"goVersion"`
}

func getVersionInfo() VersionInfo {
return VersionInfo{
func getVersionInfo() public.VersionInfo {
return public.VersionInfo{
BuildDate: buildDate,
BuildVersion: buildVersion,
BuildHash: buildHash,
GoVersion: runtime.Version(),
}
}

func (v VersionInfo) logFields() []mlog.Field {
return []mlog.Field{
mlog.String("buildDate", v.BuildDate),
mlog.String("buildVersion", v.BuildVersion),
mlog.String("buildHash", v.BuildHash),
mlog.String("goVersion", v.GoVersion),
}
}

func (s *Service) getVersion(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.NotFound(w, req)
Expand Down
10 changes: 6 additions & 4 deletions service/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"runtime"
"testing"

"github.com/mattermost/calls-offloader/public"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -36,10 +38,10 @@ func TestGetVersion(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
var info VersionInfo
var info public.VersionInfo
err = json.NewDecoder(resp.Body).Decode(&info)
require.NoError(t, err)
require.Equal(t, VersionInfo{
require.Equal(t, public.VersionInfo{
BuildHash: buildHash,
BuildDate: buildDate,
BuildVersion: buildVersion,
Expand All @@ -53,10 +55,10 @@ func TestGetVersion(t *testing.T) {
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
var info VersionInfo
var info public.VersionInfo
err = json.NewDecoder(resp.Body).Decode(&info)
require.NoError(t, err)
require.Equal(t, VersionInfo{
require.Equal(t, public.VersionInfo{
GoVersion: goVersion,
}, info)
})
Expand Down

0 comments on commit d94cee0

Please sign in to comment.