1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-12-25 15:26:30 +00:00

major cleanup of config handling

This commit is contained in:
Vincent Batts 2013-08-05 23:06:05 -04:00
parent 25f42cf2be
commit cacdb3aaa5
3 changed files with 102 additions and 123 deletions

View file

@ -5,13 +5,23 @@ import (
"launchpad.net/goyaml"
)
type Config map[string]interface{}
type Config struct {
Server bool // Run as server, if different than false (server)
Ip string // Bind address, if different than 0.0.0.0 (server)
Port string // listen port, if different than '7777' (server)
MongoHost string // mongoDB host, if different than 'localhost' (server)
MongoDB string // mongoDB db name, if different than 'filesrv' (server)
MongoUsername string // mongoDB username, if any (server)
MongoPassword string // mongoDB password, if any (server)
RemoteHost string // imgsrv server to push files to (client)
Map map[string]interface{} // key/value options (not used currently)
}
// Of the configurations, provided option, return the value as a bool
func (c *Config) GetBool(option string) (value bool) {
conf := Config{}
conf = *c
switch conf[option] {
func (c Config) GetBool(option string) (value bool) {
switch c.Map[option] {
default:
value = false
case "yes", "on", "true":
@ -21,26 +31,31 @@ func (c *Config) GetBool(option string) (value bool) {
}
// Of the configurations, provided option, return the value as a string
func (c *Config) GetString(option string) (value string) {
conf := Config{}
conf = *c
value, _ = conf[option].(string)
func (c Config) GetString(option string) (value string) {
value, _ = c.Map[option].(string)
return
}
func (c *Config) Merge(other *Config) error {
if other == nil {
return nil
}
return nil
}
// Given a filename to a YAML file, unmarshal it, and return a Config
func ReadConfigFile(filename string) (config Config, err error) {
func ReadConfigFile(filename string) (*Config, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return
return nil, err
}
err = goyaml.Unmarshal(bytes, &config)
if err != nil {
return
config := Config{}
if err = goyaml.Unmarshal(bytes, &config); err != nil {
return nil, err
}
return config, nil
return &config, nil
}
/*

152
imgsrv.go
View file

@ -10,69 +10,68 @@ package main
import (
"flag"
"fmt"
"github.com/vbatts/imgsrv/client"
"github.com/vbatts/imgsrv/config"
"github.com/vbatts/imgsrv/util"
"labix.org/v2/mgo"
"log"
"net/url"
"os"
"path/filepath"
"github.com/vbatts/imgsrv/client"
"github.com/vbatts/imgsrv/config"
"github.com/vbatts/imgsrv/util"
)
var (
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
DefaultRunAsServer = false
RunAsServer = DefaultRunAsServer
DefaultConfig = &config.Config{
Server: false,
Ip: "0.0.0.0",
Port: "7777",
MongoHost: "localhost",
MongoDB: "filesrv",
MongoUsername: "",
MongoPassword: "",
RemoteHost: "",
}
DefaultServerIP = "0.0.0.0"
ServerIP = DefaultServerIP
DefaultServerPort = "7777"
ServerPort = DefaultServerPort
DefaultMongoHost = "localhost"
MongoHost = DefaultMongoHost
DefaultMongoDB = "filesrv"
MongoDB = DefaultMongoDB
DefaultMongoUsername = ""
MongoUsername = DefaultMongoUsername
DefaultMongoPassword = ""
MongoPassword = DefaultMongoPassword
mongo_session *mgo.Session
images_db *mgo.Database
gfs *mgo.GridFS
DefaultRemoteHost = ""
RemoteHost = DefaultRemoteHost
PutFile = ""
FetchUrl = ""
FileKeywords = ""
PutFile = ""
FetchUrl = ""
FileKeywords = ""
)
func main() {
flag.Parse()
for _, arg := range flag.Args() {
// What to do with these floating args ...
// TODO What to do with these floating args ...
// Assume they're files and upload them?
log.Printf("%s", arg)
}
// loads either default or flag specified config
// to override variables
loadConfiguration(ConfigFile)
if c, err := config.ReadConfigFile(ConfigFile); err == nil {
DefaultConfig.Merge(c)
}
if DefaultConfig.Server {
// Run the server!
runServer(DefaultConfig)
} else if len(FetchUrl) > 0 {
// not sure that this ought to be exposed in the client tool
if len(FetchUrl) > 0 {
file, err := util.FetchFileFromURL(FetchUrl)
if err != nil {
log.Println(err)
return
}
log.Println(file)
} else if RunAsServer {
log.Printf("%s", ServerIP)
runServer(ServerIP, ServerPort)
} else {
if len(RemoteHost) == 0 {
// we're pushing up a file
if len(DefaultConfig.RemoteHost) == 0 {
log.Println("Please provide a remotehost!")
return
}
@ -87,7 +86,7 @@ func main() {
} else {
log.Println("WARN: you didn't provide any keywords :-(")
}
url, err := url.Parse(RemoteHost + "/v/" + queryParams)
url, err := url.Parse(DefaultConfig.RemoteHost + "/v/" + queryParams)
if err != nil {
log.Println(err)
return
@ -98,7 +97,7 @@ func main() {
log.Println(err)
return
}
log.Printf("New Image!: %s%s\n", RemoteHost, url_path)
log.Printf("New Image!: %s%s\n", DefaultConfig.RemoteHost, url_path)
}
}
@ -115,35 +114,35 @@ func init() {
"Provide alternate configuration file")
/* Server-side */
flag.BoolVar(&RunAsServer,
flag.BoolVar(&DefaultConfig.Server,
"server",
RunAsServer,
DefaultConfig.Server,
"Run as an image server (defaults to client-side)")
flag.StringVar(&ServerIP,
flag.StringVar(&DefaultConfig.Ip,
"ip",
ServerIP,
DefaultConfig.Ip,
"IP to bind to (if running as a server)('ip' in the config)")
flag.StringVar(&ServerPort,
flag.StringVar(&DefaultConfig.Port,
"port",
ServerPort,
DefaultConfig.Port,
"Port to listen on (if running as a server)('port' in the config)")
/* MongoDB settings */
flag.StringVar(&MongoHost,
flag.StringVar(&DefaultConfig.MongoHost,
"mongo-host",
MongoHost,
DefaultConfig.MongoHost,
"Mongo Host to connect to ('mongohost' in the config)")
flag.StringVar(&MongoDB,
flag.StringVar(&DefaultConfig.MongoDB,
"mongo-db",
MongoDB,
DefaultConfig.MongoDB,
"Mongo db to connect to ('mongodb' in the config)")
flag.StringVar(&MongoUsername,
flag.StringVar(&DefaultConfig.MongoUsername,
"mongo-username",
MongoUsername,
DefaultConfig.MongoUsername,
"Mongo username to auth with (if needed) ('mongousername' in the config)")
flag.StringVar(&MongoPassword,
flag.StringVar(&DefaultConfig.MongoPassword,
"mongo-password",
MongoPassword,
DefaultConfig.MongoPassword,
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
/* Client-side */
@ -152,9 +151,9 @@ func init() {
FetchUrl,
"Just fetch the file from this url")
flag.StringVar(&RemoteHost,
flag.StringVar(&DefaultConfig.RemoteHost,
"remotehost",
RemoteHost,
DefaultConfig.RemoteHost,
"Remote host to get/put files on ('remotehost' in the config)")
flag.StringVar(&PutFile,
"put",
@ -166,50 +165,3 @@ func init() {
"Keywords to associate with file. (comma delimited) (needs -put)")
}
func loadConfiguration(filename string) (c config.Config) {
//log.Printf("Attempting to load config file: %s", filename)
c, err := config.ReadConfigFile(filename)
if err != nil {
//log.Println(err)
return config.Config{}
}
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
if DefaultRunAsServer == RunAsServer && cRunAsServer {
RunAsServer = cRunAsServer
}
if DefaultServerIP == ServerIP && len(cServerIp) > 0 {
ServerIP = cServerIp
}
if DefaultServerPort == ServerPort && len(cServerPort) > 0 {
ServerPort = cServerPort
}
if DefaultMongoHost == MongoHost && len(cMongoHost) > 0 {
MongoHost = cMongoHost
}
if DefaultMongoDB == MongoDB && len(cMongoDB) > 0 {
MongoDB = cMongoDB
}
if DefaultMongoUsername == MongoUsername && len(cMongoUsername) > 0 {
MongoUsername = cMongoUsername
}
if DefaultMongoPassword == MongoPassword && len(cMongoPassword) > 0 {
MongoPassword = cMongoPassword
}
if DefaultRemoteHost == RemoteHost && len(cRemoteHost) > 0 {
RemoteHost = cRemoteHost
}
return c
}

View file

@ -16,15 +16,26 @@ import (
"github.com/vbatts/imgsrv/hash"
"github.com/vbatts/imgsrv/types"
"github.com/vbatts/imgsrv/util"
"github.com/vbatts/imgsrv/config"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
var defaultPageLimit int = 25
var (
defaultPageLimit int = 25
serverConfig config.Config
/* Run as the image server */
func runServer(ip, port string) {
var addr = fmt.Sprintf("%s:%s", ip, port)
mongo_session *mgo.Session // FIXME make this not global
images_db *mgo.Database // FIXME make this not global
gfs *mgo.GridFS // FIXME make this not global
)
/*
Run as the file/image server
*/
func runServer(c *config.Config) {
serverConfig = *c
initMongo()
defer mongo_session.Close()
@ -44,18 +55,19 @@ func runServer(ip, port string) {
http.HandleFunc("/ext/", routeExt)
http.HandleFunc("/md5/", routeMD5s)
addr := fmt.Sprintf("%s:%s", c.Ip, c.Port)
log.Printf("Serving on %s ...", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
func initMongo() {
mongo_session, err := mgo.Dial(MongoHost)
mongo_session, err := mgo.Dial(serverConfig.MongoHost)
if err != nil {
log.Panic(err)
}
images_db = mongo_session.DB(MongoDB)
if len(MongoUsername) > 0 && len(MongoPassword) > 0 {
err = images_db.Login(MongoUsername, MongoPassword)
images_db = mongo_session.DB(serverConfig.MongoDB)
if len(serverConfig.MongoUsername) > 0 && len(serverConfig.MongoPassword) > 0 {
err = images_db.Login(serverConfig.MongoUsername, serverConfig.MongoPassword)
if err != nil {
log.Panic(err)
}