~repos /gromer

#golang#htmx#ssr

git clone https://pyrossh.dev/repos/gromer.git

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


bbfd4c4e Peter John

4 years ago
update code gen
Files changed (1) hide show
  1. cmd/wapp/main.go +83 -1
cmd/wapp/main.go CHANGED
@@ -17,6 +17,15 @@ type Route struct {
17
17
  Method string
18
18
  Path string
19
19
  Pkg string
20
+ ApiSrc string
21
+ IsApi bool
22
+ }
23
+
24
+ type ApiCall struct {
25
+ Method string
26
+ Name string
27
+ Params string
28
+ Path string
20
29
  }
21
30
 
22
31
  func getMethod(src string) string {
@@ -77,6 +86,40 @@ func rewritePkg(pkg string) string {
77
86
  return lastItem
78
87
  }
79
88
 
89
+ func getApiFunc(method, route string) ApiCall {
90
+ muxRoute := bytes.NewBuffer(nil)
91
+ params := ""
92
+ foundStart := false
93
+ funcName := strings.ToLower(method)
94
+ parts := strings.Split(route, "/")
95
+ for _, p := range parts {
96
+ if p != "api" {
97
+ funcName += strings.Title(strings.Replace(p, "_", "", 2))
98
+ }
99
+ }
100
+ for _, v := range route {
101
+ if string(v) == "_" && !foundStart {
102
+ foundStart = true
103
+ muxRoute.WriteString("${")
104
+ } else if string(v) == "_" && foundStart {
105
+ foundStart = false
106
+ muxRoute.WriteString("}")
107
+ params += ", "
108
+ } else {
109
+ if foundStart {
110
+ params += string(v)
111
+ }
112
+ muxRoute.WriteString(string(v))
113
+ }
114
+ }
115
+ return ApiCall{
116
+ Method: method,
117
+ Name: funcName,
118
+ Params: params + "params",
119
+ Path: muxRoute.String(),
120
+ }
121
+ }
122
+
80
123
  func main() {
81
124
  data, err := ioutil.ReadFile("go.mod")
82
125
  if err != nil {
@@ -88,6 +131,7 @@ func main() {
88
131
  }
89
132
  moduleName := modTree.Module.Mod.Path
90
133
  routes := []*Route{}
134
+ apiCalls := []ApiCall{}
91
135
  allPkgs := map[string]string{}
92
136
  err = filepath.Walk("pages",
93
137
  func(filesrc string, info os.FileInfo, err error) error {
@@ -109,6 +153,9 @@ func main() {
109
153
  Path: rewritePath(path),
110
154
  Pkg: rewritePkg(pkg),
111
155
  })
156
+ if strings.Contains(path, "/api/") {
157
+ apiCalls = append(apiCalls, getApiFunc(method, path))
158
+ }
112
159
  }
113
160
  return nil
114
161
  })
@@ -122,7 +169,9 @@ func main() {
122
169
  ctx.Set("moduleName", moduleName)
123
170
  ctx.Set("allPkgs", allPkgs)
124
171
  ctx.Set("routes", routes)
172
+ ctx.Set("apiCalls", apiCalls)
173
+ ctx.Set("tick", "`")
125
- s, err := velvet.Render(`// Code generated by Wapp Cmd Go. DO NOT EDIT.
174
+ s, err := velvet.Render(`// Code generated by wapp. DO NOT EDIT.
126
175
  package main
127
176
 
128
177
  import (
@@ -188,4 +237,37 @@ func wrap(h interface{}) func(http.ResponseWriter, *http.Request) {
188
237
  if err != nil {
189
238
  panic(err)
190
239
  }
240
+ js, err := velvet.Render(`// Code generated by wapp. DO NOT EDIT.
241
+ import queryString from 'query-string';
242
+
243
+ const apiCall = async (method, route, params) => {
244
+ const qs = method === 'GET' && params ? '?' + queryString.stringify(params) : '';
245
+ const body = method !== 'GET' ? JSON.stringify(params) : null;
246
+ const res = await fetch({{tick}}/api/${route}${qs}{{tick}}, {
247
+ method,
248
+ headers: {
249
+ Authorization: localStorage.getItem('token'),
250
+ },
251
+ body,
252
+ });
253
+ const json = await res.json();
254
+ if (res.ok) {
255
+ return json;
256
+ } else {
257
+ throw new Error(json.error);
258
+ }
259
+ }
260
+
261
+ export default {
262
+ {{#each apiCalls as |api| }}{{api.Name}}: ({{api.Params}}) => apiCall('{{api.Method}}', {{tick}}{{api.Path}}{{tick}}, params),
263
+ {{/each}}
264
+ }
265
+ `, ctx)
266
+ if err != nil {
267
+ panic(err)
268
+ }
269
+ err = ioutil.WriteFile("api.js", []byte(js), 0644)
270
+ if err != nil {
271
+ panic(err)
272
+ }
191
273
  }