~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.


84140a54 Peter John

4 years ago
improve cli
cmd/scripts/migrate/main.go DELETED
@@ -1,32 +0,0 @@
1
- package main
2
-
3
- import (
4
- c "context"
5
- "io/ioutil"
6
-
7
- "wapp-example/context"
8
- )
9
-
10
- func main() {
11
- db := context.InitDB()
12
- ctx := c.Background()
13
- tx, err := context.BeginTransaction(db, ctx)
14
- if err != nil {
15
- panic(err)
16
- }
17
- files, err := ioutil.ReadDir("./migrations")
18
- if err != nil {
19
- panic(err)
20
- }
21
- for _, f := range files {
22
- data, err := ioutil.ReadFile("./migrations/" + f.Name())
23
- if err != nil {
24
- panic(err)
25
- }
26
- tx.MustExec(string(data))
27
- }
28
- err = tx.Commit()
29
- if err != nil {
30
- panic(err)
31
- }
32
- }
cmd/scripts/seed/main.go DELETED
@@ -1,38 +0,0 @@
1
- package main
2
-
3
- import (
4
- c "context"
5
-
6
- "github.com/bxcodec/faker/v3"
7
-
8
- "wapp-example/context"
9
- "wapp-example/pages/api/todos"
10
- )
11
-
12
- func main() {
13
- db := context.InitDB()
14
- ctx := c.Background()
15
- tx, err := context.BeginTransaction(db, ctx)
16
- if err != nil {
17
- panic(err)
18
- }
19
- reqContext := context.ReqContext{
20
- Tx: tx,
21
- UserID: "123",
22
- }
23
- for i := 0; i < 20; i++ {
24
- ti := todos.TodoInput{}
25
- err := faker.FakeData(&ti)
26
- if err != nil {
27
- panic(err)
28
- }
29
- _, _, err = todos.POST(reqContext, ti)
30
- if err != nil {
31
- panic(err)
32
- }
33
- }
34
- err = tx.Commit()
35
- if err != nil {
36
- panic(err)
37
- }
38
- }
cmd/wapp/main.go CHANGED
@@ -121,6 +121,60 @@ func getApiFunc(method, route string, params []string) ApiCall {
121
121
  }
122
122
  }
123
123
 
