Merge pull request 'Improvements' (#2) from testing into main
Reviewed-on: #2
This commit is contained in:
commit
50c41e73ff
|
@ -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 ./...
|
|
@ -24,7 +24,7 @@ builds:
|
|||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- linux
|
||||
- windows
|
||||
#- windows
|
||||
goarch:
|
||||
- amd64
|
||||
|
||||
|
|
10
README.md
10
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/
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" type="" href="./style.css">
|
||||
<script src="https://unpkg.com/htmx.org@1.9.4/dist/htmx.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
|
||||
<div id="main">
|
||||
<h1>Raul's file uploader</h1>
|
||||
<h3>Find the source <a href="https://git.bulgariu.xyz/raul/uploader" target="_blank">here!</a></h3>
|
||||
<form id='form' hx-encoding='multipart/form-data' hx-post='/api/upload'>
|
||||
<input type="file" name="file" value="">
|
||||
<br>
|
||||
<br>
|
||||
<button id="but">
|
||||
Upload
|
||||
</button>
|
||||
<progress id="progress" value="0" max="100">Upload progress:</progress>
|
||||
</form>
|
||||
<script>
|
||||
htmx.on('#form', 'htmx:xhr:progress', function(evt) {
|
||||
htmx.find('#progress').setAttribute('value', evt.detail.loaded/evt.detail.total * 100)
|
||||
});
|
||||
</script>
|
||||
|
||||
<p class="centered">
|
||||
<!-- <img src="https://bulgariu.xyz/avatar.gif" alt="" width=200px height=200px > -->
|
||||
</p>
|
||||
|
||||
<!-- <footer> -->
|
||||
<!-- Licensed under the GPL v3.0 -->
|
||||
<!-- </footer> -->
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,37 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>File Uploader</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="https://unpkg.com/htmx.org@1.9.4/dist/htmx.min.js"></script>
|
||||
<link href="./style.css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<!-- Licensed under the GPL v3.0 -->
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<div id="main">
|
||||
<h1>Raul's file uploader</h1>
|
||||
<h3>Find the source <a href="https://git.bulgariu.xyz/raul/uploader" target="_blank">here!</a></h3>
|
||||
<form id='form' hx-encoding='multipart/form-data' hx-post='/api/upload'>
|
||||
<input type="file" name="file" value="" required>
|
||||
<br>
|
||||
<br>
|
||||
<button id="but">
|
||||
Upload
|
||||
</button>
|
||||
<progress id="progress" value="0" max="100">Upload progress:</progress>
|
||||
</form>
|
||||
<script>
|
||||
htmx.on('#form', 'htmx:xhr:progress', function (evt) {
|
||||
htmx.find('#progress').setAttribute('value', evt.detail.loaded / evt.detail.total * 100)
|
||||
});
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -21,6 +21,7 @@ img {
|
|||
button {
|
||||
background-color: #eee;
|
||||
border: 2px black solid;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#but:hover {
|
|
@ -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")
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
7
go.mod
7
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
|
||||
)
|
||||
|
|
|
@ -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=
|
57
main.go
57
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue