12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /*
- The "strategy" package in Kurz provides the aliasing strategies.
- Files
- - strategy.go contains the interface and base implementation
- - strategies.go contains the strategy instances and utilities
- - manual.go contains the "manual" strategy
- - hexrcr32.go contains the "hexcrc32" strategy
- */
- package strategy
- import (
- "github.com/FGM/kurz/storage"
- "github.com/FGM/kurz/url"
- "log"
- "time"
- )
- /*
- AliasingStrategy defines the operations provided by the various aliasing implementations:
- The options parameter for Alias() MAY be used by some strategies, in which case they
- have to define their expectations about it.
- */
- type AliasingStrategy interface {
- Name() string // Return the name of the strategy object
- Alias(url url.LongUrl, s storage.Storage, options ...interface{}) (url.ShortUrl, error) // Return the short URL (alias) for a given long (source) URL
- UseCount(storage storage.Storage) int // Return the number of short URLs (aliases) using this strategy.
- }
- type baseStrategy struct{}
- func (y baseStrategy) Name() string {
- return "base"
- }
- func (y baseStrategy) Alias(long url.LongUrl, s storage.Storage, options ...interface{}) (url.ShortUrl, error) {
- var short url.ShortUrl
- var err error
- /** TODO
- * - validate alias is available
- */
- short = url.ShortUrl{
- Value: long.Value,
- ShortFor: long,
- Domain: long.Domain(),
- Strategy: y.Name(),
- SubmittedBy: storage.CurrentUser(),
- SubmittedOn: time.Now().UTC().Unix(),
- IsEnabled: true,
- }
- sql := `
- INSERT INTO shorturl(url, domain, strategy, submittedBy, submittedInfo, isEnabled)
- VALUES (?, ?, ?, ?, ?, ?)
- `
- result, err := s.DB.Exec(sql, short.Value, short.Domain, short.Strategy, short.SubmittedBy.Id, short.SubmittedOn, short.IsEnabled)
- if err != nil {
- log.Printf("Failed inserting %s: %#V", short.Value, err)
- } else {
- short.Id, _ = result.LastInsertId()
- }
- return short, err
- }
- /**
- Any nonzero result is likely an error.
- */
- func (y baseStrategy) UseCount(s storage.Storage) int {
- sql := `
- SELECT COUNT(*)
- FROM shorturl
- WHERE strategy = ?
- `
- var count int
- err := s.DB.QueryRow(sql, y.Name()).Scan(&count)
- if err != nil {
- count = 0
- log.Printf("Failed querying database for base strategy use count: %v\n", err)
- }
- return count
- }
|