Refactor Config: export all fields and don't pass by reference.

main
diff 2021-03-24 03:44:36 +00:00
parent aa0822ceea
commit b0e6894621
1 changed files with 39 additions and 35 deletions

74
main.go
View File

@ -31,37 +31,41 @@ import (
// 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
} }
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,
} }
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.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)")
flag.BoolVar(&conf.uploading, "u", false, "upload files instead of downloading (shorthand)") flag.BoolVar(&conf.Uploading, "u", false, "upload files instead of downloading (shorthand)")
flag.BoolVar(&conf.Multiple, "m", conf.Multiple, "allow uploading multiple files at once (shorthand)")
flag.Parse() flag.Parse()
conf.filePath = flag.Arg(0) conf.FilePath = flag.Arg(0)
conf.fileName = path.Base(conf.filePath) conf.FileName = path.Base(conf.FilePath)
if conf.filePath == "" && !conf.uploading { if conf.FilePath == "" && !conf.Uploading {
return conf, errors.New("no file provided to download") return conf, errors.New("no file provided to download")
} }
@ -90,15 +94,15 @@ 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 {
setupUpload(server, &conf) setupUpload(server, conf)
} else { } else {
setupDownload(server, &conf) setupDownload(server, conf)
} }
ip, err := getIP() ip, err := getIP()
@ -107,8 +111,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
url := fmt.Sprintf("http://%s:%v/%s", ip, conf.port, conf.fileName) url := fmt.Sprintf("http://%s:%v/%s", ip, conf.Port, conf.FileName)
if !conf.hideQR { if !conf.HideQR {
qrterminal.GenerateHalfBlock(url, qrterminal.M, os.Stdout) qrterminal.GenerateHalfBlock(url, qrterminal.M, os.Stdout)
} }
fmt.Println(url) fmt.Println(url)
@ -120,12 +124,12 @@ func main() {
} }
} }
func setupDownload(server *http.Server, conf *Config) { func setupDownload(server *http.Server, conf Config) {
downloads := conf.downloads downloads := conf.Downloads
http.Handle("/", http.RedirectHandler("/"+conf.fileName, http.StatusFound)) // 302 redirect http.Handle("/", http.RedirectHandler("/"+conf.FileName, http.StatusFound)) // 302 redirect
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 {
@ -155,7 +159,7 @@ var uploadTemplate = `{{template "BaseHeader" "RUFF Upload Form"}}
<form enctype="multipart/form-data" action="/" method="post"> <form enctype="multipart/form-data" action="/" method="post">
<label for="file">Select a file for upload:</label> <label for="file">Select a file for upload:</label>
<input type="file" name="file"> <input type="file" name="file">
<input type="submit" value="Upload"{{if .multiple}} multiple{{end}}> <input type="submit" value="Upload"{{if .Multiple}} multiple{{end}}>
</form> </form>
{{template "BaseFooter"}}` {{template "BaseFooter"}}`
@ -164,7 +168,7 @@ var errorTemplate = `{{template "BaseHeader" "Upload Error"}}
<p><a href="/">Go back</a></p> <p><a href="/">Go back</a></p>
{{template "BaseFooter"}}` {{template "BaseFooter"}}`
func setupUpload(server *http.Server, conf *Config) { func setupUpload(server *http.Server, conf Config) {
tpl := template.Must(template.New("BaseHeader").Parse(baseHeader)) tpl := template.Must(template.New("BaseHeader").Parse(baseHeader))
template.Must(tpl.New("BaseFooter").Parse(baseFooter)) template.Must(tpl.New("BaseFooter").Parse(baseFooter))
template.Must(tpl.New("UploadForm").Parse(uploadTemplate)) template.Must(tpl.New("UploadForm").Parse(uploadTemplate))