1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-23 16:45:39 +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)
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)
MongoDbName string // mongoDB db name, if different than 'filesrv' (server)
MongoUsername string // mongoDB username, if any (server)
MongoPassword string // mongoDB password, if any (server)
@ -55,8 +55,8 @@ func (c *Config) Merge(other *Config) error {
if len(other.MongoHost) > 0 {
c.MongoHost = other.MongoHost
}
if len(other.MongoDB) > 0 {
c.MongoDB = other.MongoDB
if len(other.MongoDbName) > 0 {
c.MongoDbName = other.MongoDbName
}
if len(other.MongoUsername) > 0 {
c.MongoUsername = other.MongoUsername

View file

@ -8,10 +8,47 @@ import (
"strings"
)
const (
DEFAULT_DB_NAME = "filesrv"
)
type Util struct {
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()
}
/*
pass through for GridFs
*/
@ -71,6 +108,7 @@ Get all the files.
pass -1 for all files
*/
func (u Util) GetFiles(limit int) (files []types.File, err error) {
//files = []types.File{}
if limit == -1 {
err = u.Gfs.Find(nil).Sort("-metadata.timestamp").Limit(limit).All(&files)
} else {

View file

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

View file

@ -36,8 +36,17 @@ Run as the file/image server
func runServer(c *config.Config) {
serverConfig = *c
initMongo()
defer mongo_session.Close()
du = dbutil.Util{
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("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
@ -64,7 +73,7 @@ func initMongo() {
if err != nil {
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 {
err = images_db.Login(serverConfig.MongoUsername, serverConfig.MongoPassword)
if err != nil {