diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bce70fb..ebbb5b2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ on: - synchronize env: - GO_VERSION: 1.19 + GO_VERSION: 1.18 jobs: unit-tests: diff --git a/README.md b/README.md index 4389d3b..b7ffd84 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,9 @@ A URL shortener library implemented in Go. go get github.com/TomerHeber/go-short-url ``` -## MongoDB - -To store the mappings between short urls and their original urls, A MongoDB database is required. - -## Development - -Install `golangci-lint`: -Check [https://golangci-lint.run/usage/install/#local-installation](https://golangci-lint.run/usage/install/#local-installation) for installation instructions. +## Documentation -Install `pre-commit`: -Check [https://pre-commit.com/](https://pre-commit.com/) for installation instructions. - -Enable the git pre commit hooks: -`pre-commit install ` +Documentation is available at: [https://pkg.go.dev/github.com/TomerHeber/go-short-url](https://pkg.go.dev/github.com/TomerHeber/go-short-url) ## Example @@ -47,4 +36,19 @@ It's a tiny webserver that generates short urls. ``` docker-compose up -d go run ./example/main.go -``` \ No newline at end of file +``` + +## MongoDB + +To store the mappings between short urls and their original urls, A MongoDB database is required. + +## Development + +Install `golangci-lint`: +Check [https://golangci-lint.run/usage/install/#local-installation](https://golangci-lint.run/usage/install/#local-installation) for installation instructions. + +Install `pre-commit`: +Check [https://pre-commit.com/](https://pre-commit.com/) for installation instructions. + +Enable the git pre commit hooks: +`pre-commit install ` \ No newline at end of file diff --git a/config.go b/config.go index 27bc4a2..9d2e153 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,8 @@ import ( "go.mongodb.org/mongo-driver/x/mongo/driver/connstring" ) +// Config is used to customize the shortener. +// To create a config instance use `DefaultConfig()`. type Config interface { getConfig() *config diff --git a/short.go b/short.go index 8acabf3..4b9f771 100644 --- a/short.go +++ b/short.go @@ -60,7 +60,7 @@ func NewShortener(config ...Config) (Shortener, error) { var s shortner s.host = ci.host - s.store, err = NewStore(ci.mongoUri, ci.host) + s.store, err = newStore(ci.mongoUri, ci.host) if err != nil { return nil, err } diff --git a/store.go b/store.go index cfb7ec3..9e83ae9 100644 --- a/store.go +++ b/store.go @@ -111,7 +111,7 @@ func getMongoCollection(ctx context.Context, database *mongo.Database, name stri return collection, nil } -func NewStore(mongoUri string, name string) (Store, error) { +func newStore(mongoUri string, name string) (Store, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() diff --git a/store_test.go b/store_test.go index 4590b7f..d1434b0 100644 --- a/store_test.go +++ b/store_test.go @@ -40,11 +40,11 @@ func TestStore(t *testing.T) { t.Run("NewStore", func(t *testing.T) { uri := getRandomMongoURIForTesting() - _, err := NewStore(uri, "short.link") + _, err := newStore(uri, "short.link") require.Nil(t, err) - _, err = NewStore(uri, "short.com") + _, err = newStore(uri, "short.com") require.Nil(t, err) - _, err = NewStore(uri, "short.link") + _, err = newStore(uri, "short.link") require.Nil(t, err) client := mongoDbClientMap[uri] @@ -64,7 +64,7 @@ func TestStore(t *testing.T) { getStoreHelper := func(t *testing.T) Store { t.Helper() uri := getRandomMongoURIForTesting() - s, err := NewStore(uri, collectionName) + s, err := newStore(uri, collectionName) require.Nil(t, err) return s } diff --git a/url_config.go b/url_config.go index 11c0fae..7c12169 100644 --- a/url_config.go +++ b/url_config.go @@ -5,6 +5,8 @@ import ( "time" ) +// UrlConfig may be used to customize the way a url is shortened. +// A UrlConfig instance can be created by calling `DefaultUrlConfig()`. type UrlConfig interface { getConfig() *urlConfig