Transition project to Cobra
This commit is contained in:
parent
56ec1c8570
commit
e2ab8a2110
|
@ -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")
|
||||||
|
}
|
|
@ -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")
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
57
main.go
57
main.go
|
@ -1,56 +1,11 @@
|
||||||
|
/*
|
||||||
|
Copyright © 2024 raul
|
||||||
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "uploader/cmd"
|
||||||
"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 main() {
|
func main() {
|
||||||
serverRoot, err := fs.Sub(assetsFolder, "assets")
|
cmd.Execute()
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue