Run go fmt

It has some Opinions on my code. Mostly whitespace and organizing the ordering of my structs.
main
diff 2021-03-25 19:22:41 +00:00
parent bbdc5325ae
commit cc4913d573
1 changed files with 35 additions and 35 deletions

70
main.go
View File

@ -1,6 +1,6 @@
// Ruff provides a pop-up web server to Retrieve/Upload Files Fast over // Ruff provides a pop-up web server to Retrieve/Upload Files Fast over
// LAN, inspired by WOOF (Web Offer One File) by Simon Budig. // LAN, inspired by WOOF (Web Offer One File) by Simon Budig.
// //
// It's based on the idea that not every device has <insert neat file transfer // It's based on the idea that not every device has <insert neat file transfer
// tool here>, but just about every device that can network has an HTTP client, // tool here>, but just about every device that can network has an HTTP client,
// making a hyper-simple HTTP server a viable option for file transfer with // making a hyper-simple HTTP server a viable option for file transfer with
@ -18,50 +18,50 @@
package main package main
import ( import (
"net"
"net/url"
"net/http"
"time"
"context" "context"
"html/template" "html/template"
"net"
"path" "net/http"
"os" "net/url"
"mime/multipart" "time"
"io"
"io"
"mime/multipart"
"os"
"path"
"fmt"
"flag"
"errors" "errors"
"flag"
"fmt"
"github.com/mdp/qrterminal" "github.com/mdp/qrterminal"
) )
// Config stores all settings for an instance of RUFF. // Config stores all settings for an instance of RUFF.
type Config struct { type Config struct {
Downloads int Downloads int
Port int Port int
FilePath string FilePath string
FileName string FileName string
HideQR bool HideQR bool
Uploading bool Uploading bool
Multiple bool Multiple bool
} }
func getConfig() (Config, error) { func getConfig() (Config, error) {
conf := Config{ conf := Config{
Downloads: 1, Downloads: 1,
Port: 8008, Port: 8008,
HideQR: false, HideQR: false,
Uploading: false, Uploading: false,
Multiple: true, Multiple: true,
} }
flag.IntVar(&conf.Downloads, "count", conf.Downloads, "number of downloads before exiting. set to -1 for unlimited downloads.") flag.IntVar(&conf.Downloads, "count", conf.Downloads, "number of downloads before exiting. set to -1 for unlimited downloads.")
flag.IntVar(&conf.Port, "port", conf.Port, "port to serve file on.") flag.IntVar(&conf.Port, "port", conf.Port, "port to serve file on.")
flag.BoolVar(&conf.HideQR, "hide-qr", conf.HideQR, "hide the QR code.") flag.BoolVar(&conf.HideQR, "hide-qr", conf.HideQR, "hide the QR code.")
flag.BoolVar(&conf.Uploading, "upload", false, "upload files instead of downloading") flag.BoolVar(&conf.Uploading, "upload", false, "upload files instead of downloading")
flag.BoolVar(&conf.Multiple, "multiple", conf.Multiple, "allow uploading multiple files at once") flag.BoolVar(&conf.Multiple, "multiple", conf.Multiple, "allow uploading multiple files at once")
flag.IntVar(&conf.Downloads, "c", conf.Downloads, "number of downloads before exiting. set to -1 for unlimited downloads. (shorthand)") flag.IntVar(&conf.Downloads, "c", conf.Downloads, "number of downloads before exiting. set to -1 for unlimited downloads. (shorthand)")
flag.IntVar(&conf.Port, "p", conf.Port, "port to serve file on. (shorthand)") flag.IntVar(&conf.Port, "p", conf.Port, "port to serve file on. (shorthand)")
flag.BoolVar(&conf.HideQR, "q", conf.HideQR, "hide the QR code. (shorthand)") flag.BoolVar(&conf.HideQR, "q", conf.HideQR, "hide the QR code. (shorthand)")
@ -89,7 +89,7 @@ func getIP() (string, error) {
if !ok { if !ok {
return "", err return "", err
} }
return localAddr.IP.String(), nil return localAddr.IP.String(), nil
} }
@ -103,9 +103,9 @@ func main() {
} }
server := &http.Server{ server := &http.Server{
Addr: fmt.Sprintf(":%v", conf.Port), Addr: fmt.Sprintf(":%v", conf.Port),
ReadTimeout: 10*time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10*time.Second, WriteTimeout: 10 * time.Second,
} }
if conf.Uploading { if conf.Uploading {
@ -134,11 +134,11 @@ func main() {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
// Wait for the server to finish any transfers, up to 3 seconds // Wait for the server to finish any transfers, up to 3 seconds
select { select {
case <-done: case <-done:
case <-time.After(3*time.Second): case <-time.After(3 * time.Second):
} }
} }
@ -148,7 +148,7 @@ func setupDownload(server *http.Server, conf Config) {
http.HandleFunc("/"+conf.FileName, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/"+conf.FileName, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename=\""+url.PathEscape(conf.FileName)+"\"") w.Header().Set("Content-Disposition", "attachment; filename=\""+url.PathEscape(conf.FileName)+"\"")
http.ServeFile(w, r, conf.FilePath) http.ServeFile(w, r, conf.FilePath)
downloads-- downloads--
if downloads == 0 { if downloads == 0 {
go shutdown(server) go shutdown(server)
@ -204,7 +204,7 @@ func setupUpload(server *http.Server, conf Config) {
template.Must(tpl.New("UploadForm").Parse(uploadTemplate)) template.Must(tpl.New("UploadForm").Parse(uploadTemplate))
template.Must(tpl.New("UploadError").Parse(errorTemplate)) template.Must(tpl.New("UploadError").Parse(errorTemplate))
template.Must(tpl.New("UploadMessage").Parse(messageTemplate)) template.Must(tpl.New("UploadMessage").Parse(messageTemplate))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Upload form // Upload form
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
@ -217,7 +217,7 @@ func setupUpload(server *http.Server, conf Config) {
// Handle upload // Handle upload
r.ParseMultipartForm(20 << 20) // Buffer a maximum of 20MB in memory. r.ParseMultipartForm(20 << 20) // Buffer a maximum of 20MB in memory.
files := make([]*multipart.FileHeader, 0, 1) files := make([]*multipart.FileHeader, 0, 1)
for _, field := range r.MultipartForm.File { for _, field := range r.MultipartForm.File {
for _, header := range field { for _, header := range field {
@ -238,7 +238,7 @@ func setupUpload(server *http.Server, conf Config) {
return return
} }
} }
tpl.ExecuteTemplate(w, "UploadMessage", "Upload successful!") tpl.ExecuteTemplate(w, "UploadMessage", "Upload successful!")
go shutdown(server) go shutdown(server)
}) })
@ -250,7 +250,7 @@ func saveFile(header *multipart.FileHeader) error {
return err return err
} }
defer inFile.Close() defer inFile.Close()
outFile, err := os.Create(header.Filename) outFile, err := os.Create(header.Filename)
if err != nil { if err != nil {
return err return err
@ -268,4 +268,4 @@ func saveFile(header *multipart.FileHeader) error {
func shutdown(server *http.Server) { func shutdown(server *http.Server) {
server.Shutdown(context.Background()) server.Shutdown(context.Background())
done <- struct{}{} done <- struct{}{}
} }