mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-23 16:45:39 +00:00
config: reimplementing the configuration to be in YAML
cleanup: beginning to separate out into multiple files
This commit is contained in:
parent
51ebbc5c34
commit
2c78e94b2c
3 changed files with 87 additions and 36 deletions
44
config.go
Normal file
44
config.go
Normal 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
31
get.go
Normal 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
|
||||||
|
}
|
||||||
|
|
48
imgsrv.go
48
imgsrv.go
|
@ -11,7 +11,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
goconf "git.corp.redhat.com/srv/git/dev/towers/engineering/go/conf.git"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -32,7 +31,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ConfigFile = fmt.Sprintf("%s/.imgsrv.conf", os.Getenv("HOME"))
|
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
|
||||||
|
|
||||||
DefaultRunAsServer = false
|
DefaultRunAsServer = false
|
||||||
RunAsServer = DefaultRunAsServer
|
RunAsServer = DefaultRunAsServer
|
||||||
|
@ -109,29 +108,6 @@ func hasFileByKeyword(keyword string) (exists bool, err error) {
|
||||||
return exists, nil
|
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) {
|
func putFileFromPath(host, filename string) (path string, err error) {
|
||||||
ext := filepath.Ext(filename)
|
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)
|
//log.Printf("Attempting to load config file: %s", filename)
|
||||||
c, err := goconf.ReadConfigFile(filename)
|
c, err := ReadConfigFile(filename)
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
//log.Println(err)
|
//log.Println(err)
|
||||||
return goconf.NewConfigFile()
|
return Config{}
|
||||||
}
|
}
|
||||||
|
|
||||||
cRunAsServer, _ := c.GetBool("default", "server")
|
cRunAsServer := c.GetBool("server")
|
||||||
cServerIp, _ := c.GetString("default", "ip")
|
cServerIp := c.GetString("ip")
|
||||||
cServerPort, _ := c.GetString("default", "port")
|
cServerPort := c.GetString("port")
|
||||||
cMongoHost, _ := c.GetString("default", "mongohost")
|
cMongoHost := c.GetString("mongohost")
|
||||||
cMongoDB, _ := c.GetString("default", "mongodb")
|
cMongoDB := c.GetString("mongodb")
|
||||||
cMongoUsername, _ := c.GetString("default", "mongousername")
|
cMongoUsername := c.GetString("mongousername")
|
||||||
cMongoPassword, _ := c.GetString("default", "mongopassword")
|
cMongoPassword := c.GetString("mongopassword")
|
||||||
cRemoteHost, _ := c.GetString("default", "remotehost")
|
cRemoteHost := c.GetString("remotehost")
|
||||||
|
|
||||||
// Only set variables from config file,
|
// Only set variables from config file,
|
||||||
// if they weren't passed as flags
|
// if they weren't passed as flags
|
||||||
|
|
Loading…
Reference in a new issue