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

165 lines
3.9 KiB
Go
Raw Normal View History

2013-01-11 21:06:36 +00:00
package main
/*
Fun Times. This is to serve as a single utility that
2013-01-11 21:06:36 +00:00
* can be an image server, that stores into a mongo backend,
OR
* the client side tool that pushes/pulls images to the running server.
*/
import (
2013-03-27 20:30:24 +00:00
"flag"
"fmt"
"log"
"net/url"
"os"
2013-08-06 03:06:05 +00:00
"github.com/vbatts/imgsrv/client"
"github.com/vbatts/imgsrv/config"
"github.com/vbatts/imgsrv/util"
2013-01-11 21:06:36 +00:00
)
var (
2013-03-27 20:30:24 +00:00
ConfigFile = fmt.Sprintf("%s/.imgsrv.yaml", os.Getenv("HOME"))
2013-08-06 03:06:05 +00:00
DefaultConfig = &config.Config{
Server: false,
Ip: "0.0.0.0",
Port: "7777",
MongoHost: "localhost",
2013-10-04 03:26:01 +00:00
MongoDbName: "filesrv",
2013-08-06 03:06:05 +00:00
MongoUsername: "",
MongoPassword: "",
RemoteHost: "",
}
PutFile = ""
FetchUrl = ""
FileKeywords = ""
2013-01-11 21:06:36 +00:00
)
2013-02-11 16:00:57 +00:00
func main() {
2013-03-27 20:30:24 +00:00
flag.Parse()
for _, arg := range flag.Args() {
2013-08-06 03:06:05 +00:00
// TODO What to do with these floating args ...
// Assume they're files and upload them?
2013-03-27 20:30:24 +00:00
log.Printf("%s", arg)
}
// loads either default or flag specified config
// to override variables
2013-08-06 03:06:05 +00:00
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
2013-03-27 20:30:24 +00:00
2013-05-10 19:03:54 +00:00
file, err := util.FetchFileFromURL(FetchUrl)
2013-03-27 20:30:24 +00:00
if err != nil {
log.Println(err)
return
}
log.Println(file)
2013-08-06 03:06:05 +00:00
2013-03-27 20:30:24 +00:00
} else {
// we're pushing up a file
2013-08-06 03:06:05 +00:00
if len(DefaultConfig.RemoteHost) == 0 {
2013-03-27 20:30:24 +00:00
log.Println("Please provide a remotehost!")
return
}
if len(PutFile) == 0 { //&& len(flag.Args()) == 0) {
log.Println("Please provide files to be uploaded!")
return
}
params := map[string]string{}
2013-03-27 20:30:24 +00:00
if len(FileKeywords) > 0 {
params["keywords"] = FileKeywords
2013-03-27 20:30:24 +00:00
} else {
log.Println("WARN: you didn't provide any keywords :-(")
}
url, err := url.Parse(DefaultConfig.RemoteHost + "/f/")
2013-03-27 20:30:24 +00:00
if err != nil {
log.Println(err)
return
}
url_path, err := client.PutFileFromPath(url.String(), PutFile, params)
2013-03-27 20:30:24 +00:00
if err != nil {
log.Println(err)
return
}
2013-10-10 14:31:27 +00:00
fmt.Printf("%s%s\n", DefaultConfig.RemoteHost, url_path)
2013-03-27 20:30:24 +00:00
}
2013-02-11 16:00:57 +00:00
}
2013-05-11 03:44:02 +00:00
/*
http://golang.org/doc/effective_go.html#init
TODO refactor flags and config, to assign, instead of pass by reference.
http://play.golang.org/p/XhGqn-MOjL
*/
2013-01-11 21:06:36 +00:00
func init() {
2013-03-27 20:30:24 +00:00
flag.StringVar(&ConfigFile,
"config",
ConfigFile,
"Provide alternate configuration file")
/* Server-side */
2013-08-06 03:06:05 +00:00
flag.BoolVar(&DefaultConfig.Server,
2013-03-27 20:30:24 +00:00
"server",
2013-08-06 03:06:05 +00:00
DefaultConfig.Server,
2013-03-27 20:30:24 +00:00
"Run as an image server (defaults to client-side)")
2013-08-06 03:06:05 +00:00
flag.StringVar(&DefaultConfig.Ip,
2013-03-27 20:30:24 +00:00
"ip",
2013-08-06 03:06:05 +00:00
DefaultConfig.Ip,
2013-03-27 20:30:24 +00:00
"IP to bind to (if running as a server)('ip' in the config)")
2013-08-06 03:06:05 +00:00
flag.StringVar(&DefaultConfig.Port,
2013-03-27 20:30:24 +00:00
"port",
2013-08-06 03:06:05 +00:00
DefaultConfig.Port,
2013-03-27 20:30:24 +00:00
"Port to listen on (if running as a server)('port' in the config)")
/* MongoDB settings */
2013-08-06 03:06:05 +00:00
flag.StringVar(&DefaultConfig.MongoHost,
2013-03-27 20:30:24 +00:00
"mongo-host",
2013-08-06 03:06:05 +00:00
DefaultConfig.MongoHost,
2013-03-27 20:30:24 +00:00
"Mongo Host to connect to ('mongohost' in the config)")
2013-10-04 03:26:01 +00:00
flag.StringVar(&DefaultConfig.MongoDbName,
2013-03-27 20:30:24 +00:00
"mongo-db",
2013-10-04 03:26:01 +00:00
DefaultConfig.MongoDbName,
2013-03-27 20:30:24 +00:00
"Mongo db to connect to ('mongodb' in the config)")
2013-08-06 03:06:05 +00:00
flag.StringVar(&DefaultConfig.MongoUsername,
2013-03-27 20:30:24 +00:00
"mongo-username",
2013-08-06 03:06:05 +00:00
DefaultConfig.MongoUsername,
2013-03-27 20:30:24 +00:00
"Mongo username to auth with (if needed) ('mongousername' in the config)")
2013-08-06 03:06:05 +00:00
flag.StringVar(&DefaultConfig.MongoPassword,
2013-03-27 20:30:24 +00:00
"mongo-password",
2013-08-06 03:06:05 +00:00
DefaultConfig.MongoPassword,
2013-03-27 20:30:24 +00:00
"Mongo password to auth with (if needed) ('mongopassword' in the config)")
/* Client-side */
flag.StringVar(&FetchUrl,
"fetch",
FetchUrl,
"Just fetch the file from this url")
2013-08-06 03:06:05 +00:00
flag.StringVar(&DefaultConfig.RemoteHost,
2013-03-27 20:30:24 +00:00
"remotehost",
2013-08-06 03:06:05 +00:00
DefaultConfig.RemoteHost,
2013-03-27 20:30:24 +00:00
"Remote host to get/put files on ('remotehost' in the config)")
flag.StringVar(&PutFile,
"put",
PutFile,
"Put file on remote server (needs -remotehost)")
flag.StringVar(&FileKeywords,
"keywords",
FileKeywords,
"Keywords to associate with file. (comma delimited) (needs -put)")
2013-01-11 21:06:36 +00:00
}