1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-30 03:55:42 +00:00

*: fixes to be working on new abstractions

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2017-04-07 13:29:07 -04:00
parent b80d34f423
commit 610cd418ca
Signed by: vbatts
GPG key ID: 10937E57733F1362
3 changed files with 43 additions and 10 deletions

View file

@ -7,11 +7,11 @@ import (
) )
// Handles are all the register backing Handlers // Handles are all the register backing Handlers
var Handles map[string]Handler var Handles = map[string]Handler{}
// Handler is the means of getting "files" from the backing database // Handler is the means of getting "files" from the backing database
type Handler interface { type Handler interface {
Init(config interface{}) error Init(config []byte, err error) error
Close() error Close() error
Open(filename string) (File, error) Open(filename string) (File, error)
@ -33,14 +33,38 @@ type Handler interface {
GetKeywords() (kp []types.IdCount, err error) GetKeywords() (kp []types.IdCount, err error)
} }
type MetaDataer interface { // File is what is stored and fetched from the backing database
GetMeta(result interface{}) (err error)
SetMeta(metadata interface{})
}
type File interface { type File interface {
io.Reader io.Reader
io.Writer io.Writer
io.Closer io.Closer
MetaDataer MetaDataer
} }
// MetaDataer allows set/get for optional metadata
type MetaDataer interface {
/*
GetMeta unmarshals the optional "metadata" field associated with the file into
the result parameter. The meaning of keys under that field is user-defined. For
example:
result := struct{ INode int }{}
err = file.GetMeta(&result)
if err != nil {
panic(err.String())
}
fmt.Printf("inode: %d\n", result.INode)
*/
GetMeta(result interface{}) (err error)
/*
SetMeta changes the optional "metadata" field associated with the file. The
meaning of keys under that field is user-defined. For example:
file.SetMeta(bson.M{"inode": inode})
It is a runtime error to call this function when the file is not open for
writing.
*/
SetMeta(metadata interface{})
}

View file

@ -1,6 +1,7 @@
package mongo package mongo
import ( import (
"encoding/json"
"strings" "strings"
"github.com/vbatts/imgsrv/dbutil" "github.com/vbatts/imgsrv/dbutil"
@ -29,8 +30,15 @@ type mongoHandle struct {
Gfs *mgo.GridFS Gfs *mgo.GridFS
} }
func (h *mongoHandle) Init(config interface{}) (err error) { func (h *mongoHandle) Init(config []byte, err error) error {
h.config = config.(dbConfig) if err != nil {
return err
}
h.config = dbConfig{}
if err := json.Unmarshal(config, &h.config); err != nil {
return err
}
h.Session, err = mgo.Dial(h.config.Seed) h.Session, err = mgo.Dial(h.config.Seed)
if err != nil { if err != nil {

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -45,7 +46,7 @@ func runServer(c *config.Config) {
serverConfig.MongoDbName, serverConfig.MongoDbName,
} }
if err := du.Init(duConfig); err != nil { if err := du.Init(json.Marshal(duConfig)); err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer du.Close() // TODO this ought to catch a signal to cleanup defer du.Close() // TODO this ought to catch a signal to cleanup