diff --git a/.gitignore b/.gitignore index d18c9a3..cc40fd6 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ +debug /config.* /handlers/static.go diff --git a/build/config.yaml b/build/config.yaml index d0caf89..63b35a9 100644 --- a/build/config.yaml +++ b/build/config.yaml @@ -1,11 +1,8 @@ -http: - ListenAddr: ':8080' # Consists of 'IP:Port', e.g. ':8080' listens on any IP and on Port 8080 - BaseURL: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth -General: - DataDir: ./data # Contains: the database and the private key - EnableDebugMode: true # Activates more detailed logging - ShortedIDLength: 4 # Length of the random generated ID which is used for new shortened URLs -oAuth: - Google: - ClientID: replace me # ClientID which you get from console.cloud.google.com - ClientSecret: replace me # ClientSecret which get from console.cloud.google.com +listen_addr: ':8080' # Consists of 'IP:Port', e.g. ':8080' listens on any IP and on Port 8080 +base_url: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth +data_dir: ./data # Contains: the database and the private key +enable_debug_mode: true # Activates more detailed logging +shorted_id_length: 4 # Length of the random generated ID which is used for new shortened URLs +Google: + ClientID: replace me # ClientID which you get from console.cloud.google.com + ClientSecret: replace me # ClientSecret which get from console.cloud.google.com diff --git a/handlers/auth.go b/handlers/auth.go index a26fd97..21c2239 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -17,7 +17,7 @@ import ( func (h *Handler) initOAuth() { h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey()))) - auth.WithAdapterWrapper(auth.NewGoogleAdapter(viper.GetString("oAuth.Google.ClientID"), viper.GetString("oAuth.Google.ClientSecret"), viper.GetString("http.BaseURL")), h.engine.Group("/api/v1/auth/google")) + auth.WithAdapterWrapper(auth.NewGoogleAdapter(viper.GetString("Google.ClientID"), viper.GetString("Google.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/google")) h.engine.POST("/api/v1/check", h.handleGoogleCheck) } diff --git a/handlers/auth_test.go b/handlers/auth_test.go index c9c2f69..56baac4 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -27,9 +27,9 @@ var ( ExpiresAt: time.Now().Add(time.Hour * 24 * 365).Unix(), }, "google", - "sub sub sub", + "id", "name", - "url", + "picture", } tokenString string ) diff --git a/handlers/handlers.go b/handlers/handlers.go index dabd6e4..0d826f5 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -29,7 +29,7 @@ var DoNotPrivateKeyChecking = false // New initializes the http handlers func New(store store.Store) (*Handler, error) { - if !viper.GetBool("General.EnableDebugMode") { + if !viper.GetBool("enable_debug_mode") { gin.SetMode(gin.ReleaseMode) } h := &Handler{ @@ -77,7 +77,7 @@ func (h *Handler) setHandlers() error { // Listen starts the http server func (h *Handler) Listen() error { - return h.engine.Run(viper.GetString("http.ListenAddr")) + return h.engine.Run(viper.GetString("listen_addr")) } // CloseStore stops the http server and the closes the db gracefully diff --git a/handlers/public.go b/handlers/public.go index 858730c..1bc381b 100644 --- a/handlers/public.go +++ b/handlers/public.go @@ -55,7 +55,7 @@ func (h *Handler) handleAccess(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } - if h.store.IncreaseVisitCounter(id); err != nil { + if err := h.store.IncreaseVisitCounter(id); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } diff --git a/handlers/test.yaml b/handlers/test.yaml index 823f57f..a8b8cdf 100644 --- a/handlers/test.yaml +++ b/handlers/test.yaml @@ -1,4 +1,3 @@ -General: - DataDir: ../data - EnableDebugMode: true - ShortedIDLength: 4 +data_dir: ../data +enable_debug_mode: true +shorted_id_length: 4 diff --git a/main.go b/main.go index c79447f..9b41318 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func initShortener() (func(), error) { if err := util.ReadInConfig(); err != nil { return nil, errors.Wrap(err, "could not reload config file") } - if viper.GetBool("General.EnableDebugMode") { + if viper.GetBool("enable_debug_mode") { logrus.SetLevel(logrus.DebugLevel) } store, err := store.New() diff --git a/store/store.go b/store/store.go index 517c2fc..5806118 100644 --- a/store/store.go +++ b/store/store.go @@ -64,7 +64,7 @@ func New() (*Store, error) { } return &Store{ db: db, - idLength: viper.GetInt("General.ShortedIDLength"), + idLength: viper.GetInt("shorted_id_length"), bucketName: bucketName, }, nil } diff --git a/store/store_test.go b/store/store_test.go index c0866fa..2417c70 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -12,8 +12,8 @@ const ( ) func TestGenerateRandomString(t *testing.T) { - viper.SetDefault("General.DataDir", "data") - viper.SetDefault("General.ShortedIDLength", 4) + viper.SetDefault("data_dir", "data") + viper.SetDefault("shorted_id_length", 4) tt := []struct { name string length int diff --git a/store/util.go b/store/util.go index bbda0a3..028f773 100644 --- a/store/util.go +++ b/store/util.go @@ -45,7 +45,7 @@ func (s *Store) createEntry(entry Entry, entryID string) (string, error) { // generateRandomString generates a random string with an predefined length func generateRandomString(length int) (string, error) { var result string - for len(result) < int(length) { + for len(result) < length { num, err := rand.Int(rand.Reader, big.NewInt(int64(127))) if err != nil { return "", err diff --git a/util/config.go b/util/config.go index 5199fe8..d17e0d9 100644 --- a/util/config.go +++ b/util/config.go @@ -5,6 +5,8 @@ import ( "path/filepath" "strings" + "github.com/sirupsen/logrus" + "github.com/pkg/errors" "github.com/spf13/viper" ) @@ -26,20 +28,24 @@ func ReadInConfig() error { } viper.AddConfigPath(".") setConfigDefaults() - if err := viper.ReadInConfig(); err != nil { - return errors.Wrap(err, "could not reload config file") + switch err := viper.ReadInConfig(); err.(type) { + case viper.ConfigFileNotFoundError: + logrus.Info("No configuration file found, using defaults and environment overrides.") + break + default: + return errors.Wrap(err, "could not read config file") } return checkForDatadir() } // setConfigDefaults sets the default values for the configuration func setConfigDefaults() { - viper.SetDefault("http.ListenAddr", ":8080") - viper.SetDefault("http.BaseURL", "http://localhost:3000") + viper.SetDefault("listen_addr", ":8080") + viper.SetDefault("base_url", "http://localhost:3000") - viper.SetDefault("General.DataDir", "data") - viper.SetDefault("General.EnableDebugMode", true) - viper.SetDefault("General.ShortedIDLength", 4) + viper.SetDefault("data_dir", "data") + viper.SetDefault("enable_debug_mode", true) + viper.SetDefault("shorted_id_length", 4) } // GetDataDir returns the absolute path of the data directory @@ -50,7 +56,7 @@ func GetDataDir() string { // checkForDatadir checks for the data dir and creates it if it not exists func checkForDatadir() error { var err error - dataDirPath, err = filepath.Abs(viper.GetString("General.DataDir")) + dataDirPath, err = filepath.Abs(viper.GetString("data_dir")) if err != nil { return errors.Wrap(err, "could not get relative data dir path") } diff --git a/util/private.go b/util/private.go index 1f7d4ef..81b049d 100644 --- a/util/private.go +++ b/util/private.go @@ -19,7 +19,7 @@ func CheckForPrivateKey() error { privateKey = privateDatContent } else if os.IsNotExist(err) { randomGeneratedKey := make([]byte, 256) - if _, err := rand.Read(randomGeneratedKey); err != nil { + if _, err = rand.Read(randomGeneratedKey); err != nil { return errors.Wrap(err, "could not read random bytes") } if err = ioutil.WriteFile(privateDatPath, randomGeneratedKey, 0644); err != nil {