diff --git a/README.md b/README.md index b695a63..19b4347 100644 --- a/README.md +++ b/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 -- Update http stuff to integration +- Update http stuff to the gin framework - Authorization - Deletion - Test docker-compose installation \ No newline at end of file diff --git a/handlers/handlers.go b/handlers/handlers.go index 687c9a4..58314c1 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -39,7 +39,6 @@ func (h *Handler) setHandlers() { h.engine.POST("/api/v1/info", h.handleInfo) h.engine.StaticFile("/", "static/index.html") h.engine.GET("/:id", h.handleAccess) - gin.SetMode(gin.ReleaseMode) } // 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 -func (h *Handler) Stop() error { - // err := h.store.Close() - // if err != nil { - // return err - // } +func (h *Handler) CloseStore() error { return h.store.Close() - // return h.server.Shutdown(context.Background()) } diff --git a/main.go b/main.go index 9cb4bf0..c5a94b8 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "log" "os" "os/signal" - "strconv" "github.com/maxibanki/golang-url-shortener/handlers" "github.com/maxibanki/golang-url-shortener/store" @@ -13,7 +12,7 @@ import ( func main() { dbPath := "main.db" listenAddr := ":8080" - idLength := uint(4) + idLength := 4 if os.Getenv("SHORTENER_DB_PATH") != "" { dbPath = os.Getenv("SHORTENER_DB_PATH") } @@ -21,11 +20,7 @@ func main() { listenAddr = os.Getenv("SHORTENER_LISTEN_ADDR") } if os.Getenv("SHORTENER_ID_LENGTH") != "" { - length, err := strconv.ParseUint(os.Getenv("SHORTENER_ID_LENGTH"), 10, 32) - if err != nil { - log.Fatalf("could not convert ID length into an uint: %v", err) - } - idLength = uint(length) + idLength = int(os.Getenv("SHORTENER_ID_LENGTH")) } stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt) @@ -42,7 +37,7 @@ func main() { }() <-stop log.Println("Shutting down...") - err = handler.Stop() + err = handler.CloseStore() if err != nil { log.Printf("failed to stop the handlers: %v", err) } diff --git a/store/store.go b/store/store.go index 54a159a..df42bcf 100644 --- a/store/store.go +++ b/store/store.go @@ -15,7 +15,7 @@ import ( type Store struct { db *bolt.DB bucketName []byte - idLength uint + idLength int } // 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") // 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}) if err != nil { 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 -func generateRandomString(length uint) string { +func generateRandomString(length int) string { letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") b := make([]rune, length) for i := range b {