forked from semihalev/sdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
80 lines (66 loc) · 1.55 KB
/
gen.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
//go:build ignore
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
)
// middleware list order very important, handlers call via this order.
var middlewareList = []string{
"recovery",
"loop",
"metrics",
"accesslist",
"ratelimit",
"edns",
"accesslog",
"chaos",
"hostsfile",
"blocklist",
"as112",
"cache",
"failover",
"resolver",
"forwarder",
}
func main() {
var pathlist []string
for _, name := range middlewareList {
stat, err := os.Stat(filepath.Join(middlewareDir, name))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if !stat.IsDir() {
fmt.Println("path is not directory")
os.Exit(1)
}
pathlist = append(pathlist, filepath.Join(prefixDir, middlewareDir, name))
}
file, err := os.Create(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
file.WriteString("// Code generated by gen.go DO NOT EDIT.\n")
file.WriteString("\npackage main\n\nimport (\n")
file.WriteString("\t\"github.com/semihalev/sdns/config\"\n")
file.WriteString("\t\"github.com/semihalev/sdns/middleware\"\n")
sort.StringSlice(pathlist).Sort()
for _, path := range pathlist {
file.WriteString("\t\"" + path + "\"\n")
}
file.WriteString(")\n\n")
file.WriteString("func init() {\n")
for _, name := range middlewareList {
file.WriteString("\tmiddleware.Register(\"" + name + "\", func(cfg *config.Config) middleware.Handler { return " + name + ".New(cfg) })\n")
}
file.WriteString("}\n")
}
const (
filename = "zregister.go"
prefixDir = "github.com/semihalev/sdns"
middlewareDir = "middleware"
)