-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
160 lines (113 loc) Β· 3.01 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"path"
"srv42/utils"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
const version = "1.3.1"
var (
hostv6 = flag.String("hostv6", "[::1]", "IPv6 host to listen on")
hostv4 = flag.String("hostv4", "127.0.0.1", "IPv4 host to listen on")
port = flag.Int("p", 1337, "port to use")
status = flag.Int("s", 202, "status code to return")
verbose = flag.Bool("v", false, "should srv42 print the full path and body")
debug = flag.Bool("d", false, "print debug information")
printVersion = flag.Bool("version", false, "print srv42 version")
wait = 60 * time.Second
root *string
)
func printv() {
fmt.Printf("SRV42 - v%v\n", version)
os.Exit(0)
}
func ListenHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(*status)
if *verbose {
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s\n\n", string(requestDump))
}
}
func Init() {
args := flag.Args()
utils.Debug(debug, fmt.Sprintf("Arguments | %v", args))
if len(args) == 0 {
utils.Debug(debug, "No arguments provided, fallback to listen only")
return
}
utils.Debug(debug, fmt.Sprintf("Flags | hosts: %v + %v - port: %v", *hostv6, *hostv4, *port))
if path.IsAbs(args[0]) {
root = &args[0]
utils.Debug(debug, fmt.Sprintf("Path is absolute: %s", *root))
} else {
cwd, err := os.Getwd()
utils.CheckErr(err)
absPath := path.Join(cwd, args[0])
root = &absPath
utils.Debug(debug, fmt.Sprintf("Path is not absolute | cwd: %s + %s : %s", cwd, args[0], *root))
}
}
func Serve() {
r := mux.NewRouter()
if root != nil {
fileServer := http.FileServer(http.Dir(*root))
r.PathPrefix("/").Handler(fileServer).Methods(http.MethodGet, http.MethodOptions)
} else {
handler := http.HandlerFunc(ListenHandler)
r.PathPrefix("/").Handler(handler).Methods(http.MethodGet, http.MethodOptions, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete)
}
r.Use(mux.CORSMethodMiddleware(r))
l := handlers.LoggingHandler(os.Stdout, r)
srvV6 := &http.Server{
Handler: l,
Addr: fmt.Sprintf("%s:%d", *hostv6, *port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
srvV4 := &http.Server{
Handler: l,
Addr: fmt.Sprintf("%s:%d", *hostv4, *port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
fmt.Printf("Serving on: http://localhost:%d/\n", *port)
go func() {
err := srvV6.ListenAndServe()
utils.CheckErr(err)
}()
go func() {
err := srvV4.ListenAndServe()
utils.CheckErr(err)
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
srvV6.Shutdown(ctx)
srvV4.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}
func main() {
flag.Parse()
if *printVersion {
printv()
}
Init()
Serve()
}