Skip to content

Commit ef7d746

Browse files
committed
feat: add ipv6 support
1 parent 7446383 commit ef7d746

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

main.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ import (
1616
"github.com/gorilla/mux"
1717
)
1818

19-
const version = "1.2.0"
19+
const version = "1.3.0"
2020

2121
var (
22-
host = flag.String("h", "127.0.0.1", "host to listen on")
22+
hostv6 = flag.String("hostv6", "[::1]", "IPv6 host to listen on")
23+
hostv4 = flag.String("hostv4", "127.0.0.1", "IPv4 host to listen on")
2324
port = flag.Int("p", 1337, "port to use")
2425
status = flag.Int("s", 202, "status code to return")
2526
verbose = flag.Bool("v", false, "should srv42 print the full path and body")
2627
debug = flag.Bool("d", false, "print debug information")
2728

29+
printVersion = flag.Bool("version", false, "print srv42 version")
30+
2831
wait = 60 * time.Second
2932

3033
root *string
3134
)
3235

36+
func printv() {
37+
fmt.Printf("SRV42 - v%v\n", version)
38+
os.Exit(0)
39+
}
40+
3341
func ListenHandler(w http.ResponseWriter, r *http.Request) {
3442
w.WriteHeader(*status)
3543
}
@@ -46,7 +54,7 @@ func Init() {
4654

4755
}
4856

49-
utils.Debug(debug, fmt.Sprintf("Flags | host: %v - port: %v", *host, *port))
57+
utils.Debug(debug, fmt.Sprintf("Flags | hosts: %v + %v - port: %v", *hostv6, *hostv4, *port))
5058

5159
if path.IsAbs(args[0]) {
5260

@@ -86,9 +94,17 @@ func Serve() {
8694

8795
l := handlers.LoggingHandler(os.Stdout, r)
8896

89-
srv := &http.Server{
97+
srvV6 := &http.Server{
98+
Handler: l,
99+
Addr: fmt.Sprintf("%s:%d", *hostv6, *port),
100+
WriteTimeout: 15 * time.Second,
101+
ReadTimeout: 15 * time.Second,
102+
IdleTimeout: 60 * time.Second,
103+
}
104+
105+
srvV4 := &http.Server{
90106
Handler: l,
91-
Addr: fmt.Sprintf("%s:%d", *host, *port),
107+
Addr: fmt.Sprintf("%s:%d", *hostv4, *port),
92108
WriteTimeout: 15 * time.Second,
93109
ReadTimeout: 15 * time.Second,
94110
IdleTimeout: 60 * time.Second,
@@ -97,7 +113,12 @@ func Serve() {
97113
fmt.Printf("Serving on: http://localhost:%d/\n", *port)
98114

99115
go func() {
100-
err := srv.ListenAndServe()
116+
err := srvV6.ListenAndServe()
117+
utils.CheckErr(err)
118+
}()
119+
120+
go func() {
121+
err := srvV4.ListenAndServe()
101122
utils.CheckErr(err)
102123
}()
103124

@@ -110,7 +131,8 @@ func Serve() {
110131
ctx, cancel := context.WithTimeout(context.Background(), wait)
111132
defer cancel()
112133

113-
srv.Shutdown(ctx)
134+
srvV6.Shutdown(ctx)
135+
srvV4.Shutdown(ctx)
114136

115137
log.Println("shutting down")
116138
os.Exit(0)
@@ -119,6 +141,10 @@ func Serve() {
119141
func main() {
120142
flag.Parse()
121143

144+
if *printVersion {
145+
printv()
146+
}
147+
122148
Init()
123149

124150
Serve()

0 commit comments

Comments
 (0)