-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (111 loc) · 2.92 KB
/
main.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
)
var (
// Version vars updated by go build -ldflags
build = "UNKNOWN"
githash = "UNKNOWN"
version = "UNKNOWN"
exit = os.Exit
exitOnError = flag.ExitOnError
handleFunc = http.HandleFunc
listenAndServe = http.ListenAndServe
logFatalln = log.Fatalln
logPrintf = log.Printf
o = getOpts()
pixelServer = hearAndObey
stdErr = os.Stderr
)
// opts struct for command line options and setting initial variables
type opts struct {
*flag.FlagSet
file *string
httpPath *string
help *bool
ip *string
port *string
version *bool
}
func hearAndObey(parms string) error {
handleFunc(*o.httpPath, loadPix)
return listenAndServe(parms, nil)
}
func cleanArgs(args []string) []string {
var rArgs []string
NEXT:
for _, a := range args {
switch {
case strings.HasPrefix(a, "-test"):
continue NEXT
case strings.HasPrefix(a, "-convey"):
continue NEXT
default:
rArgs = append(rArgs, a)
}
}
return rArgs
}
func (o *opts) setArgs() {
if err := o.Parse(cleanArgs((os.Args[1:]))); err != nil {
o.Usage()
}
switch {
case *o.help:
o.Usage()
exit(0)
case *o.version:
fmt.Printf(" Version:\t\t%s\n Build date:\t\t%s\n Git short hash:\t%v\n", version, build, githash)
exit(0)
}
}
// getOpts returns command line flags and values or displays help
func getOpts() *opts {
flags := flag.NewFlagSet("pixelserv", exitOnError)
flags.Usage = func() {
fmt.Fprintf(stdErr, "Usage: %v [options]\n\n", path.Base(os.Args[0]))
flags.PrintDefaults()
}
return &opts{
file: flags.String("f", "", "load pixel or other content from `<file>` source"),
help: flags.Bool("h", false, "Display help"),
httpPath: flags.String("path", "/", "Set HTTP root path"),
ip: flags.String("ip", "127.0.0.1", "IP address for "+path.Base(os.Args[0])+" to bind to"),
port: flags.String("port", "80", "Port number for "+path.Base(os.Args[0])+" to listen on"),
FlagSet: flags,
version: flags.Bool("version", false, "Show version"),
}
}
func getPix(f string) ([]byte, error) {
if f != "" {
return ioutil.ReadFile(f)
}
return []byte{71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 128, 0, 0, 255, 255, 255, 0, 0, 0, 33, 249, 4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59}, nil
}
func loadPix(w http.ResponseWriter, _ *http.Request) {
pix, err := getPix(*o.file)
if err != nil {
logFatalln(err.Error())
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", http.DetectContentType(pix))
w.Header().Set("Content-Length", fmt.Sprint(len(pix)))
w.Header().Set("Accept-Ranges", "bytes")
i, err := w.Write(pix)
if err != nil {
log.Printf("Write integer: %d", i)
logFatalln(err.Error())
}
}
func main() {
o.setArgs()
logPrintf("web server listening @ http://%v:%v", *o.ip, *o.port)
logFatalln(pixelServer(fmt.Sprintf("%v:%v", *o.ip, *o.port)))
}