1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-27 10:35:41 +00:00

config: reimplementing the configuration to be in YAML

cleanup: beginning to separate out into multiple files
This commit is contained in:
Vincent Batts 2013-02-08 23:29:43 -05:00
parent 51ebbc5c34
commit 2c78e94b2c
3 changed files with 87 additions and 36 deletions

44
config.go Normal file
View file

@ -0,0 +1,44 @@
package main
import (
"launchpad.net/goyaml"
"io/ioutil"
)
type Config map[string]interface{}
func (c *Config) GetBool(option string) (value bool) {
conf := Config{}
conf = *c
switch conf[option] {
default: value = false
case "yes", "on", "true": value = true
}
return
}
func (c *Config) GetString(option string) (value string) {
conf := Config{}
conf = *c
value, _ = conf[option].(string)
return
}
func ReadConfigFile(filename string) (config Config, err error) {
bytes, err := ioutil.ReadFile(filename)
if (err != nil) {
return
}
err = goyaml.Unmarshal(bytes, &config)
if (err != nil) {
return
}
return config, nil
}
func WriteConfigFile(filename string, data []byte) (err error) {
return
}

31
get.go Normal file
View file

@ -0,0 +1,31 @@
package main
import (
"labix.org/v2/mgo/bson"
)
/* gfs is a *mgo.GridFS defined in imgsrv.go */
func GetFileByFilename(filename string) (this_file File, err error) {
err = gfs.Find(bson.M{"filename":filename}).One(&this_file)
if (err != nil) {
return this_file, err
}
return this_file, nil
}
func GetFileRandom() (this_file File, err error) {
r := rand64()
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
if (err != nil) {
return this_file, err
}
if (len(this_file.Md5) == 0) {
err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file)
}
if (err != nil) {
return this_file, err
}
return this_file, nil
}

View file

@ -11,7 +11,6 @@ import (
"log"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
goconf "git.corp.redhat.com/srv/git/dev/towers/engineering/go/conf.git"
"net/http"
"flag"
"fmt"
@ -32,7 +31,7 @@ import (
)
var (
ConfigFile = fmt.Sprintf("%s/.imgsrv.conf", os.Getenv("HOME"))
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
DefaultRunAsServer = false
RunAsServer = DefaultRunAsServer
@ -109,29 +108,6 @@ func hasFileByKeyword(keyword string) (exists bool, err error) {
return exists, nil
}
func getFileByFilename(filename string) (this_file File, err error) {
err = gfs.Find(bson.M{"filename":filename}).One(&this_file)
if (err != nil) {
return this_file, err
}
return this_file, nil
}
func getFileRandom() (this_file File, err error) {
r := rand64()
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
if (err != nil) {
return this_file, err
}
if (len(this_file.Md5) == 0) {
err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file)
}
if (err != nil) {
return this_file, err
}
return this_file, nil
}
func putFileFromPath(host, filename string) (path string, err error) {
ext := filepath.Ext(filename)
@ -675,22 +651,22 @@ func init() {
}
func loadConfiguration(filename string) (c *goconf.ConfigFile) {
func loadConfiguration(filename string) (c Config) {
//log.Printf("Attempting to load config file: %s", filename)
c, err := goconf.ReadConfigFile(filename)
c, err := ReadConfigFile(filename)
if (err != nil) {
//log.Println(err)
return goconf.NewConfigFile()
return Config{}
}
cRunAsServer, _ := c.GetBool("default", "server")
cServerIp, _ := c.GetString("default", "ip")
cServerPort, _ := c.GetString("default", "port")
cMongoHost, _ := c.GetString("default", "mongohost")
cMongoDB, _ := c.GetString("default", "mongodb")
cMongoUsername, _ := c.GetString("default", "mongousername")
cMongoPassword, _ := c.GetString("default", "mongopassword")
cRemoteHost, _ := c.GetString("default", "remotehost")
cRunAsServer := c.GetBool("server")
cServerIp := c.GetString("ip")
cServerPort := c.GetString("port")
cMongoHost := c.GetString("mongohost")
cMongoDB := c.GetString("mongodb")
cMongoUsername := c.GetString("mongousername")
cMongoPassword := c.GetString("mongopassword")
cRemoteHost := c.GetString("remotehost")
// Only set variables from config file,
// if they weren't passed as flags