Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2020-02-24 15:59:13 +0300
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2020-02-24 15:59:13 +0300
commit61dcb73441ba8e940b34c0ad18df363a5c0e768d (patch)
tree562ff103fb993d7423ebd7f17cdd297d83a1e3f0
parentbfbfb6259e7c875f053b49aaf430a1c4db7a1e9f (diff)
Move serving and lookup path factory to source package
-rw-r--r--internal/source/gitlab/factory.go47
-rw-r--r--internal/source/gitlab/factory/lookup.go22
-rw-r--r--internal/source/gitlab/factory/lookup_test.go29
-rw-r--r--internal/source/gitlab/factory/serving.go34
-rw-r--r--internal/source/gitlab/factory_test.go (renamed from internal/source/gitlab/factory/serving_test.go)28
-rw-r--r--internal/source/gitlab/gitlab.go9
6 files changed, 75 insertions, 94 deletions
diff --git a/internal/source/gitlab/factory.go b/internal/source/gitlab/factory.go
new file mode 100644
index 00000000..e14913c4
--- /dev/null
+++ b/internal/source/gitlab/factory.go
@@ -0,0 +1,47 @@
+package gitlab
+
+import (
+ "strings"
+
+ "gitlab.com/gitlab-org/gitlab-pages/internal/serving"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/serving/disk"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/serving/serverless"
+ "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
+)
+
+// fabricateLookupPath fabricates a serving LookupPath based on the API LookupPath
+// `size` argument is DEPRECATED, see
+// https://gitlab.com/gitlab-org/gitlab-pages/issues/272
+func fabricateLookupPath(size int, lookup api.LookupPath) *serving.LookupPath {
+ return &serving.LookupPath{
+ Prefix: lookup.Prefix,
+ Path: strings.TrimPrefix(lookup.Source.Path, "/"),
+ IsNamespaceProject: (lookup.Prefix == "/" && size > 1),
+ IsHTTPSOnly: lookup.HTTPSOnly,
+ HasAccessControl: lookup.AccessControl,
+ ProjectID: uint64(lookup.ProjectID),
+ }
+}
+
+// fabricateServing fabricates serving based on the GitLab API response
+func fabricateServing(lookup api.LookupPath) serving.Serving {
+ source := lookup.Source
+
+ switch source.Type {
+ case "file":
+ return disk.New()
+ case "serverless":
+ serving, err := serverless.NewFromAPISource(source.Serverless)
+ if err != nil {
+ break
+ }
+
+ return serving
+ }
+
+ return defaultServing()
+}
+
+func defaultServing() serving.Serving {
+ return disk.New()
+}
diff --git a/internal/source/gitlab/factory/lookup.go b/internal/source/gitlab/factory/lookup.go
deleted file mode 100644
index 71bb24bc..00000000
--- a/internal/source/gitlab/factory/lookup.go
+++ /dev/null
@@ -1,22 +0,0 @@
-package factory
-
-import (
- "strings"
-
- "gitlab.com/gitlab-org/gitlab-pages/internal/serving"
- "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
-)
-
-// LookupPath fabricates a serving LookupPath based on the API LookupPath
-// `size` argument is DEPRECATED, see
-// https://gitlab.com/gitlab-org/gitlab-pages/issues/272
-func LookupPath(size int, lookup api.LookupPath) *serving.LookupPath {
- return &serving.LookupPath{
- Prefix: lookup.Prefix,
- Path: strings.TrimPrefix(lookup.Source.Path, "/"),
- IsNamespaceProject: (lookup.Prefix == "/" && size > 1),
- IsHTTPSOnly: lookup.HTTPSOnly,
- HasAccessControl: lookup.AccessControl,
- ProjectID: uint64(lookup.ProjectID),
- }
-}
diff --git a/internal/source/gitlab/factory/lookup_test.go b/internal/source/gitlab/factory/lookup_test.go
deleted file mode 100644
index e9cd76f8..00000000
--- a/internal/source/gitlab/factory/lookup_test.go
+++ /dev/null
@@ -1,29 +0,0 @@
-package factory
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
-)
-
-func TestLookupPath(t *testing.T) {
- t.Run("when lookup path is not a namespace project", func(t *testing.T) {
- lookup := api.LookupPath{Prefix: "/something"}
-
- path := LookupPath(1, lookup)
-
- require.Equal(t, path.Prefix, "/something")
- require.False(t, path.IsNamespaceProject)
- })
-
- t.Run("when lookup path is a namespace project", func(t *testing.T) {
- lookup := api.LookupPath{Prefix: "/"}
-
- path := LookupPath(2, lookup)
-
- require.Equal(t, path.Prefix, "/")
- require.True(t, path.IsNamespaceProject)
- })
-}
diff --git a/internal/source/gitlab/factory/serving.go b/internal/source/gitlab/factory/serving.go
deleted file mode 100644
index 22b99bc1..00000000
--- a/internal/source/gitlab/factory/serving.go
+++ /dev/null
@@ -1,34 +0,0 @@
-package factory
-
-import (
- "gitlab.com/gitlab-org/gitlab-pages/internal/serving"
- "gitlab.com/gitlab-org/gitlab-pages/internal/serving/disk"
- "gitlab.com/gitlab-org/gitlab-pages/internal/serving/serverless"
- "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
-)
-
-// Serving fabricates serving based on the GitLab API response
-func Serving(lookup api.LookupPath) serving.Serving {
- source := lookup.Source
-
- switch source.Type {
- case "file":
- return disk.New()
- case "serverless":
- serving, err := serverless.NewFromAPISource(source.Serverless)
- if err != nil {
- break
- }
-
- return serving
- }
-
- return DefaultServing()
-}
-
-// DefaultServing returns a serving that we will use as a default one, for
-// example to show an error, if API response does not allow us to properly
-// fabricate a serving
-func DefaultServing() serving.Serving {
- return disk.New()
-}
diff --git a/internal/source/gitlab/factory/serving_test.go b/internal/source/gitlab/factory_test.go
index 6d4a3787..2f3e1994 100644
--- a/internal/source/gitlab/factory/serving_test.go
+++ b/internal/source/gitlab/factory_test.go
@@ -1,4 +1,4 @@
-package factory
+package gitlab
import (
"testing"
@@ -11,14 +11,34 @@ import (
"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
)
-func TestServing(t *testing.T) {
+func TestFabricateLookupPath(t *testing.T) {
+ t.Run("when lookup path is not a namespace project", func(t *testing.T) {
+ lookup := api.LookupPath{Prefix: "/something"}
+
+ path := fabricateLookupPath(1, lookup)
+
+ require.Equal(t, path.Prefix, "/something")
+ require.False(t, path.IsNamespaceProject)
+ })
+
+ t.Run("when lookup path is a namespace project", func(t *testing.T) {
+ lookup := api.LookupPath{Prefix: "/"}
+
+ path := fabricateLookupPath(2, lookup)
+
+ require.Equal(t, path.Prefix, "/")
+ require.True(t, path.IsNamespaceProject)
+ })
+}
+
+func TestFabricateServing(t *testing.T) {
t.Run("when lookup path requires disk serving", func(t *testing.T) {
lookup := api.LookupPath{
Prefix: "/",
Source: api.Source{Type: "file"},
}
- require.IsType(t, &disk.Disk{}, Serving(lookup))
+ require.IsType(t, &disk.Disk{}, fabricateServing(lookup))
})
t.Run("when lookup path requires serverless serving", func(t *testing.T) {
@@ -39,6 +59,6 @@ func TestServing(t *testing.T) {
},
}
- require.IsType(t, &serverless.Serverless{}, Serving(lookup))
+ require.IsType(t, &serverless.Serverless{}, fabricateServing(lookup))
})
}
diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go
index 7accac0c..6260200a 100644
--- a/internal/source/gitlab/gitlab.go
+++ b/internal/source/gitlab/gitlab.go
@@ -13,7 +13,6 @@ import (
"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/cache"
"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/client"
- "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/factory"
)
// Gitlab source represent a new domains configuration source. We fetch all the
@@ -66,7 +65,7 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) {
response := g.client.Resolve(r.Context(), host)
if response.Error != nil {
- return &serving.Request{Serving: factory.DefaultServing()}, response.Error
+ return &serving.Request{Serving: defaultServing()}, response.Error
}
urlPath := path.Clean(r.URL.Path)
@@ -83,14 +82,14 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) {
}
return &serving.Request{
- Serving: factory.Serving(lookup),
- LookupPath: factory.LookupPath(size, lookup),
+ Serving: fabricateServing(lookup),
+ LookupPath: fabricateLookupPath(size, lookup),
SubPath: subPath}, nil
}
}
// TODO improve code around default serving, when `disk` serving gets removed
// https://gitlab.com/gitlab-org/gitlab-pages/issues/353
- return &serving.Request{Serving: factory.DefaultServing()},
+ return &serving.Request{Serving: defaultServing()},
errors.New("could not match lookup path")
}