-
Notifications
You must be signed in to change notification settings - Fork 248
/
httpmiddleware.go
46 lines (42 loc) · 979 Bytes
/
httpmiddleware.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
package codegen
import (
"github.com/sirupsen/logrus"
"github.com/unionj-cloud/go-doudou/version"
"os"
"path/filepath"
"text/template"
)
var httpMwTmpl = `/**
* Generated by go-doudou {{.Version}}.
* You can edit it as your need.
*/
package httpsrv`
// GenHttpMiddleware generates http middleware file
func GenHttpMiddleware(dir string) {
var (
err error
mwfile string
f *os.File
tpl *template.Template
httpDir string
)
httpDir = filepath.Join(dir, "transport/httpsrv")
if err = os.MkdirAll(httpDir, os.ModePerm); err != nil {
panic(err)
}
mwfile = filepath.Join(httpDir, "middleware.go")
if _, err = os.Stat(mwfile); os.IsNotExist(err) {
if f, err = os.Create(mwfile); err != nil {
panic(err)
}
defer f.Close()
tpl, _ = template.New("middleware.go.tmpl").Parse(httpMwTmpl)
_ = tpl.Execute(f, struct {
Version string
}{
Version: version.Release,
})
} else {
logrus.Warnf("file %s already exists", mwfile)
}
}