This commit is contained in:
Graham Steffaniak 2023-09-01 19:22:38 -05:00
parent 87da81310d
commit 1cd055d768
2 changed files with 22 additions and 17 deletions

View File

@ -19,6 +19,7 @@ import (
v "github.com/spf13/viper" v "github.com/spf13/viper"
lumberjack "gopkg.in/natefinch/lumberjack.v2" lumberjack "gopkg.in/natefinch/lumberjack.v2"
"github.com/gtsteffaniak/filebrowser/auth"
"github.com/gtsteffaniak/filebrowser/diskcache" "github.com/gtsteffaniak/filebrowser/diskcache"
fbhttp "github.com/gtsteffaniak/filebrowser/http" fbhttp "github.com/gtsteffaniak/filebrowser/http"
"github.com/gtsteffaniak/filebrowser/img" "github.com/gtsteffaniak/filebrowser/img"
@ -44,12 +45,16 @@ func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
cobra.MousetrapHelpText = "" cobra.MousetrapHelpText = ""
rootCmd.SetVersionTemplate("File Browser version {{printf \"%s\" .Version}}\n") rootCmd.SetVersionTemplate("File Browser version {{printf \"%s\" .Version}}\n")
settings.Initialize()
log.Println(settings.GlobalConfiguration)
flags := rootCmd.Flags() flags := rootCmd.Flags()
persistent := rootCmd.PersistentFlags() persistent := rootCmd.PersistentFlags()
persistent.StringVarP(&cfgFile, "config", "c", "", "config file path") persistent.StringVarP(&cfgFile, "config", "c", "", "config file path")
persistent.StringP("database", "d", "./filebrowser.db", "database path")
flags.Bool("noauth", false, "use the noauth auther when using quick setup")
flags.String("username", "admin", "username for the first user when using quick config") flags.String("username", "admin", "username for the first user when using quick config")
flags.String("password", "", "hashed password for the first user when using quick config (default \"admin\")") flags.String("password", "", "hashed password for the first user when using quick config (default \"admin\")")
} }
@ -83,7 +88,6 @@ the quick setup mode and a new database will be bootstraped and a new
user created with the credentials from options "username" and "password".`, user created with the credentials from options "username" and "password".`,
Run: python(func(cmd *cobra.Command, args []string, d pythonData) { Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
serverConfig := settings.GlobalConfiguration.Server serverConfig := settings.GlobalConfiguration.Server
log.Println(cfgFile)
if !d.hadDB { if !d.hadDB {
quickSetup(cmd.Flags(), d) quickSetup(cmd.Flags(), d)
@ -111,8 +115,7 @@ user created with the credentials from options "username" and "password".`,
checkErr(err) checkErr(err)
var listener net.Listener var listener net.Listener
listenAddress := serverConfig.Address address := serverConfig.Address + ":" + strconv.Itoa(serverConfig.Port)
address := listenAddress + ":" + strconv.Itoa(serverConfig.Port)
switch { switch {
case serverConfig.Socket != "": case serverConfig.Socket != "":
@ -138,8 +141,7 @@ user created with the credentials from options "username" and "password".`,
sigc := make(chan os.Signal, 1) sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM) signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
go cleanupHandler(listener, sigc) go cleanupHandler(listener, sigc)
_, err = os.Stat("frontend/dist")
checkErr(err)
assetsFs := dirFS{Dir: http.Dir("frontend/dist")} assetsFs := dirFS{Dir: http.Dir("frontend/dist")}
handler, err := fbhttp.NewHandler(imgSvc, fileCache, d.store, &serverConfig, assetsFs) handler, err := fbhttp.NewHandler(imgSvc, fileCache, d.store, &serverConfig, assetsFs)
checkErr(err) checkErr(err)
@ -243,7 +245,13 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) {
} }
var err error var err error
set.Auth.Method = settings.GlobalConfiguration.Auth.Method if settings.GlobalConfiguration.Auth.Method == "noAuth" {
set.Auth.Method = "noAuth"
err = d.store.Auth.Save(&auth.NoAuth{})
} else {
set.Auth.Method = "json"
err = d.store.Auth.Save(&auth.JSONAuth{})
}
err = d.store.Settings.Save(set) err = d.store.Settings.Save(set)
checkErr(err) checkErr(err)

View File

@ -10,7 +10,7 @@ import (
var GlobalConfiguration Settings var GlobalConfiguration Settings
func init() { func Initialize() {
// Open and read the YAML file // Open and read the YAML file
yamlFile, err := os.Open("filebrowser.yml") yamlFile, err := os.Open("filebrowser.yml")
if err != nil { if err != nil {
@ -28,8 +28,6 @@ func init() {
if err != nil { if err != nil {
log.Fatalf("Error unmarshaling YAML data: %v", err) log.Fatalf("Error unmarshaling YAML data: %v", err)
} }
log.Printf("GlobalConfiguration: \n%#v\n", GlobalConfiguration)
// Now you have the Settings struct with values from the YAML file // Now you have the Settings struct with values from the YAML file
// You can access the values like: defaultSettings.Key, defaultSettings.Server.Port, etc. // You can access the values like: defaultSettings.Key, defaultSettings.Server.Port, etc.
} }
@ -37,17 +35,16 @@ func init() {
func setDefaults() { func setDefaults() {
GlobalConfiguration = Settings{ GlobalConfiguration = Settings{
Signup: true, Signup: true,
Server: Server{ Server: Server{
IndexingInterval: 5, IndexingInterval: 5,
Port: 8080, Port: 8080,
NumImageProcessors: 1, NumImageProcessors: 1,
BaseURL: "/", BaseURL: "/files",
}, },
Auth: Auth{ Auth: Auth{
Method: "noauth",
Recaptcha: Recaptcha{ Recaptcha: Recaptcha{
Host: "", Host: "",
}, },
}, },
} }
} }