Browse Source

changed the IDLength to int instead of uint

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
b445d33358
  1. 2
      README.md
  2. 8
      handlers/handlers.go
  3. 11
      main.go
  4. 6
      store/store.go

2
README.md

@ -119,7 +119,7 @@ To use this, POST a JSON with the field `id` to the endpoint. It will return a J
Next changes sorted by priority Next changes sorted by priority
- Update http stuff to integration - Update http stuff to the gin framework
- Authorization - Authorization
- Deletion - Deletion
- Test docker-compose installation - Test docker-compose installation

8
handlers/handlers.go

@ -39,7 +39,6 @@ func (h *Handler) setHandlers() {
h.engine.POST("/api/v1/info", h.handleInfo) h.engine.POST("/api/v1/info", h.handleInfo)
h.engine.StaticFile("/", "static/index.html") h.engine.StaticFile("/", "static/index.html")
h.engine.GET("/:id", h.handleAccess) h.engine.GET("/:id", h.handleAccess)
gin.SetMode(gin.ReleaseMode)
} }
// handleCreate handles requests to create an entry // handleCreate handles requests to create an entry
@ -112,11 +111,6 @@ func (h *Handler) Listen() error {
} }
// Stop stops the http server and the closes the db gracefully // Stop stops the http server and the closes the db gracefully
func (h *Handler) Stop() error { func (h *Handler) CloseStore() error {
// err := h.store.Close()
// if err != nil {
// return err
// }
return h.store.Close() return h.store.Close()
// return h.server.Shutdown(context.Background())
} }

11
main.go

@ -4,7 +4,6 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv"
"github.com/maxibanki/golang-url-shortener/handlers" "github.com/maxibanki/golang-url-shortener/handlers"
"github.com/maxibanki/golang-url-shortener/store" "github.com/maxibanki/golang-url-shortener/store"
@ -13,7 +12,7 @@ import (
func main() { func main() {
dbPath := "main.db" dbPath := "main.db"
listenAddr := ":8080" listenAddr := ":8080"
idLength := uint(4) idLength := 4
if os.Getenv("SHORTENER_DB_PATH") != "" { if os.Getenv("SHORTENER_DB_PATH") != "" {
dbPath = os.Getenv("SHORTENER_DB_PATH") dbPath = os.Getenv("SHORTENER_DB_PATH")
} }
@ -21,11 +20,7 @@ func main() {
listenAddr = os.Getenv("SHORTENER_LISTEN_ADDR") listenAddr = os.Getenv("SHORTENER_LISTEN_ADDR")
} }
if os.Getenv("SHORTENER_ID_LENGTH") != "" { if os.Getenv("SHORTENER_ID_LENGTH") != "" {
length, err := strconv.ParseUint(os.Getenv("SHORTENER_ID_LENGTH"), 10, 32) idLength = int(os.Getenv("SHORTENER_ID_LENGTH"))
if err != nil {
log.Fatalf("could not convert ID length into an uint: %v", err)
}
idLength = uint(length)
} }
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt) signal.Notify(stop, os.Interrupt)
@ -42,7 +37,7 @@ func main() {
}() }()
<-stop <-stop
log.Println("Shutting down...") log.Println("Shutting down...")
err = handler.Stop() err = handler.CloseStore()
if err != nil { if err != nil {
log.Printf("failed to stop the handlers: %v", err) log.Printf("failed to stop the handlers: %v", err)
} }

6
store/store.go

@ -15,7 +15,7 @@ import (
type Store struct { type Store struct {
db *bolt.DB db *bolt.DB
bucketName []byte bucketName []byte
idLength uint idLength int
} }
// Entry is the data set which is stored in the DB as JSON // Entry is the data set which is stored in the DB as JSON
@ -40,7 +40,7 @@ var ErrGeneratingTriesFailed = errors.New("could not generate unique id, db full
var ErrIDIsEmpty = errors.New("id is empty") var ErrIDIsEmpty = errors.New("id is empty")
// New initializes the store with the db // New initializes the store with the db
func New(dbName string, idLength uint) (*Store, error) { func New(dbName string, idLength int) (*Store, error) {
db, err := bolt.Open(dbName, 0644, &bolt.Options{Timeout: 1 * time.Second}) db, err := bolt.Open(dbName, 0644, &bolt.Options{Timeout: 1 * time.Second})
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not open bolt DB database") return nil, errors.Wrap(err, "could not open bolt DB database")
@ -181,7 +181,7 @@ func (s *Store) Close() 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 int) string {
letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length) b := make([]rune, length)
for i := range b { for i := range b {

Loading…
Cancel
Save