Browse Source

- added focus to the input field

- improved generateRandomString func
- added LogLevel
dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
113c684ce5
  1. 3
      main.go
  2. 9
      static/src/Home/Home.js
  3. 29
      store/util.go

3
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")

9
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 {
<Header size='huge'>Simplify your links</Header>
<Form onSubmit={this.handleURLSubmit} autoComplete="off">
<Form.Field>
<Input required size='big' action={{ icon: 'arrow right', labelPosition: 'right', content: 'Shorten' }} type='url' onChange={this.handleURLChange} name='url' placeholder='Paste a link to shorten it' />
<Input required size='big' ref={input => 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' />
</Form.Field>
</Form>
</Segment >
@ -55,14 +58,14 @@ export default class HomeComponent extends Component {
</Card.Content>
<Card.Content extra>
<div className='ui two buttons'>
<Modal closeIcon trigger={<Button icon='qrcode' content='Show QR-Code'/>}>
<Modal closeIcon trigger={<Button icon='qrcode' content='Show QR-Code' />}>
<Modal.Header className="ui center aligned">{link[0]}</Modal.Header>
<Modal.Content style={{ textAlign: "center" }}>
<QRCode style={{ width: "75%" }} value={link[0]} />
</Modal.Content>
</Modal>
<Clipboard component="button" className="ui button" data-clipboard-text={link[0]} button-title="Copy the Shortened URL to Clipboard">
<i class="clipboard icon"></i>
<Icon name="clipboard" />
Copy to Clipboard
</Clipboard>
</div>

29
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
}

Loading…
Cancel
Save