socialnetworknews-api

git clone git://archive.git.mtrnord.blog/SocialNetworkNews/socialnetworknews-api.git
Log | Files | Refs | LICENSE

commit 437a325b3df389405e7fe7b27b6f7aab6ece448a
parent 4437e1e44301ac72671762511fe2134b9b6450ed
Author: MTRNord <mtrnord1@gmail.com>
Date:   Tue, 13 Mar 2018 18:47:54 +0100

Use system config dir

Diffstat:
Mapi/webserver.go | 4+++-
Mconfig/config.go | 11++++++++++-
Mdb/db.go | 21++++++++++-----------
Mtwitter/twitter.go | 4+++-
4 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/api/webserver.go b/api/webserver.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "github.com/SocialNetworkNews/SocialNetworkNews_API/config" "github.com/SocialNetworkNews/SocialNetworkNews_API/db" "github.com/SocialNetworkNews/SocialNetworkNews_API/twitter" "github.com/dgraph-io/badger" @@ -254,8 +255,9 @@ func getTweets() ([]byte, error) { // open output file currentTime := time.Now().Local() currentTime = currentTime.AddDate(0, 0, -1) + filePath := config.ConfigPath() filename := fmt.Sprintf("tweets_%s.csv", currentTime.Format("2006_01_02")) - dataFilePath := filepath.Join(".", "data", filename) + dataFilePath := filepath.Join(filePath, "data", filename) fo, err := os.Open(dataFilePath) if err != nil { diff --git a/config/config.go b/config/config.go @@ -1,8 +1,10 @@ package config import ( + "github.com/shibukawa/configdir" "gopkg.in/yaml.v2" "io/ioutil" + "path/filepath" "sync" ) @@ -20,11 +22,18 @@ type TwitterConfig struct { Hashtags []string `yaml:"hashtags,flow"` } +func ConfigPath() string { + configDirs := configdir.New("SocialNetworksNews", "API") + filePath := filepath.ToSlash(configDirs.QueryFolders(configdir.Global)[0].Path) + return filePath +} + func GetConfig() (*Config, error) { var gFerr error configOnce.Do(func() { config := &Config{} - b, err := ioutil.ReadFile("config.yaml") // just pass the file name + filePath := ConfigPath() + b, err := ioutil.ReadFile(filepath.Join(filePath, "config.yaml")) // just pass the file name if err != nil { gFerr = err return diff --git a/db/db.go b/db/db.go @@ -2,8 +2,8 @@ package db import ( "errors" + "github.com/SocialNetworkNews/SocialNetworkNews_API/config" "github.com/dgraph-io/badger" - "github.com/shibukawa/configdir" "os" "path/filepath" "sync" @@ -15,18 +15,17 @@ var onceDB sync.Once func OpenDB() (db *badger.DB, err error) { onceDB.Do(func() { // Open the data.db file. It will be created if it doesn't exist. - configDirs := configdir.New("SocialNetworksNews", "API") - filePath := filepath.ToSlash(configDirs.QueryFolders(configdir.Global)[0].Path) + filePath := config.ConfigPath() - if _, StatErr := os.Stat(filePath + "/data/"); os.IsNotExist(StatErr) { - MkdirErr := os.MkdirAll(filePath+"/data/", 0700) + if _, StatErr := os.Stat(filepath.Join(filePath, "data")); os.IsNotExist(StatErr) { + MkdirErr := os.MkdirAll(filepath.Join(filePath, "data"), 0700) if MkdirErr != nil { err = MkdirErr return } } - if _, StatErr := os.Stat(filePath + "/data/cache/"); os.IsNotExist(StatErr) { - MkdirErr := os.MkdirAll(filePath+"/data/cache/", 0700) + if _, StatErr := os.Stat(filepath.Join(filePath, "data", "cache")); os.IsNotExist(StatErr) { + MkdirErr := os.MkdirAll(filepath.Join(filePath, "data", "cache"), 0700) if MkdirErr != nil { err = MkdirErr return @@ -34,11 +33,11 @@ func OpenDB() (db *badger.DB, err error) { } opts := badger.DefaultOptions opts.SyncWrites = false - opts.Dir = filePath + "/data/cache" - opts.ValueDir = filePath + "/data/cache" + opts.Dir = filepath.Join(filePath, "data", "cache") + opts.ValueDir = filepath.Join(filePath, "data", "cache") - if _, StatErr := os.Stat(filePath + "/data/cache/LOCK"); StatErr == nil { - DeleteErr := os.Remove(filePath + "/data/cache/LOCK") + if _, StatErr := os.Stat(filepath.Join(filePath, "data", "cache", "LOCK")); StatErr == nil { + DeleteErr := os.Remove(filepath.Join(filePath, "data", "cache", "LOCK")) if DeleteErr != nil { err = DeleteErr return diff --git a/twitter/twitter.go b/twitter/twitter.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "github.com/SocialNetworkNews/SocialNetworkNews_API/config" "github.com/SocialNetworkNews/anaconda" "github.com/dghubble/oauth1" "github.com/dghubble/oauth1/twitter" @@ -129,7 +130,8 @@ func (t *TwitterAPI) writeCSV(tweet []string) error { os.Mkdir(dataPath, os.ModePerm) } filename := fmt.Sprintf("tweets_%s.csv", currentTime.Format("2006_01_02")) - dataFilePath := filepath.Join(".", "data", filename) + filePath := config.ConfigPath() + dataFilePath := filepath.Join(filePath, "data", filename) // write the file f, err := os.OpenFile(dataFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)