Skip to content

Commit

Permalink
chore: add shorturl example
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Feb 19, 2024
1 parent eb9ea8f commit f3dbedb
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dominikbraun/graph v0.23.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
2 changes: 2 additions & 0 deletions examples/shorturl/kod.toml
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" }
94 changes: 94 additions & 0 deletions examples/shorturl/kod_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/shorturl/kod_gen_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions examples/shorturl/kod_gen_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions examples/shorturl/short_url.go
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(),
}
}
51 changes: 51 additions & 0 deletions examples/shorturl/short_url_test.go
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)
})
})
}

0 comments on commit f3dbedb

Please sign in to comment.