124
+ // "io/ioutil"
125
+ // func migrate() {
126
+ // db := context.InitDB()
127
+ // ctx := c.Background()
128
+ // tx, err := context.BeginTransaction(db, ctx)
129
+ // if err != nil {
130
+ // panic(err)
131
+ // }
132
+ // files, err := ioutil.ReadDir("./migrations")
133
+ // if err != nil {
134
+ // panic(err)
135
+ // }
136
+ // for _, f := range files {
137
+ // data, err := ioutil.ReadFile("./migrations/" + f.Name())
138
+ // if err != nil {
139
+ // panic(err)
140
+ // }
141
+ // tx.MustExec(string(data))
142
+ // }
143
+ // err = tx.Commit()
144
+ // if err != nil {
145
+ // panic(err)
146
+ // }
147
+ // }
148
+
149
+ // "github.com/bxcodec/faker/v3"
150
+ // func seed() {
151
+ // db := context.InitDB()
152
+ // ctx := c.Background()
153
+ // tx, err := context.BeginTransaction(db, ctx)
154
+ // if err != nil {
155
+ // panic(err)
156
+ // }
157
+ // reqContext := context.ReqContext{
158
+ // Tx: tx,
159
+ // UserID: "123",
160
+ // }
161
+ // for i := 0; i < 20; i++ {
162
+ // ti := todos.TodoInput{}
163
+ // err := faker.FakeData(&ti)
164
+ // if err != nil {
165
+ // panic(err)
166
+ // }
167
+ // _, _, err = todos.POST(reqContext, ti)
168
+ // if err != nil {
169
+ // panic(err)
170
+ // }
171
+ // }
172
+ // err = tx.Commit()
173
+ // if err != nil {
174
+ // panic(err)
175
+ // }
176
+ // }
177
+
124
178
  func main() {
125
179
  data, err := ioutil.ReadFile("go.mod")
126
180
  if err != nil {
@@ -144,13 +198,13 @@ func main() {
144
198
  method := getMethod(route)
145
199
  path := getRoute(method, route)
146
200
  pkg := getPackage(path)
147
- routePath := rewritePath(path)
148
- params := pathParamsRegex.FindAllString(routePath, -1)
149
201
  allPkgs[pkg] = ""
150
202
  if path == "" { // for index page
151
203
  path = "/"
152
204
  pkg = "pages"
153
205
  }
206
+ routePath := rewritePath(path)
207
+ params := pathParamsRegex.FindAllString(routePath, -1)
154
208
  routes = append(routes, &Route{
155
209
  Method: method,
156
210
  Path: routePath,
@@ -180,7 +234,6 @@ package main
180
234
 
181
235
  import (
182
236
  "embed"
183
- "log"
184
237
  "net/http"
185
238
  "os"
186
239
  "time"
@@ -188,6 +241,8 @@ import (
188
241
  "github.com/apex/gateway/v2"
189
242
  "github.com/gorilla/mux"
190
243
  "github.com/pyros2097/wapp"
244
+ "github.com/rs/zerolog"
245
+ "github.com/rs/zerolog/log"
191
246
 
192
247
  "{{ moduleName }}/context"
193
248
  {{#each allPkgs }}"{{ moduleName }}/pages{{ @key }}"
@@ -198,6 +253,8 @@ import (
198
253
  var assetsFS embed.FS
199
254
 
200
255
  func main() {
256
+ zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
257
+ log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
201
258
  isLambda := os.Getenv("_LAMBDA_SERVER_PORT") != ""
202
259
  r := mux.NewRouter()
203
260
  r.PathPrefix("/assets/").Handler(http.FileServer(http.FS(assetsFS)))
@@ -211,25 +268,24 @@ func main() {
211
268
  WriteTimeout: 30 * time.Second,
212
269
  ReadTimeout: 30 * time.Second,
213
270
  }
214
- log.Fatal(srv.ListenAndServe())
271
+ log.Fatal().Stack().Err(srv.ListenAndServe()).Msg("failed to listen")
215
272
  } else {
216
273
  log.Print("running in lambda mode")
217
- log.Fatal(gateway.ListenAndServe(":3000", r))
274
+ log.Fatal().Stack().Err(gateway.ListenAndServe(":3000", r)).Msg("failed to listen")
218
275
  }
219
276
  }
220
277
 
221
278
  func handle(router *mux.Router, method, route string, h interface{}) {
222
279
  router.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
280
+ log.Info().Msgf("%-4.4s %s", r.Method, r.URL.Path)
223
- ctx, err := context.NewReqContext(r)
281
+ ctx, err := context.WithContext(r.Context())
224
282
  if err != nil {
225
283
  wapp.RespondError(w, 500, err)
226
284
  return
227
285
  }
228
286
  err = wapp.PerformRequest(route, h, ctx, w, r)
229
287
  if err != nil {
230
- ctx.Rollback()
288
+ log.Error().Stack().Err(err).Msg("")
231
- } else {
232
- ctx.Commit()
233
289
  }
234
290
  }).Methods(method)
235
291
  }
readme.md CHANGED
@@ -23,16 +23,6 @@ go mod init
23
23
  go get -u -v github.com/pyros2097/wapp/cmd/wapp
24
24
  ```
25
25
 
26
- # Example
26
+ # Example
27
27
 
28
28
  https://github.com/pyros2097/wapp-example
29
-
30
- ```go
31
- func WithWappContext(ctx context.Context) context.Context {
32
- return context.WithValue(ctx, "wapp", WappContext{})
33
- }
34
-
35
- func GetWappContext(ctx context.Context) WappContext {
36
- return ctx.Value("wapp").(WaåppContext)
37
- }
38
- ```