mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-26 15:56:31 +00:00
major cleanup of config handling
This commit is contained in:
parent
25f42cf2be
commit
cacdb3aaa5
3 changed files with 102 additions and 123 deletions
|
@ -5,13 +5,23 @@ import (
|
||||||
"launchpad.net/goyaml"
|
"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
|
// Of the configurations, provided option, return the value as a bool
|
||||||
func (c *Config) GetBool(option string) (value bool) {
|
func (c Config) GetBool(option string) (value bool) {
|
||||||
conf := Config{}
|
switch c.Map[option] {
|
||||||
conf = *c
|
|
||||||
switch conf[option] {
|
|
||||||
default:
|
default:
|
||||||
value = false
|
value = false
|
||||||
case "yes", "on", "true":
|
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
|
// Of the configurations, provided option, return the value as a string
|
||||||
func (c *Config) GetString(option string) (value string) {
|
func (c Config) GetString(option string) (value string) {
|
||||||
conf := Config{}
|
value, _ = c.Map[option].(string)
|
||||||
conf = *c
|
|
||||||
value, _ = conf[option].(string)
|
|
||||||
return
|
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
|
// 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)
|
bytes, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = goyaml.Unmarshal(bytes, &config)
|
config := Config{}
|
||||||
if err != nil {
|
if err = goyaml.Unmarshal(bytes, &config); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
146
imgsrv.go
146
imgsrv.go
|
@ -10,41 +10,30 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/vbatts/imgsrv/client"
|
|
||||||
"github.com/vbatts/imgsrv/config"
|
|
||||||
"github.com/vbatts/imgsrv/util"
|
|
||||||
"labix.org/v2/mgo"
|
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/vbatts/imgsrv/client"
|
||||||
|
"github.com/vbatts/imgsrv/config"
|
||||||
|
"github.com/vbatts/imgsrv/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
|
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
|
||||||
|
|
||||||
DefaultRunAsServer = false
|
DefaultConfig = &config.Config{
|
||||||
RunAsServer = DefaultRunAsServer
|
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 = ""
|
PutFile = ""
|
||||||
FetchUrl = ""
|
FetchUrl = ""
|
||||||
FileKeywords = ""
|
FileKeywords = ""
|
||||||
|
@ -53,26 +42,36 @@ var (
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
for _, arg := range flag.Args() {
|
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)
|
log.Printf("%s", arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// loads either default or flag specified config
|
// loads either default or flag specified config
|
||||||
// to override variables
|
// 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)
|
file, err := util.FetchFileFromURL(FetchUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println(file)
|
log.Println(file)
|
||||||
} else if RunAsServer {
|
|
||||||
log.Printf("%s", ServerIP)
|
|
||||||
runServer(ServerIP, ServerPort)
|
|
||||||
} else {
|
} else {
|
||||||
if len(RemoteHost) == 0 {
|
// we're pushing up a file
|
||||||
|
|
||||||
|
if len(DefaultConfig.RemoteHost) == 0 {
|
||||||
log.Println("Please provide a remotehost!")
|
log.Println("Please provide a remotehost!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -87,7 +86,7 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
log.Println("WARN: you didn't provide any keywords :-(")
|
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 {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -98,7 +97,7 @@ func main() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
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")
|
"Provide alternate configuration file")
|
||||||
|
|
||||||
/* Server-side */
|
/* Server-side */
|
||||||
flag.BoolVar(&RunAsServer,
|
flag.BoolVar(&DefaultConfig.Server,
|
||||||
"server",
|
"server",
|
||||||
RunAsServer,
|
DefaultConfig.Server,
|
||||||
"Run as an image server (defaults to client-side)")
|
"Run as an image server (defaults to client-side)")
|
||||||
flag.StringVar(&ServerIP,
|
flag.StringVar(&DefaultConfig.Ip,
|
||||||
"ip",
|
"ip",
|
||||||
ServerIP,
|
DefaultConfig.Ip,
|
||||||
"IP to bind to (if running as a server)('ip' in the config)")
|
"IP to bind to (if running as a server)('ip' in the config)")
|
||||||
flag.StringVar(&ServerPort,
|
flag.StringVar(&DefaultConfig.Port,
|
||||||
"port",
|
"port",
|
||||||
ServerPort,
|
DefaultConfig.Port,
|
||||||
"Port to listen on (if running as a server)('port' in the config)")
|
"Port to listen on (if running as a server)('port' in the config)")
|
||||||
|
|
||||||
/* MongoDB settings */
|
/* MongoDB settings */
|
||||||
flag.StringVar(&MongoHost,
|
flag.StringVar(&DefaultConfig.MongoHost,
|
||||||
"mongo-host",
|
"mongo-host",
|
||||||
MongoHost,
|
DefaultConfig.MongoHost,
|
||||||
"Mongo Host to connect to ('mongohost' in the config)")
|
"Mongo Host to connect to ('mongohost' in the config)")
|
||||||
flag.StringVar(&MongoDB,
|
flag.StringVar(&DefaultConfig.MongoDB,
|
||||||
"mongo-db",
|
"mongo-db",
|
||||||
MongoDB,
|
DefaultConfig.MongoDB,
|
||||||
"Mongo db to connect to ('mongodb' in the config)")
|
"Mongo db to connect to ('mongodb' in the config)")
|
||||||
flag.StringVar(&MongoUsername,
|
flag.StringVar(&DefaultConfig.MongoUsername,
|
||||||
"mongo-username",
|
"mongo-username",
|
||||||
MongoUsername,
|
DefaultConfig.MongoUsername,
|
||||||
"Mongo username to auth with (if needed) ('mongousername' in the config)")
|
"Mongo username to auth with (if needed) ('mongousername' in the config)")
|
||||||
flag.StringVar(&MongoPassword,
|
flag.StringVar(&DefaultConfig.MongoPassword,
|
||||||
"mongo-password",
|
"mongo-password",
|
||||||
MongoPassword,
|
DefaultConfig.MongoPassword,
|
||||||
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
|
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
|
||||||
|
|
||||||
/* Client-side */
|
/* Client-side */
|
||||||
|
@ -152,9 +151,9 @@ func init() {
|
||||||
FetchUrl,
|
FetchUrl,
|
||||||
"Just fetch the file from this url")
|
"Just fetch the file from this url")
|
||||||
|
|
||||||
flag.StringVar(&RemoteHost,
|
flag.StringVar(&DefaultConfig.RemoteHost,
|
||||||
"remotehost",
|
"remotehost",
|
||||||
RemoteHost,
|
DefaultConfig.RemoteHost,
|
||||||
"Remote host to get/put files on ('remotehost' in the config)")
|
"Remote host to get/put files on ('remotehost' in the config)")
|
||||||
flag.StringVar(&PutFile,
|
flag.StringVar(&PutFile,
|
||||||
"put",
|
"put",
|
||||||
|
@ -166,50 +165,3 @@ func init() {
|
||||||
"Keywords to associate with file. (comma delimited) (needs -put)")
|
"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
|
|
||||||
}
|
|
||||||
|
|
28
server.go
28
server.go
|
@ -16,15 +16,26 @@ import (
|
||||||
"github.com/vbatts/imgsrv/hash"
|
"github.com/vbatts/imgsrv/hash"
|
||||||
"github.com/vbatts/imgsrv/types"
|
"github.com/vbatts/imgsrv/types"
|
||||||
"github.com/vbatts/imgsrv/util"
|
"github.com/vbatts/imgsrv/util"
|
||||||
|
"github.com/vbatts/imgsrv/config"
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultPageLimit int = 25
|
var (
|
||||||
|
defaultPageLimit int = 25
|
||||||
|
serverConfig config.Config
|
||||||
|
|
||||||
/* Run as the image server */
|
mongo_session *mgo.Session // FIXME make this not global
|
||||||
func runServer(ip, port string) {
|
images_db *mgo.Database // FIXME make this not global
|
||||||
var addr = fmt.Sprintf("%s:%s", ip, port)
|
gfs *mgo.GridFS // FIXME make this not global
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Run as the file/image server
|
||||||
|
*/
|
||||||
|
func runServer(c *config.Config) {
|
||||||
|
serverConfig = *c
|
||||||
|
|
||||||
initMongo()
|
initMongo()
|
||||||
defer mongo_session.Close()
|
defer mongo_session.Close()
|
||||||
|
@ -44,18 +55,19 @@ func runServer(ip, port string) {
|
||||||
http.HandleFunc("/ext/", routeExt)
|
http.HandleFunc("/ext/", routeExt)
|
||||||
http.HandleFunc("/md5/", routeMD5s)
|
http.HandleFunc("/md5/", routeMD5s)
|
||||||
|
|
||||||
|
addr := fmt.Sprintf("%s:%s", c.Ip, c.Port)
|
||||||
log.Printf("Serving on %s ...", addr)
|
log.Printf("Serving on %s ...", addr)
|
||||||
log.Fatal(http.ListenAndServe(addr, nil))
|
log.Fatal(http.ListenAndServe(addr, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initMongo() {
|
func initMongo() {
|
||||||
mongo_session, err := mgo.Dial(MongoHost)
|
mongo_session, err := mgo.Dial(serverConfig.MongoHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
images_db = mongo_session.DB(MongoDB)
|
images_db = mongo_session.DB(serverConfig.MongoDB)
|
||||||
if len(MongoUsername) > 0 && len(MongoPassword) > 0 {
|
if len(serverConfig.MongoUsername) > 0 && len(serverConfig.MongoPassword) > 0 {
|
||||||
err = images_db.Login(MongoUsername, MongoPassword)
|
err = images_db.Login(serverConfig.MongoUsername, serverConfig.MongoPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue