From e2ab8a2110c4c53a546a03c2794092b784b891c7 Mon Sep 17 00:00:00 2001 From: raul Date: Fri, 3 May 2024 08:23:58 +0200 Subject: [PATCH] Transition project to Cobra --- {assets => cmd/assets}/index.html | 0 {assets => cmd/assets}/style.css | 0 cmd/root.go | 41 ++++++++++++++++++++++ cmd/server.go | 33 ++++++++++++++++++ cmd/serverFunc.go | 56 ++++++++++++++++++++++++++++++ main.go | 57 ++++--------------------------- 6 files changed, 136 insertions(+), 51 deletions(-) rename {assets => cmd/assets}/index.html (100%) rename {assets => cmd/assets}/style.css (100%) create mode 100644 cmd/root.go create mode 100644 cmd/server.go create mode 100644 cmd/serverFunc.go diff --git a/assets/index.html b/cmd/assets/index.html similarity index 100% rename from assets/index.html rename to cmd/assets/index.html diff --git a/assets/style.css b/cmd/assets/style.css similarity index 100% rename from assets/style.css rename to cmd/assets/style.css diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..8fbfd17 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,41 @@ +/* +Copyright © 2024 raul +*/ + +package cmd + +import ( + "github.com/spf13/cobra" + "os" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "uploader", + Short: "Small HTTP server written in Go that lets users easily upload files to your computer.", + Long: `Small HTTP server written in Go that lets users easily upload files to your computer.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.uploader.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..1b19f76 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,33 @@ +/* +Copyright © 2024 raul +*/ + +package cmd + +import ( + "github.com/spf13/cobra" +) + +// serveCmd represents the serve command +var serveCmd = &cobra.Command{ + Use: "server", + Short: "Start HTTP server", + Long: `Start HTTP server`, + Run: func(cmd *cobra.Command, args []string) { + server() + }, +} + +func init() { + rootCmd.AddCommand(serveCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + serveCmd.PersistentFlags().String("port", "1302", "Port for server to listen on") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go new file mode 100644 index 0000000..909f10f --- /dev/null +++ b/cmd/serverFunc.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "embed" + "fmt" + "io" + "io/fs" + "log" + "net/http" + "os" +) + +//go:embed assets +var assetsFolder embed.FS + +func uploadFile(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Successful upload!") + + file, header, err := r.FormFile("file") + + if err != nil { + log.Printf("Error happened receiving file: %v\n", err) + } + + defer file.Close() + + _, err = os.Stat("./temp/") + if os.IsNotExist(err) { + os.Mkdir("./temp/", 0700) + } + + f, err := os.OpenFile("./temp/"+header.Filename, os.O_WRONLY|os.O_CREATE, 0660) + if err != nil { + log.Printf("Error happened opening file: %v\n", err) + } + + log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent()) + io.Copy(f, file) +} + +func server() { + serverRoot, err := fs.Sub(assetsFolder, "assets") + if err != nil { + log.Fatalf("Error occurred setting HTML root: %v\n", err) + } + + http.Handle("/", http.FileServer(http.FS(serverRoot))) + http.HandleFunc("/api/upload", uploadFile) + + fmt.Println("Listening on port 8080...") + + err = http.ListenAndServe(":8080", nil) + if err != nil { + log.Fatal(err) + } +} diff --git a/main.go b/main.go index 7c2cce8..8a8ff67 100644 --- a/main.go +++ b/main.go @@ -1,56 +1,11 @@ +/* +Copyright © 2024 raul +*/ + package main -import ( - "embed" - "fmt" - "io" - "io/fs" - "log" - "net/http" - "os" -) - -//go:embed assets -var assetsFolder embed.FS - -func uploadFile(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, "Successful upload!") - - file, header, err := r.FormFile("file") - - if err != nil { - log.Printf("Error happened receiving file: %v\n", err) - } - - defer file.Close() - - _, err = os.Stat("./temp/") - if os.IsNotExist(err) { - os.Mkdir("./temp/", 0700) - } - - f, err := os.OpenFile("./temp/"+header.Filename, os.O_WRONLY|os.O_CREATE, 0660) - if err != nil { - log.Printf("Error happened opening file: %v\n", err) - } - - log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent()) - io.Copy(f, file) -} +import "uploader/cmd" func main() { - serverRoot, err := fs.Sub(assetsFolder, "assets") - if err != nil { - log.Fatalf("Error occurred setting HTML root: %v\n", err) - } - - http.Handle("/", http.FileServer(http.FS(serverRoot))) - http.HandleFunc("/api/upload", uploadFile) - - fmt.Println("Listening on port 8080...") - - err = http.ListenAndServe(":8080", nil) - if err != nil { - log.Fatal(err) - } + cmd.Execute() }