diff --git a/main.go b/main.go index 062b31d..9f88473 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,9 @@ func initShortener(log *logrus.Logger) (func(), error) { return nil, errors.Wrap(err, "could not get config") } conf := config.Get() + if conf.Handlers.EnableDebugMode { + log.SetLevel(logrus.DebugLevel) + } store, err := store.New(conf.Store, log) if err != nil { return nil, errors.Wrap(err, "could not create store") diff --git a/static/src/Home/Home.js b/static/src/Home/Home.js index 85bfed3..4c05d28 100644 --- a/static/src/Home/Home.js +++ b/static/src/Home/Home.js @@ -8,6 +8,9 @@ export default class HomeComponent extends Component { state = { links: [] } + componentDidMount() { + this.urlInput.focus() + } handleURLSubmit = () => { fetch('/api/v1/protected/create', { method: 'POST', @@ -35,7 +38,7 @@ export default class HomeComponent extends Component {
Simplify your links
- + this.urlInput = input} action={{ icon: 'arrow right', labelPosition: 'right', content: 'Shorten' }} type='url' onChange={this.handleURLChange} name='url' placeholder='Paste a link to shorten it' />
@@ -55,14 +58,14 @@ export default class HomeComponent extends Component {
- }> + }> {link[0]} - + Copy to Clipboard
diff --git a/store/util.go b/store/util.go index 8e00af8..5b23c65 100644 --- a/store/util.go +++ b/store/util.go @@ -1,9 +1,11 @@ package store import ( + "crypto/rand" "encoding/json" - "math/rand" + "math/big" "time" + "unicode" "github.com/boltdb/bolt" "github.com/pkg/errors" @@ -40,7 +42,10 @@ func (s *Store) createEntryRaw(key, value []byte) error { // createEntry creates a new entry func (s *Store) createEntry(URL, remoteAddr string) (string, error) { - id := generateRandomString(s.idLength) + id, err := generateRandomString(s.idLength) + if err != nil { + return "", errors.Wrap(err, "could not generate random string") + } exists := s.checkExistence(id) if !exists { raw, err := json.Marshal(Entry{ @@ -57,11 +62,19 @@ func (s *Store) createEntry(URL, remoteAddr string) (string, error) { } // generateRandomString generates a random string with an predefined length -func generateRandomString(length uint) string { - letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - b := make([]rune, length) - for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] +func generateRandomString(length uint) (string, error) { + var result string + for len(result) < int(length) { + num, err := rand.Int(rand.Reader, big.NewInt(int64(127))) + if err != nil { + return "", err + } + n := num.Int64() + // Make sure that the number/byte/letter is inside + // the range of printable ASCII characters (excluding space and DEL) + if unicode.IsLetter(rune(n)) { + result += string(n) + } } - return string(b) + return result, nil }