mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Gauranteed no collisions from now on
This commit is contained in:
parent
67bb9e5b1a
commit
cfc2cd9a49
10
db.go
10
db.go
@ -172,9 +172,19 @@ func (p *WikiData) save(newText string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not encode WikiData: %s", err)
|
||||
}
|
||||
p.Title = strings.ToLower(p.Title)
|
||||
err = bucket.Put([]byte(p.Title), enc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could add to bucket: %s", err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
err = db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte("programdata"))
|
||||
id, _ := b.NextSequence()
|
||||
idInt := int(id)
|
||||
return b.Put(itob(idInt), []byte(p.Title))
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
|
13
main.go
13
main.go
@ -8,6 +8,7 @@ import (
|
||||
"path"
|
||||
"runtime"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@ -63,6 +64,18 @@ Options:`)
|
||||
Open(RuntimeArgs.DatabaseLocation)
|
||||
defer Close()
|
||||
|
||||
// create programdata bucket
|
||||
err := db.Update(func(tx *bolt.Tx) error {
|
||||
_, err := tx.CreateBucketIfNotExists([]byte("programdata"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("create bucket: %s", err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Default page
|
||||
aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md"))
|
||||
p := WikiData{"about", "", []string{}, []string{}}
|
||||
|
33
utils.go
33
utils.go
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math/rand"
|
||||
@ -9,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
@ -42,10 +44,23 @@ func randomAdjective() string {
|
||||
|
||||
func randomAlliterateCombo() (combo string) {
|
||||
combo = ""
|
||||
takenNames := []string{}
|
||||
err := db.View(func(tx *bolt.Tx) error {
|
||||
// Assume bucket exists and has keys
|
||||
b := tx.Bucket([]byte("programdata"))
|
||||
c := b.Cursor()
|
||||
for k, v := c.First(); k != nil; k, v = c.Next() {
|
||||
takenNames = append(takenNames, strings.ToLower(string(v)))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
animal := randomAnimal()
|
||||
adjective := randomAdjective()
|
||||
if animal[0] == adjective[0] {
|
||||
if animal[0] == adjective[0] && stringInSlice(strings.ToLower(adjective+animal), takenNames) == false {
|
||||
combo = adjective + animal
|
||||
break
|
||||
}
|
||||
@ -53,6 +68,22 @@ func randomAlliterateCombo() (combo string) {
|
||||
return
|
||||
}
|
||||
|
||||
func stringInSlice(s string, strings []string) bool {
|
||||
for _, k := range strings {
|
||||
if s == k {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// itob returns an 8-byte big endian representation of v.
|
||||
func itob(v int) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(b, uint64(v))
|
||||
return b
|
||||
}
|
||||
|
||||
func contentType(filename string) string {
|
||||
switch {
|
||||
case strings.Contains(filename, ".css"):
|
||||
|
Loading…
Reference in New Issue
Block a user