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") return nil, errors.Wrap(err, "could not get config")
} }
conf := config.Get() conf := config.Get()
if conf.Handlers.EnableDebugMode {
log.SetLevel(logrus.DebugLevel)
}
store, err := store.New(conf.Store, log) store, err := store.New(conf.Store, log)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not create store") 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 = { state = {
links: [] links: []
} }
componentDidMount() {
this.urlInput.focus()
}
handleURLSubmit = () => { handleURLSubmit = () => {
fetch('/api/v1/protected/create', { fetch('/api/v1/protected/create', {
method: 'POST', method: 'POST',
@ -35,7 +38,7 @@ export default class HomeComponent extends Component {
<Header size='huge'>Simplify your links</Header> <Header size='huge'>Simplify your links</Header>
<Form onSubmit={this.handleURLSubmit} autoComplete="off"> <Form onSubmit={this.handleURLSubmit} autoComplete="off">
<Form.Field> <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.Field>
</Form> </Form>
</Segment > </Segment >
@ -55,14 +58,14 @@ export default class HomeComponent extends Component {
</Card.Content> </Card.Content>
<Card.Content extra> <Card.Content extra>
<div className='ui two buttons'> <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.Header className="ui center aligned">{link[0]}</Modal.Header>
<Modal.Content style={{ textAlign: "center" }}> <Modal.Content style={{ textAlign: "center" }}>
<QRCode style={{ width: "75%" }} value={link[0]} /> <QRCode style={{ width: "75%" }} value={link[0]} />
</Modal.Content> </Modal.Content>
</Modal> </Modal>
<Clipboard component="button" className="ui button" data-clipboard-text={link[0]} button-title="Copy the Shortened URL to Clipboard"> <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 Copy to Clipboard
</Clipboard> </Clipboard>
</div> </div>

29
store/util.go

@ -1,9 +1,11 @@
package store package store
import ( import (
"crypto/rand"
"encoding/json" "encoding/json"
"math/rand" "math/big"
"time" "time"
"unicode"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -40,7 +42,10 @@ func (s *Store) createEntryRaw(key, value []byte) error {
// createEntry creates a new entry // createEntry creates a new entry
func (s *Store) createEntry(URL, remoteAddr string) (string, error) { 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) exists := s.checkExistence(id)
if !exists { if !exists {
raw, err := json.Marshal(Entry{ 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 // generateRandomString generates a random string with an predefined length
func generateRandomString(length uint) string { func generateRandomString(length uint) (string, error) {
letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") var result string
b := make([]rune, length) for len(result) < int(length) {
for i := range b { num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
b[i] = letterRunes[rand.Intn(len(letterRunes))] 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