filebrowser/cmd/filemanager/main.go

135 lines
3.2 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-29 10:53:34 +00:00
"log"
2017-07-26 09:01:06 +00:00
"net"
2017-07-20 08:03:14 +00:00
"net/http"
2017-07-29 10:53:34 +00:00
"os"
2017-07-20 08:03:14 +00:00
"strconv"
"strings"
2017-07-29 10:53:34 +00:00
lumberjack "gopkg.in/natefinch/lumberjack.v2"
2017-07-20 08:03:14 +00:00
"github.com/hacdias/filemanager"
2017-07-27 20:39:23 +00:00
"github.com/hacdias/fileutils"
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"`
2017-07-26 10:28:47 +00:00
Address string `json:"address"`
2017-07-20 08:03:14 +00:00
Commands []string `json:"commands"`
Port int `json:"port"`
2017-07-29 10:53:34 +00:00
Logger string `json:"log"`
2017-07-20 08:03:14 +00:00
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
2017-07-29 10:53:34 +00:00
logfile string
2017-07-20 08:03:14 +00:00
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")
2017-07-29 10:53:34 +00:00
flag.StringVar(&logfile, "log", "stdout", "Logger to log the errors.")
2017-07-20 08:03:14 +00:00
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()
2017-07-29 10:53:34 +00:00
// Set up process log before anything bad happens
switch logfile {
case "stdout":
log.SetOutput(os.Stdout)
case "stderr":
log.SetOutput(os.Stderr)
case "":
log.SetOutput(ioutil.Discard)
default:
log.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 100,
MaxAge: 14,
MaxBackups: 10,
})
}
2017-07-20 08:03:14 +00:00
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: "",
2017-07-27 20:39:23 +00:00
FileSystem: fileutils.Dir(scope),
2017-07-20 08:03:14 +00:00
})
if err != nil {
2017-07-29 10:53:34 +00:00
log.Fatal(err)
2017-07-20 08:03:14 +00:00
}
fm.SetBaseURL("/")
fm.SetPrefixURL("/")
2017-07-26 09:01:06 +00:00
listener, err := net.Listen("tcp", addr+":"+port)
if err != nil {
2017-07-29 10:53:34 +00:00
log.Fatal(err)
2017-07-26 09:01:06 +00:00
}
fmt.Println("Listening on", listener.Addr().String())
if err := http.Serve(listener, fm); err != nil {
2017-07-29 10:53:34 +00:00
log.Fatal(err)
2017-07-20 08:03:14 +00:00
}
}
func loadConfig() {
file, err := ioutil.ReadFile(config)
if err != nil {
2017-07-29 10:53:34 +00:00
log.Fatal(err)
2017-07-20 08:03:14 +00:00
}
var conf *confFile
err = json.Unmarshal(file, &conf)
if err != nil {
2017-07-29 10:53:34 +00:00
log.Fatal(err)
2017-07-20 08:03:14 +00:00
}
database = conf.Database
scope = conf.Scope
2017-07-26 10:28:47 +00:00
addr = conf.Address
2017-07-29 10:53:34 +00:00
logfile = conf.Logger
2017-07-20 08:03:14 +00:00
commands = strings.Join(conf.Commands, " ")
port = strconv.Itoa(conf.Port)
allowNew = conf.AllowNew
allowEdit = conf.AllowEdit
allowCommands = conf.AllowCommands
}