-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (73 loc) · 1.95 KB
/
main.go
File metadata and controls
94 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Serve is a very simple static file server in go
Usage:
-p="8100": port to serve on
-d=".": the directory of static files to host
Navigating to http://localhost:8100 will display the index.html or directory
listing file.
*/
package main
import (
"bytes"
"errors"
"fart/messages"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"sync"
"time"
)
// set as LDFLAGS
var (
Version = "v0.0.1"
BuildTarget = "from-source"
BuildDate = "cold-build"
)
func main() {
port := flag.String("p", "8100", "port to serve on")
file := flag.String("f", "", "the file to provide for download")
shareWithWorld := flag.Bool("w", false, "share file through ngrok")
version := flag.Bool("v", false, "show version ouptut")
debug := flag.Bool("d", false, "show debug output")
flag.Parse()
// create debugger
debugger := messages.Debugger{ON: *debug}
if *version {
messages.OutputVersion(Version, BuildTarget, BuildDate, true, 0)
}
if *file == "" {
messages.PrintFileNotProvided(true, 1)
}
// read provided file
data, err := ioutil.ReadFile(*file)
if err != nil {
debugger.UnwrapAndPrint(err)
}
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
debugger.Printf("Serving %s on HTTP port: %s\n", *file, *port)
log.Fatal(http.ListenAndServe(":"+*port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename="+*file)
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
http.ServeContent(w, r, *file, time.Now(), bytes.NewReader(data))
})))
wg.Done()
}()
if *shareWithWorld {
debugger.Println("sharing with public via ngrok")
cmd := exec.Command("ngrok", "http", *port)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if errors.Unwrap(err) == exec.ErrNotFound || err == exec.ErrNotFound {
messages.PrintNgrokNotFound(true, 1)
}
log.Fatalf("failed to share serve in ngrok %v", err)
}
}
wg.Wait()
}