-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.go
167 lines (136 loc) · 3.35 KB
/
init.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package generator
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"text/template"
"github.com/schartey/dgraph-lambda-go/codegen/config"
)
func GenerateConfig() {
if t, err := os.Open("lambda.yaml"); os.IsNotExist(err) {
f, err := createFile("lambda.yaml")
if err != nil {
panic(err)
}
lambdaTemplate.Execute(f, struct{}{})
f.Close()
} else {
t.Close()
}
}
func Init(config *config.Config) error {
f, err := createFile(config.Exec.Filename)
if err != nil {
panic(err)
}
template.Must(template.New("exec").Parse("package "+config.Exec.Package)).Execute(f, struct{}{})
f.Close()
f, err = createFile(config.Model.Filename)
if err != nil {
panic(err)
}
template.Must(template.New("model").Parse("package "+config.Model.Package)).Execute(f, struct{}{})
f.Close()
f, err = createFile(config.Resolver.Dir)
if err != nil {
panic(err)
}
f.Close()
// Generate Resolver
f, err = createFile(path.Join(config.Resolver.Dir, "resolver.go"))
if err != nil {
panic(err)
}
resolverTemplate.Execute(f, struct {
Package string
}{
Package: config.Resolver.Package,
})
f.Close()
f, err = os.Create("server.go")
if err != nil {
fmt.Println(err.Error())
}
serverTemplate.Execute(f, struct {
ResolverPath string
ResolverPackage string
GeneratedPath string
GeneratedPackage string
Standalone bool
}{
ResolverPath: path.Join(config.Root, config.Resolver.Dir),
ResolverPackage: config.Resolver.Package,
GeneratedPath: path.Join(config.Root, path.Dir(config.Exec.Filename)),
GeneratedPackage: config.Exec.Package,
Standalone: config.Server.Standalone,
})
f.Close()
return nil
}
func createFile(p string) (*os.File, error) {
path := p
file := ""
if strings.Contains(filepath.Base(p), ".") {
path = filepath.Dir(p)
file = filepath.Base(p)
}
if err := os.MkdirAll(path, os.ModePerm); err != nil {
print(err.Error())
return nil, err
}
if file == "" {
return nil, nil
}
return os.Create(p)
}
var resolverTemplate = template.Must(template.New("resolver").Parse(`package {{ .Package }}
// Add objects to your desire
type Resolver struct {
}`))
var lambdaTemplate = template.Must(template.New("lambda").Parse(`schema:
- ./*.graphql
exec:
filename: lambda/generated/generated.go
package: generated
model:
filename: lambda/model/models_gen.go
package: model
autobind:
# - "github.com/schartey/dgraph-lambda-go/examples/models"
resolver:
dir: lambda/resolvers
package: resolvers
filename_template: "{resolver}.resolver.go" # also allow "{name}.resolvers.go"
server:
standalone: true`))
var serverTemplate = template.Must(template.New("server").Parse(`package main
import (
"fmt"
{{ if not .Standalone }}
"net/http"
"github.com/go-chi/chi"
{{ end }}
"github.com/schartey/dgraph-lambda-go/api"
"{{ .GeneratedPath }}"
"{{ .ResolverPath }}"
)
func main() {
{{ if .Standalone }}
resolver := &{{ .ResolverPackage }}.Resolver{}
executer := {{ .GeneratedPackage }}.NewExecuter(resolver)
lambda := api.New(executer)
err := lambda.Serve()
fmt.Println(err.Error())
{{ else }}
r := chi.NewRouter()
resolver := &{{ .ResolverPackage }}.Resolver{}
executer := {{ .GeneratedPackage }}.NewExecuter(resolver)
lambda := api.New(executer)
r.Post("/graphql-worker", lambda.Route)
fmt.Println("Lambda listening on 8686")
fmt.Println(http.ListenAndServe(":8686", r))
{{ end }}
}
`))