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

cleaned up the dbutil initialization

This commit is contained in:
Vincent Batts 2013-10-03 23:26:01 -04:00
parent cebd3acc3c
commit 9b2c342375
4 changed files with 86 additions and 39 deletions

View file

@ -10,7 +10,7 @@ type Config struct {
Ip string // Bind address, if different than 0.0.0.0 (server) Ip string // Bind address, if different than 0.0.0.0 (server)
Port string // listen port, if different than '7777' (server) Port string // listen port, if different than '7777' (server)
MongoHost string // mongoDB host, if different than 'localhost' (server) MongoHost string // mongoDB host, if different than 'localhost' (server)
MongoDB string // mongoDB db name, if different than 'filesrv' (server) MongoDbName string // mongoDB db name, if different than 'filesrv' (server)
MongoUsername string // mongoDB username, if any (server) MongoUsername string // mongoDB username, if any (server)
MongoPassword string // mongoDB password, if any (server) MongoPassword string // mongoDB password, if any (server)
@ -37,37 +37,37 @@ func (c Config) GetString(option string) (value string) {
} }
func (c *Config) Merge(other *Config) error { func (c *Config) Merge(other *Config) error {
if other == nil { if other == nil {
return nil return nil
} }
if other.Server { if other.Server {
c.Server = other.Server c.Server = other.Server
} }
if len(other.Ip) > 0 { if len(other.Ip) > 0 {
c.Ip = other.Ip c.Ip = other.Ip
} }
if len(other.Port) > 0 { if len(other.Port) > 0 {
c.Port = other.Port c.Port = other.Port
} }
if len(other.Port) > 0 { if len(other.Port) > 0 {
c.Port = other.Port c.Port = other.Port
} }
if len(other.MongoHost) > 0 { if len(other.MongoHost) > 0 {
c.MongoHost = other.MongoHost c.MongoHost = other.MongoHost
} }
if len(other.MongoDB) > 0 { if len(other.MongoDbName) > 0 {
c.MongoDB = other.MongoDB c.MongoDbName = other.MongoDbName
} }
if len(other.MongoUsername) > 0 { if len(other.MongoUsername) > 0 {
c.MongoUsername = other.MongoUsername c.MongoUsername = other.MongoUsername
} }
if len(other.MongoPassword) > 0 { if len(other.MongoPassword) > 0 {
c.MongoPassword = other.MongoPassword c.MongoPassword = other.MongoPassword
} }
if len(other.RemoteHost) > 0 { if len(other.RemoteHost) > 0 {
c.RemoteHost = other.RemoteHost c.RemoteHost = other.RemoteHost
} }
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

View file

@ -8,8 +8,45 @@ import (
"strings" "strings"
) )
const (
DEFAULT_DB_NAME = "filesrv"
)
type Util struct { type Util struct {
Gfs *mgo.GridFS Seed string // mongo host seed to Dial into
User string // mongo credentials, if needed
Pass string // mongo credentials, if needed
DbName string // mongo database name, if needed
Session *mgo.Session
FileDb *mgo.Database
Gfs *mgo.GridFS
}
func (u *Util) Init() error {
var err error
u.Session, err = mgo.Dial(u.Seed)
if err != nil {
return err
}
if len(u.DbName) > 0 {
u.FileDb = u.Session.DB(u.DbName)
} else {
u.FileDb = u.Session.DB(DEFAULT_DB_NAME)
}
if len(u.User) > 0 && len(u.Pass) > 0 {
err = u.FileDb.Login(u.User, u.Pass)
if err != nil {
return err
}
}
u.Gfs = u.FileDb.GridFS("fs")
return nil
}
func (u Util) Close() {
u.Session.Close()
} }
/* /*
@ -71,6 +108,7 @@ Get all the files.
pass -1 for all files pass -1 for all files
*/ */
func (u Util) GetFiles(limit int) (files []types.File, err error) { func (u Util) GetFiles(limit int) (files []types.File, err error) {
//files = []types.File{}
if limit == -1 { if limit == -1 {
err = u.Gfs.Find(nil).Sort("-metadata.timestamp").Limit(limit).All(&files) err = u.Gfs.Find(nil).Sort("-metadata.timestamp").Limit(limit).All(&files)
} else { } else {

View file

@ -28,7 +28,7 @@ var (
Ip: "0.0.0.0", Ip: "0.0.0.0",
Port: "7777", Port: "7777",
MongoHost: "localhost", MongoHost: "localhost",
MongoDB: "filesrv", MongoDbName: "filesrv",
MongoUsername: "", MongoUsername: "",
MongoPassword: "", MongoPassword: "",
RemoteHost: "", RemoteHost: "",
@ -132,9 +132,9 @@ func init() {
"mongo-host", "mongo-host",
DefaultConfig.MongoHost, DefaultConfig.MongoHost,
"Mongo Host to connect to ('mongohost' in the config)") "Mongo Host to connect to ('mongohost' in the config)")
flag.StringVar(&DefaultConfig.MongoDB, flag.StringVar(&DefaultConfig.MongoDbName,
"mongo-db", "mongo-db",
DefaultConfig.MongoDB, DefaultConfig.MongoDbName,
"Mongo db to connect to ('mongodb' in the config)") "Mongo db to connect to ('mongodb' in the config)")
flag.StringVar(&DefaultConfig.MongoUsername, flag.StringVar(&DefaultConfig.MongoUsername,
"mongo-username", "mongo-username",

View file

@ -36,8 +36,17 @@ Run as the file/image server
func runServer(c *config.Config) { func runServer(c *config.Config) {
serverConfig = *c serverConfig = *c
initMongo() du = dbutil.Util{
defer mongo_session.Close() Seed: serverConfig.MongoHost,
User: serverConfig.MongoUsername,
Pass: serverConfig.MongoPassword,
DbName: serverConfig.MongoDbName,
}
err := du.Init()
if err != nil {
log.Fatal(err)
}
defer du.Close() // TODO this ought to catch a signal to cleanup
http.HandleFunc("/", routeRoot) http.HandleFunc("/", routeRoot)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
@ -64,7 +73,7 @@ func initMongo() {
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
images_db = mongo_session.DB(serverConfig.MongoDB) images_db = mongo_session.DB(serverConfig.MongoDbName)
if len(serverConfig.MongoUsername) > 0 && len(serverConfig.MongoPassword) > 0 { if len(serverConfig.MongoUsername) > 0 && len(serverConfig.MongoPassword) > 0 {
err = images_db.Login(serverConfig.MongoUsername, serverConfig.MongoPassword) err = images_db.Login(serverConfig.MongoUsername, serverConfig.MongoPassword)
if err != nil { if err != nil {