-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
313 additions
and
0 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
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
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,2 @@ | ||
["github.com/go-kod/kod/examples/shorturl/Component"] | ||
redisconfig = { addr = "localhost:6379" } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,79 @@ | ||
package shorturl | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-kod/kod" | ||
"github.com/go-kod/kod/ext/client/kredis" | ||
"github.com/go-kod/kod/interceptor/kvalidate" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type config struct { | ||
RedisConfig kredis.Config | ||
} | ||
|
||
type impl struct { | ||
kod.Implements[Component] | ||
kod.WithConfig[config] | ||
|
||
redis *kredis.Client | ||
uuid uuid.UUID | ||
} | ||
|
||
func (i *impl) Init(ctx context.Context) error { | ||
i.redis = i.Config().RedisConfig.Build() | ||
i.uuid = uuid.New() | ||
|
||
return nil | ||
} | ||
|
||
type GenerateRequest struct { | ||
URL string `validate:"required"` | ||
Duration time.Duration | ||
} | ||
|
||
type GenerateResponse struct { | ||
Short string | ||
} | ||
|
||
// Generate generates a short url. | ||
func (i *impl) Generate(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error) { | ||
|
||
key := i.uuid.String() | ||
_, err := i.redis.SetEx(ctx, key, req.URL, req.Duration).Result() | ||
if err != nil { | ||
i.L(ctx).Error("failed to set key", "key", key, "req", req, "error", err) | ||
return nil, err | ||
} | ||
return &GenerateResponse{ | ||
Short: key, | ||
}, nil | ||
} | ||
|
||
type GetRequest struct { | ||
Short string `validate:"required"` | ||
} | ||
|
||
type GetResponse struct { | ||
URL string | ||
} | ||
|
||
// Get gets the original url from short url. | ||
func (i *impl) Get(ctx context.Context, req *GetRequest) (*GetResponse, error) { | ||
result, err := i.redis.Get(ctx, req.Short).Result() | ||
if err != nil { | ||
i.L(ctx).Error("failed to get key", "req", req, "error", err) | ||
return nil, err | ||
} | ||
return &GetResponse{ | ||
URL: result, | ||
}, nil | ||
} | ||
|
||
func (i *impl) Interceptors() []kod.Interceptor { | ||
return []kod.Interceptor{ | ||
kvalidate.Interceptor(), | ||
} | ||
} |
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,51 @@ | ||
package shorturl | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-kod/kod" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShortURL(t *testing.T) { | ||
kod.RunTest(t, func(ctx context.Context, impl Component) { | ||
result, err := impl.Generate(ctx, &GenerateRequest{ | ||
URL: "https://github.com/go-kod/kod", | ||
Duration: time.Second, | ||
}) | ||
assert.Nil(t, err) | ||
assert.NotEmpty(t, result.Short) | ||
|
||
getRes, err := impl.Get(ctx, &GetRequest{ | ||
Short: result.Short, | ||
}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "https://github.com/go-kod/kod", getRes.URL) | ||
}, kod.WithConfigFile("./kod.toml")) | ||
} | ||
|
||
func TestShortURLInvalid(t *testing.T) { | ||
t.Run("Generate", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
kod.RunTest(t, func(ctx context.Context, impl Component) { | ||
result, err := impl.Generate(ctx, &GenerateRequest{ | ||
Duration: time.Second, | ||
}) | ||
assert.NotNil(t, err) | ||
assert.Nil(t, result) | ||
}) | ||
}) | ||
|
||
t.Run("Get", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
kod.RunTest(t, func(ctx context.Context, impl Component) { | ||
result, err := impl.Get(ctx, &GetRequest{}) | ||
assert.NotNil(t, err) | ||
assert.Nil(t, result) | ||
}) | ||
}) | ||
} |