14 changed files with 166 additions and 201 deletions
@ -1,17 +0,0 @@ |
|||
{ |
|||
"Store": { |
|||
"DBPath": "main.db", |
|||
"ShortedIDLength": 4 |
|||
}, |
|||
"Handlers": { |
|||
"ListenAddr": ":8080", |
|||
"BaseURL": "http://localhost:3000", |
|||
"EnableGinDebugMode": false, |
|||
"OAuth": { |
|||
"Google": { |
|||
"ClientID": "", |
|||
"ClientSecret": "" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
http: |
|||
ListenAddr: ':8080' |
|||
BaseURL: 'http://localhost:3000' |
|||
General: |
|||
DBPath: main.db |
|||
EnableDebugMode: true |
|||
ShortedIDLength: 4 |
|||
oAuth: |
|||
Google: |
|||
ClientID: replace me |
|||
ClientSecret: replace me |
|||
@ -1,92 +0,0 @@ |
|||
package config |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"io/ioutil" |
|||
"os" |
|||
"path/filepath" |
|||
|
|||
"github.com/pkg/errors" |
|||
) |
|||
|
|||
// Configuration holds all the needed parameters use
|
|||
// the URL Shortener
|
|||
type Configuration struct { |
|||
Store Store |
|||
Handlers Handlers |
|||
} |
|||
|
|||
// Store contains the needed fields for the Store package
|
|||
type Store struct { |
|||
DBPath string |
|||
ShortedIDLength uint |
|||
} |
|||
|
|||
// Handlers contains the needed fields for the Handlers package
|
|||
type Handlers struct { |
|||
ListenAddr string |
|||
BaseURL string |
|||
EnableDebugMode bool |
|||
Secret []byte |
|||
OAuth struct { |
|||
Google struct { |
|||
ClientID string |
|||
ClientSecret string |
|||
} |
|||
} |
|||
} |
|||
|
|||
var ( |
|||
config *Configuration |
|||
configPath string |
|||
) |
|||
|
|||
// Get returns the configuration from a given file
|
|||
func Get() *Configuration { |
|||
return config |
|||
} |
|||
|
|||
// Preload loads the configuration file into the memory for further usage
|
|||
func Preload() error { |
|||
var err error |
|||
configPath, err = getConfigPath() |
|||
if err != nil { |
|||
return errors.Wrap(err, "could not get configuration path") |
|||
} |
|||
if err = updateConfig(); err != nil { |
|||
return errors.Wrap(err, "could not update config") |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func updateConfig() error { |
|||
file, err := ioutil.ReadFile(configPath) |
|||
if err != nil { |
|||
return errors.Wrap(err, "could not read configuration file") |
|||
} |
|||
if err = json.Unmarshal(file, &config); err != nil { |
|||
return errors.Wrap(err, "could not unmarshal configuration file") |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func getConfigPath() (string, error) { |
|||
ex, err := os.Executable() |
|||
if err != nil { |
|||
return "", errors.Wrap(err, "could not get executable path") |
|||
} |
|||
return filepath.Join(filepath.Dir(ex), "config.json"), nil |
|||
} |
|||
|
|||
// Set replaces the current configuration with the given one
|
|||
func Set(conf *Configuration) error { |
|||
data, err := json.MarshalIndent(conf, "", " ") |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if err = ioutil.WriteFile(configPath, data, 0644); err != nil { |
|||
return err |
|||
} |
|||
config = conf |
|||
return nil |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
package util |
|||
|
|||
import ( |
|||
"os" |
|||
"path/filepath" |
|||
"strings" |
|||
|
|||
"github.com/pkg/errors" |
|||
"github.com/spf13/viper" |
|||
) |
|||
|
|||
var dataDirPath string |
|||
|
|||
func ReadInConfig() error { |
|||
viper.AutomaticEnv() |
|||
viper.SetEnvPrefix("gus") |
|||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) |
|||
viper.SetConfigName("config") |
|||
viper.AddConfigPath(".") |
|||
SetConfigDefaults() |
|||
err := viper.ReadInConfig() |
|||
if err != nil { |
|||
return errors.Wrap(err, "could not reload config file") |
|||
} |
|||
return CheckForDatadir() |
|||
} |
|||
|
|||
func SetConfigDefaults() { |
|||
viper.SetDefault("http.ListenAddr", ":8080") |
|||
viper.SetDefault("http.BaseURL", "http://localhost:3000") |
|||
|
|||
viper.SetDefault("General.DataDir", "data") |
|||
viper.SetDefault("General.EnableDebugMode", true) |
|||
viper.SetDefault("General.ShortedIDLength", 4) |
|||
} |
|||
|
|||
func GetDataDir() string { |
|||
return dataDirPath |
|||
} |
|||
|
|||
func CheckForDatadir() error { |
|||
var err error |
|||
dataDirPath, err = filepath.Abs(viper.GetString("General.DataDir")) |
|||
if err != nil { |
|||
return errors.Wrap(err, "could not get relative data dir path") |
|||
} |
|||
if _, err = os.Stat(dataDirPath); os.IsNotExist(err) { |
|||
err = os.MkdirAll(dataDirPath, 0755) |
|||
if err != nil { |
|||
return errors.Wrap(err, "could not create config directory") |
|||
} |
|||
} |
|||
return nil |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
package util |
|||
|
|||
import ( |
|||
"crypto/rand" |
|||
"io/ioutil" |
|||
"os" |
|||
"path/filepath" |
|||
|
|||
"github.com/pkg/errors" |
|||
) |
|||
|
|||
var privateKey []byte |
|||
|
|||
func CheckForPrivateKey() error { |
|||
privateDat := filepath.Join(GetDataDir(), "private.dat") |
|||
d, err := ioutil.ReadFile(privateDat) |
|||
if err == nil { |
|||
privateKey = d |
|||
} else if os.IsNotExist(err) { |
|||
b := make([]byte, 256) |
|||
if _, err := rand.Read(b); err != nil { |
|||
return errors.Wrap(err, "could not read random bytes") |
|||
} |
|||
if err = ioutil.WriteFile(privateDat, b, 0644); err != nil { |
|||
return errors.Wrap(err, "could not write private key") |
|||
} |
|||
privateKey = b |
|||
} else if err != nil { |
|||
return errors.Wrap(err, "could not read private key") |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func GetPrivateKey() []byte { |
|||
return privateKey |
|||
} |
|||
Loading…
Reference in new issue