From 3d7e638cc63679349637b6a8cec02ec73cf60b2b Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 13 Sep 2018 15:30:32 +0200 Subject: [PATCH] Initial --- Main.go | 10 ++++++++++ Variables.go | 1 + cli/CmdStart.go | 31 +++++++++++++++++++++++++++++++ cli/CmdVersion.go | 22 ++++++++++++++++++++++ cli/Init.go | 7 +++++++ cli/Variables.go | 12 ++++++++++++ configuration/Scheme.go | 7 +++++++ go.mod | 7 +++++++ go.sum | 6 ++++++ version/Version.go | 14 ++++++++++++++ 10 files changed, 117 insertions(+) create mode 100644 Main.go create mode 100644 Variables.go create mode 100644 cli/CmdStart.go create mode 100644 cli/CmdVersion.go create mode 100644 cli/Init.go create mode 100644 cli/Variables.go create mode 100644 configuration/Scheme.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 version/Version.go diff --git a/Main.go b/Main.go new file mode 100644 index 0000000..b2eedb6 --- /dev/null +++ b/Main.go @@ -0,0 +1,10 @@ +package main + +import ( + "fileserver/cli" +) + +// The entry point of the application. +func main() { + cli.Init() +} diff --git a/Variables.go b/Variables.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/Variables.go @@ -0,0 +1 @@ +package main diff --git a/cli/CmdStart.go b/cli/CmdStart.go new file mode 100644 index 0000000..59f0a00 --- /dev/null +++ b/cli/CmdStart.go @@ -0,0 +1,31 @@ +package cli + +import ( + "fileserver/version" + "log" + "net/http" + + "github.com/spf13/cobra" +) + +func cmdStart() (cmd *cobra.Command) { + cmd = &cobra.Command{ + Use: "start", + Short: "Starts the Simple File Server with the given configuration.", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + // Run the server + log.Printf("Simple File Server v%s", version.VERSION) + log.Printf("Compiled with Go %s", version.GetGoversion()) + log.Printf("Configuration: root directory='%s', bind to='%s", configData.RootDirectory, configData.BindHostAndPort) + log.Println("Start the file server now...") + + http.Handle("/", http.FileServer(http.Dir(configData.RootDirectory))) + http.ListenAndServe(configData.BindHostAndPort, nil) + }, + } + + cmd.Flags().StringVarP(&configData.RootDirectory, "rootDirectory", "d", ".", "The root directory. All files in this directory and its sub-directories get published.") + cmd.Flags().StringVarP(&configData.BindHostAndPort, "bindHostPort", "b", "0.0.0.0:8080", "The address or hostname and port where to bind the file server.") + return +} diff --git a/cli/CmdVersion.go b/cli/CmdVersion.go new file mode 100644 index 0000000..6dd4f88 --- /dev/null +++ b/cli/CmdVersion.go @@ -0,0 +1,22 @@ +package cli + +import ( + "fileserver/version" + "fmt" + + "github.com/spf13/cobra" +) + +func cmdVersion() (cmd *cobra.Command) { + cmd = &cobra.Command{ + Use: "version", + Short: "Prints the current version", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("Simple File Server v%s\n", version.VERSION) + fmt.Printf("Compiled with Go %s", version.GetGoversion()) + }, + } + + return +} diff --git a/cli/Init.go b/cli/Init.go new file mode 100644 index 0000000..83be960 --- /dev/null +++ b/cli/Init.go @@ -0,0 +1,7 @@ +package cli + +func Init() { + rootCLI.AddCommand(cmdStart()) + rootCLI.AddCommand(cmdVersion()) + rootCLI.Execute() +} diff --git a/cli/Variables.go b/cli/Variables.go new file mode 100644 index 0000000..297a4af --- /dev/null +++ b/cli/Variables.go @@ -0,0 +1,12 @@ +package cli + +import ( + "fileserver/Configuration" + + "github.com/spf13/cobra" +) + +var ( + rootCLI = &cobra.Command{Use: "SimpleFileServer"} + configData = &configuration.Container{} +) diff --git a/configuration/Scheme.go b/configuration/Scheme.go new file mode 100644 index 0000000..437617b --- /dev/null +++ b/configuration/Scheme.go @@ -0,0 +1,7 @@ +package configuration + +// The data container for the configuration. +type Container struct { + BindHostAndPort string + RootDirectory string +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5813fb2 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module fileserver + +require ( + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/spf13/cobra v0.0.3 + github.com/spf13/pflag v1.0.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c496234 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= +github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= +github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= diff --git a/version/Version.go b/version/Version.go new file mode 100644 index 0000000..f28ba17 --- /dev/null +++ b/version/Version.go @@ -0,0 +1,14 @@ +package version + +import ( + "runtime" + "strings" +) + +const VERSION = "1.0.0" + +func GetGoversion() (ver string) { + ver = runtime.Version() + ver = strings.Replace(ver, "go", "", 1) + return +}