-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
59 lines (50 loc) · 1.25 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
/*
* Copyright (C) 2016 wikiwi.io
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package main
import (
"fmt"
"net/http"
"os"
"regexp"
"github.com/jessevdk/go-flags"
)
var version = "0.2.1-dev"
var content = "User-agent: *\nDisallow: /\n"
var opts struct {
Listen string `long:"listen" description:"address to listen on" default:"0.0.0.0:8080" env:"ROBOTS_DISALLOW_LISTEN"`
Version bool `long:"version" short:"v" description:"show version number"`
}
type handler struct {
PathPattern *regexp.Regexp
MetaImport string
RedirectName string
RedirectTo string
}
func (h *handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
_, err := rw.Write([]byte(content))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
func main() {
parser := flags.NewParser(&opts, flags.Default)
parser.Name = "robots-disallow"
_, err := parser.Parse()
if err != nil {
if e2, ok := err.(*flags.Error); ok && e2.Type == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
if opts.Version {
fmt.Println(version)
} else {
h := &handler{}
fmt.Println("Listening on " + opts.Listen + "...")
panic(http.ListenAndServe(opts.Listen, h))
}
}