2017-07-20 08:03:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-29 12:28:18 +00:00
|
|
|
"path/filepath"
|
2017-07-20 08:03:14 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-07-29 10:53:34 +00:00
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
|
2017-07-29 13:26:33 +00:00
|
|
|
"github.com/hacdias/filemanager/plugins"
|
|
|
|
|
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-29 12:28:18 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
2017-07-20 08:03:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2017-07-29 10:53:34 +00:00
|
|
|
logfile string
|
2017-07-29 13:26:33 +00:00
|
|
|
plugin string
|
2017-08-01 19:49:56 +00:00
|
|
|
locale string
|
2017-07-29 12:28:18 +00:00
|
|
|
port int
|
2017-07-20 08:03:14 +00:00
|
|
|
allowCommands bool
|
|
|
|
allowEdit bool
|
|
|
|
allowNew bool
|
2017-07-30 08:21:17 +00:00
|
|
|
showVer bool
|
|
|
|
version = "master"
|
2017-07-20 08:03:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-07-29 13:57:45 +00:00
|
|
|
flag.StringVarP(&config, "config", "c", "", "Configuration file")
|
2017-07-29 12:28:18 +00:00
|
|
|
flag.IntVarP(&port, "port", "p", 0, "HTTP Port (default is random)")
|
|
|
|
flag.StringVarP(&addr, "address", "a", "", "Address to listen to (default is all of them)")
|
|
|
|
flag.StringVarP(&database, "database", "d", "./filemanager.db", "Database file")
|
|
|
|
flag.StringVarP(&logfile, "log", "l", "stdout", "Errors logger; can use 'stdout', 'stderr' or file")
|
|
|
|
flag.StringVarP(&scope, "scope", "s", ".", "Default scope option for new users")
|
|
|
|
flag.StringVar(&commands, "commands", "git svn hg", "Default commands option for new users")
|
|
|
|
flag.BoolVar(&allowCommands, "allow-commands", true, "Default allow commands option for new users")
|
|
|
|
flag.BoolVar(&allowEdit, "allow-edit", true, "Default allow edit option for new users")
|
|
|
|
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users")
|
2017-08-01 19:49:56 +00:00
|
|
|
flag.StringVar(&locale, "locale", "en", "Default locale for new users")
|
2017-07-29 13:26:33 +00:00
|
|
|
flag.StringVar(&plugin, "plugin", "", "Plugin you want to enable")
|
2017-07-30 08:21:17 +00:00
|
|
|
flag.BoolVarP(&showVer, "version", "v", false, "Show version")
|
2017-07-29 12:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupViper() {
|
|
|
|
viper.SetDefault("Address", "")
|
|
|
|
viper.SetDefault("Port", "0")
|
|
|
|
viper.SetDefault("Database", "./filemanager.db")
|
|
|
|
viper.SetDefault("Scope", ".")
|
|
|
|
viper.SetDefault("Logger", "stdout")
|
|
|
|
viper.SetDefault("Commands", []string{"git", "svn", "hg"})
|
|
|
|
viper.SetDefault("AllowCommmands", true)
|
|
|
|
viper.SetDefault("AllowEdit", true)
|
|
|
|
viper.SetDefault("AllowNew", true)
|
2017-07-29 13:26:33 +00:00
|
|
|
viper.SetDefault("Plugin", "")
|
2017-08-01 19:49:56 +00:00
|
|
|
viper.SetDefault("Locale", "en")
|
2017-07-29 12:28:18 +00:00
|
|
|
|
|
|
|
viper.BindPFlag("Port", flag.Lookup("port"))
|
|
|
|
viper.BindPFlag("Address", flag.Lookup("address"))
|
|
|
|
viper.BindPFlag("Database", flag.Lookup("database"))
|
|
|
|
viper.BindPFlag("Scope", flag.Lookup("scope"))
|
|
|
|
viper.BindPFlag("Logger", flag.Lookup("log"))
|
|
|
|
viper.BindPFlag("Commands", flag.Lookup("commands"))
|
|
|
|
viper.BindPFlag("AllowCommands", flag.Lookup("allow-commands"))
|
|
|
|
viper.BindPFlag("AllowEdit", flag.Lookup("allow-edit"))
|
|
|
|
viper.BindPFlag("AlowNew", flag.Lookup("allow-new"))
|
2017-08-01 19:49:56 +00:00
|
|
|
viper.BindPFlag("Locale", flag.Lookup("locale"))
|
2017-07-29 13:26:33 +00:00
|
|
|
viper.BindPFlag("Plugin", flag.Lookup("plugin"))
|
2017-07-29 12:28:18 +00:00
|
|
|
|
|
|
|
viper.SetConfigName("filemanager")
|
|
|
|
viper.AddConfigPath(".")
|
2017-07-20 08:03:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-07-29 12:28:18 +00:00
|
|
|
setupViper()
|
2017-07-20 08:03:14 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-07-30 08:21:17 +00:00
|
|
|
if showVer {
|
|
|
|
fmt.Println("filemanager version", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2017-07-29 12:28:18 +00:00
|
|
|
// Add a configuration file if set.
|
|
|
|
if config != "" {
|
2017-07-29 13:57:45 +00:00
|
|
|
ext := filepath.Ext(config)
|
|
|
|
dir := filepath.Dir(config)
|
|
|
|
config = strings.TrimSuffix(config, ext)
|
|
|
|
|
|
|
|
if dir != "" {
|
|
|
|
viper.AddConfigPath(dir)
|
|
|
|
config = strings.TrimPrefix(config, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetConfigName(config)
|
2017-07-29 12:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read configuration from a file if exists.
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigParseError); ok {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up process log before anything bad happens.
|
|
|
|
switch viper.GetString("Logger") {
|
2017-07-29 10:53:34 +00:00
|
|
|
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-29 12:28:18 +00:00
|
|
|
// Create a File Manager instance.
|
|
|
|
fm, err := filemanager.New(viper.GetString("Database"), filemanager.User{
|
|
|
|
AllowCommands: viper.GetBool("AllowCommands"),
|
|
|
|
AllowEdit: viper.GetBool("AllowEdit"),
|
|
|
|
AllowNew: viper.GetBool("AllowNew"),
|
|
|
|
Commands: viper.GetStringSlice("Commands"),
|
2017-07-20 08:03:14 +00:00
|
|
|
Rules: []*filemanager.Rule{},
|
2017-08-01 19:49:56 +00:00
|
|
|
Locale: viper.GetString("Locale"),
|
2017-07-20 08:03:14 +00:00
|
|
|
CSS: "",
|
2017-07-29 12:28:18 +00:00
|
|
|
FileSystem: fileutils.Dir(viper.GetString("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
|
|
|
}
|
|
|
|
|
2017-07-29 13:26:33 +00:00
|
|
|
if viper.GetString("Plugin") == "hugo" {
|
|
|
|
// Initialize the default settings for Hugo.
|
|
|
|
hugo := &plugins.Hugo{
|
|
|
|
Root: viper.GetString("Scope"),
|
|
|
|
Public: filepath.Join(viper.GetString("Scope"), "public"),
|
|
|
|
Args: []string{},
|
|
|
|
CleanPublic: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the Hugo executable path.
|
|
|
|
if err = hugo.Find(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fm.ActivatePlugin("hugo", hugo); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 12:28:18 +00:00
|
|
|
// Builds the address and a listener.
|
|
|
|
laddr := viper.GetString("Address") + ":" + viper.GetString("Port")
|
|
|
|
listener, err := net.Listen("tcp", laddr)
|
2017-07-26 09:01:06 +00:00
|
|
|
if err != nil {
|
2017-07-29 10:53:34 +00:00
|
|
|
log.Fatal(err)
|
2017-07-26 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-29 12:28:18 +00:00
|
|
|
// Tell the user the port in which is listening.
|
2017-07-26 09:01:06 +00:00
|
|
|
fmt.Println("Listening on", listener.Addr().String())
|
2017-07-20 08:03:14 +00:00
|
|
|
|
2017-07-29 12:28:18 +00:00
|
|
|
// Starts the server.
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|