mirror of
https://github.com/kataras/iris.git
synced 2025-12-23 12:57:05 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
52
_examples/url-shortener/factory.go
Normal file
52
_examples/url-shortener/factory.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Generator the type to generate keys(short urls)
|
||||
type Generator func() string
|
||||
|
||||
// DefaultGenerator is the defautl url generator
|
||||
var DefaultGenerator = func() string {
|
||||
id, _ := uuid.NewRandom()
|
||||
return id.String()
|
||||
}
|
||||
|
||||
// Factory is responsible to generate keys(short urls)
|
||||
type Factory struct {
|
||||
store Store
|
||||
generator Generator
|
||||
}
|
||||
|
||||
// NewFactory receives a generator and a store and returns a new url Factory.
|
||||
func NewFactory(generator Generator, store Store) *Factory {
|
||||
return &Factory{
|
||||
store: store,
|
||||
generator: generator,
|
||||
}
|
||||
}
|
||||
|
||||
// Gen generates the key.
|
||||
func (f *Factory) Gen(uri string) (key string, err error) {
|
||||
// we don't return the parsed url because #hash are converted to uri-compatible
|
||||
// and we don't want to encode/decode all the time, there is no need for that,
|
||||
// we save the url as the user expects if the uri validation passed.
|
||||
_, err = url.ParseRequestURI(uri)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
key = f.generator()
|
||||
// Make sure that the key is unique
|
||||
for {
|
||||
if v := f.store.Get(key); v == "" {
|
||||
break
|
||||
}
|
||||
key = f.generator()
|
||||
}
|
||||
|
||||
return key, nil
|
||||
}
|
||||
Reference in New Issue
Block a user