-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
46 lines (40 loc) · 927 Bytes
/
server.go
File metadata and controls
46 lines (40 loc) · 927 Bytes
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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/spf13/pflag"
)
type Server struct {
port int
code int
redirect string
freeze bool
}
func (s Server) Index(w http.ResponseWriter, r *http.Request) {
if s.freeze {
time.Sleep(60 * time.Second)
}
if s.redirect != "" {
http.Redirect(w, r, s.redirect, s.code)
return
}
w.WriteHeader(s.code)
fmt.Fprintf(w, "It Works!\n")
}
func main() {
s := Server{}
pflag.IntVar(&s.port, "port", 1945, "the port to listen")
pflag.IntVar(&s.code, "code", 200, "HTTP status code to return")
pflag.StringVar(&s.redirect, "redirect", "", "URL to redirect to")
pflag.BoolVar(&s.freeze, "freeze", false, "freeze for all requests")
pflag.Parse()
http.HandleFunc("/", s.Index)
addr := fmt.Sprintf("0.0.0.0:%d", s.port)
fmt.Printf("listening on http://%s/\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatalln(err)
}
}