mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-24 23:06:31 +00:00
cleaned up the dbutil initialization
This commit is contained in:
parent
cebd3acc3c
commit
9b2c342375
4 changed files with 86 additions and 39 deletions
|
@ -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)
|
||||
|
||||
|
@ -37,37 +37,37 @@ func (c Config) GetString(option string) (value string) {
|
|||
}
|
||||
|
||||
func (c *Config) Merge(other *Config) error {
|
||||
if other == nil {
|
||||
return nil
|
||||
}
|
||||
if other.Server {
|
||||
c.Server = other.Server
|
||||
}
|
||||
if len(other.Ip) > 0 {
|
||||
c.Ip = other.Ip
|
||||
}
|
||||
if len(other.Port) > 0 {
|
||||
c.Port = other.Port
|
||||
}
|
||||
if len(other.Port) > 0 {
|
||||
c.Port = other.Port
|
||||
}
|
||||
if len(other.MongoHost) > 0 {
|
||||
c.MongoHost = other.MongoHost
|
||||
}
|
||||
if len(other.MongoDB) > 0 {
|
||||
c.MongoDB = other.MongoDB
|
||||
}
|
||||
if len(other.MongoUsername) > 0 {
|
||||
c.MongoUsername = other.MongoUsername
|
||||
}
|
||||
if len(other.MongoPassword) > 0 {
|
||||
c.MongoPassword = other.MongoPassword
|
||||
}
|
||||
if len(other.RemoteHost) > 0 {
|
||||
c.RemoteHost = other.RemoteHost
|
||||
}
|
||||
return nil
|
||||
if other == nil {
|
||||
return nil
|
||||
}
|
||||
if other.Server {
|
||||
c.Server = other.Server
|
||||
}
|
||||
if len(other.Ip) > 0 {
|
||||
c.Ip = other.Ip
|
||||
}
|
||||
if len(other.Port) > 0 {
|
||||
c.Port = other.Port
|
||||
}
|
||||
if len(other.Port) > 0 {
|
||||
c.Port = other.Port
|
||||
}
|
||||
if len(other.MongoHost) > 0 {
|
||||
c.MongoHost = other.MongoHost
|
||||
}
|
||||
if len(other.MongoDbName) > 0 {
|
||||
c.MongoDbName = other.MongoDbName
|
||||
}
|
||||
if len(other.MongoUsername) > 0 {
|
||||
c.MongoUsername = other.MongoUsername
|
||||
}
|
||||
if len(other.MongoPassword) > 0 {
|
||||
c.MongoPassword = other.MongoPassword
|
||||
}
|
||||
if len(other.RemoteHost) > 0 {
|
||||
c.RemoteHost = other.RemoteHost
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Given a filename to a YAML file, unmarshal it, and return a Config
|
||||
|
|
|
@ -8,8 +8,45 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_DB_NAME = "filesrv"
|
||||
)
|
||||
|
||||
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
|
||||
*/
|
||||
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 {
|
||||
|
|
|
@ -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",
|
||||
|
|
15
server.go
15
server.go
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue