Debian Codegraph @

cmd/atomdns/internal/generate/handler.go

raw
1 package generate
2
3 import (
4 "bytes"
5 "errors"
6 "log"
7 "os"
8 "path/filepath"
9 )
10
11 // Handlers returns the handlers.
12 func Handlers(path ...string) ([]string, error) {
13 dir := "."
14 if len(path) > 0 {
15 dir = path[0]
16 }
17
18 subdirs, err := os.ReadDir(dir)
19 if err != nil {
20 log.Fatal(err)
21 }
22 handlers := []string{}
23 for _, d := range subdirs {
24 if !d.IsDir() {
25 continue
26 }
27 handler := dir + "/" + filepath.Join(d.Name(), d.Name()+".go")
28 _, err := os.Stat(handler)
29 if errors.Is(err, os.ErrNotExist) {
30 continue
31 }
32 types, err := Types(handler)
33 if err != nil {
34 return nil, err
35 }
36 // insanely crude check, but if there is a line that matches
37 // 'HandlerFunc(next/_ dns.HandlerFunc) dns.HandlerFunc {' in the file it _is_ an actual handler and
38 // not only a Setupper - global is skipped then for example.
39 p, _ := os.ReadFile(handler)
40 if bytes.Contains(p, []byte(" HandlerFunc(")) {
41 handlers = append(handlers, types...)
42 }
43 }
44 return handlers, nil
45 }