filebrowser/cmd/filemanager/main.go

108 lines
2.6 KiB
Go
Raw Normal View History

2017-07-20 08:03:14 +00:00
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
2017-07-26 09:01:06 +00:00
"net"
2017-07-20 08:03:14 +00:00
"net/http"
"strconv"
"strings"
"github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/dir"
2017-07-20 08:03:14 +00:00
)
// confFile contains the configuration file for this File Manager instance.
// If the user chooses to use a configuration file, the flags will be ignored.
type confFile struct {
Database string `json:"database"`
Scope string `json:"scope"`
Commands []string `json:"commands"`
Port int `json:"port"`
AllowCommands bool `json:"allowCommands"`
AllowEdit bool `json:"allowEdit"`
AllowNew bool `json:"allowNew"`
}
var (
2017-07-26 09:01:06 +00:00
addr string
2017-07-20 08:03:14 +00:00
config string
database string
scope string
commands string
port string
allowCommands bool
allowEdit bool
allowNew bool
)
func init() {
flag.StringVar(&config, "config", "", "JSON configuration file")
2017-07-26 09:01:06 +00:00
flag.StringVar(&port, "port", "0", "HTTP Port (default is random)")
flag.StringVar(&addr, "address", "", "Address to listen to (default is all of them)")
2017-07-20 08:03:14 +00:00
flag.StringVar(&database, "database", "./filemanager.db", "Database path")
2017-07-25 11:25:15 +00:00
flag.StringVar(&scope, "scope", ".", "Default scope for new users")
2017-07-20 08:03:14 +00:00
flag.StringVar(&commands, "commands", "git svn hg", "Space separated commands available for new users")
flag.BoolVar(&allowCommands, "allow-commands", true, "Default allow commands option")
flag.BoolVar(&allowEdit, "allow-edit", true, "Default allow edit option")
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option")
}
func main() {
flag.Parse()
if config != "" {
loadConfig()
}
fm, err := filemanager.New(database, filemanager.User{
AllowCommands: allowCommands,
AllowEdit: allowEdit,
AllowNew: allowNew,
Commands: strings.Split(strings.TrimSpace(commands), " "),
Rules: []*filemanager.Rule{},
CSS: "",
FileSystem: dir.Dir(scope),
2017-07-20 08:03:14 +00:00
})
if err != nil {
panic(err)
}
fm.SetBaseURL("/")
fm.SetPrefixURL("/")
2017-07-26 09:01:06 +00:00
listener, err := net.Listen("tcp", addr+":"+port)
if err != nil {
panic(err)
}
fmt.Println("Listening on", listener.Addr().String())
if err := http.Serve(listener, fm); err != nil {
2017-07-20 08:03:14 +00:00
panic(err)
}
}
func loadConfig() {
file, err := ioutil.ReadFile(config)
if err != nil {
panic(err)
}
var conf *confFile
err = json.Unmarshal(file, &conf)
if err != nil {
panic(err)
}
database = conf.Database
scope = conf.Scope
commands = strings.Join(conf.Commands, " ")
port = strconv.Itoa(conf.Port)
allowNew = conf.AllowNew
allowEdit = conf.AllowEdit
allowCommands = conf.AllowCommands
}