diff --git a/.gitea/workflows/go-audit.yaml b/.gitea/workflows/go-audit.yaml
deleted file mode 100644
index 70c5e4b..0000000
--- a/.gitea/workflows/go-audit.yaml
+++ /dev/null
@@ -1,25 +0,0 @@
-name: Go audit
-run-name: ${{ gitea.actor }} pulled to main! 🚀
-on:
- pull_request:
- branches: [main]
-jobs:
- audit:
- runs-on: ubuntu-latest
- steps:
-
- - uses: actions/checkout@v2
-
- - name: Set up Go
- uses: actions/setup-go@v2
- with:
- go-version: '>=1.22.0'
-
- - name: Verify dependencies
- run: go mod verify
-
- - name: Build
- run: go build -v ./...
-
- - name: Run go vet
- run: go vet ./...
diff --git a/.goreleaser.yaml b/.goreleaser.yaml
index 8e02d12..f62d6c6 100644
--- a/.goreleaser.yaml
+++ b/.goreleaser.yaml
@@ -24,7 +24,7 @@ builds:
- CGO_ENABLED=0
goos:
- linux
- - windows
+ #- windows
goarch:
- amd64
diff --git a/README.md b/README.md
index e48a67d..7f6ad77 100644
--- a/README.md
+++ b/README.md
@@ -4,6 +4,12 @@ Small HTTP server that lets users easily upload files to your computer.
## Usage:
```
-./uploader
+Start HTTP server
+
+Usage:
+ uploader server [flags]
+
+Flags:
+ -h, --help help for server
+ -p, --port string Port for server to listen on (default "1302")
```
-Visit http://localhost:8080/
diff --git a/assets/index.html b/assets/index.html
deleted file mode 100644
index b449b2e..0000000
--- a/assets/index.html
+++ /dev/null
@@ -1,38 +0,0 @@
-
-
-
-
-
-
-
-
-
-
Raul's file uploader
-
Find the source here!
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/cmd/assets/index.html b/cmd/assets/index.html
new file mode 100644
index 0000000..fe9218a
--- /dev/null
+++ b/cmd/assets/index.html
@@ -0,0 +1,37 @@
+
+
+
+
+ File Uploader
+
+
+
+
+
+
+
+
+
+
+
+
Raul's file uploader
+
Find the source here!
+
+
+
+
+
+
+
diff --git a/assets/style.css b/cmd/assets/style.css
similarity index 97%
rename from assets/style.css
rename to cmd/assets/style.css
index c68725e..441522a 100644
--- a/assets/style.css
+++ b/cmd/assets/style.css
@@ -21,6 +21,7 @@ img {
button {
background-color: #eee;
border: 2px black solid;
+ margin-bottom: 10px;
}
#but:hover {
diff --git a/cmd/root.go b/cmd/root.go
new file mode 100644
index 0000000..cac581e
--- /dev/null
+++ b/cmd/root.go
@@ -0,0 +1,27 @@
+/*
+Copyright © 2024 raul
+*/
+
+package cmd
+
+import (
+ "github.com/spf13/cobra"
+ "os"
+)
+
+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.`,
+}
+
+func Execute() {
+ err := rootCmd.Execute()
+ if err != nil {
+ os.Exit(1)
+ }
+}
+
+func init() {
+ 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..d077471
--- /dev/null
+++ b/cmd/server.go
@@ -0,0 +1,40 @@
+/*
+Copyright © 2024 raul
+*/
+
+package cmd
+
+import (
+ "github.com/spf13/cobra"
+ "log"
+)
+
+var serveCmd = &cobra.Command{
+ Use: "server",
+ Short: "Start HTTP server",
+ Long: `Start HTTP server`,
+ Run: func(cmd *cobra.Command, args []string) {
+
+ if err := setServerParameters(cmd); err != nil {
+ log.Fatalf("Error happened trying to set parameters: %v\n", err)
+ }
+
+ server()
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(serveCmd)
+ serveCmd.PersistentFlags().StringP("port", "p", "1302", "Port for server to listen on")
+}
+
+func setServerParameters(cmd *cobra.Command) error {
+ parameterPort, err := cmd.Flags().GetString("port")
+ if err != nil {
+ return err
+ }
+ if parameterPort != "" {
+ listenPort = parameterPort
+ }
+ return nil
+}
diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go
new file mode 100644
index 0000000..87bc5ff
--- /dev/null
+++ b/cmd/serverFunc.go
@@ -0,0 +1,73 @@
+package cmd
+
+import (
+ "embed"
+ "fmt"
+ "io"
+ "io/fs"
+ "log"
+ "mime/multipart"
+ "net/http"
+ "os"
+)
+
+var (
+ listenPort string = "1302"
+)
+
+//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")
+ log.Printf("[%v] Received file \"%v\" from %v\n", r.RemoteAddr, header.Filename, r.UserAgent())
+
+ 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)
+ }
+ isDone := make(chan bool)
+
+ go copyTo(isDone, f, file)
+
+ <-isDone
+ log.Printf("Successfully copied file!\n")
+}
+
+func copyTo(isDone chan bool, f *os.File, file multipart.File) {
+ _, err := io.Copy(f, file)
+ if err != nil {
+ log.Printf("Error happened writing file: %v\n", err)
+ }
+ isDone <- true
+}
+
+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.Printf("Listening on port %v...\n", listenPort)
+
+ err = http.ListenAndServe(":"+listenPort, nil)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/go.mod b/go.mod
index 0bf93cf..547f33b 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,10 @@
module uploader
go 1.22.2
+
+require github.com/spf13/cobra v1.8.0
+
+require (
+ github.com/inconshreveable/mousetrap v1.1.0 // indirect
+ github.com/spf13/pflag v1.0.5 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..d0e8c2c
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,10 @@
+github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
+github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
+github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
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()
}