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


2dead420 Peter John

tag: v0.4.0

v0.4.0

4 years ago
release wapp command
Files changed (1) hide show
  1. cmd/main.go +38 -14
cmd/main.go CHANGED
@@ -16,6 +16,7 @@ import (
16
16
  type Route struct {
17
17
  Method string
18
18
  Path string
19
+ Pkg string
19
20
  }
20
21
 
21
22
  func getMethod(src string) string {
@@ -43,10 +44,17 @@ func getMethod(src string) string {
43
44
  }
44
45
 
45
46
  func getRoute(method, src string) string {
47
+ return strings.ReplaceAll(src, "/"+strings.ToLower(method)+".go", "")
48
+ }
49
+
50
+ func getPackage(src string) string {
51
+ return src
52
+ }
53
+
54
+ func rewritePath(route string) string {
46
55
  muxRoute := bytes.NewBuffer(nil)
47
- baseRoute := strings.Replace(src, "/"+strings.ToLower(method)+".go", "", 1)
48
56
  foundStart := false
49
- for _, v := range baseRoute {
57
+ for _, v := range route {
50
58
  if string(v) == "_" && !foundStart {
51
59
  foundStart = true
52
60
  muxRoute.WriteString("{")
@@ -60,30 +68,47 @@ func getRoute(method, src string) string {
60
68
  return muxRoute.String()
61
69
  }
62
70
 
71
+ func rewritePkg(pkg string) string {
72
+ arr := strings.Split(pkg, "/")
73
+ lastItem := arr[len(arr)-1]
74
+ if strings.Contains(lastItem, "_") {
75
+ return arr[len(arr)-2] + strings.ReplaceAll(lastItem, "_", "")
76
+ }
77
+ return lastItem
78
+ }
79
+
63
80
  func main() {
64
81
  data, err := ioutil.ReadFile("go.mod")
65
82
  if err != nil {
66
- log.Fatalf("go.mod file not found %w", err)
83
+ log.Fatalf("go.mod file not found %s", err.Error())
67
84
  }
68
85
  modTree, err := modfile.Parse("go.mod", data, nil)
69
86
  if err != nil {
70
- log.Fatalf("could not parse go.mod %w", err)
87
+ log.Fatalf("could not parse go.mod %s", err.Error())
71
88
  }
72
89
  moduleName := modTree.Module.Mod.Path
73
90
  routes := []*Route{}
91
+ allPkgs := map[string]string{}
74
92
  err = filepath.Walk("pages",
75
93
  func(filesrc string, info os.FileInfo, err error) error {
76
94
  if err != nil {
77
95
  return err
78
96
  }
79
97
  if !info.IsDir() {
80
- route := strings.Replace(filesrc, "pages", "", 1)
98
+ route := strings.ReplaceAll(filesrc, "pages", "")
81
99
  method := getMethod(route)
82
100
  path := getRoute(method, route)
101
+ pkg := getPackage(path)
102
+ allPkgs[pkg] = ""
83
103
  if path == "" { // for index page
84
104
  path = "/"
105
+ pkg = "pages"
85
106
  }
86
- routes = append(routes, &Route{Method: method, Path: path})
107
+ routes = append(routes, &Route{
108
+ Method: method,
109
+ Path: rewritePath(path),
110
+ Pkg: rewritePkg(pkg),
111
+ })
87
112
  }
88
113
  return nil
89
114
  })
@@ -95,6 +120,7 @@ func main() {
95
120
  }
96
121
  ctx := velvet.NewContext()
97
122
  ctx.Set("moduleName", moduleName)
123
+ ctx.Set("allPkgs", allPkgs)
98
124
  ctx.Set("routes", routes)
99
125
  s, err := velvet.Render(`// GENERATED BY WAPP DO NOT EDIT THIS FILE
100
126
  package main
@@ -108,10 +134,10 @@ import (
108
134
 
109
135
  "github.com/apex/gateway/v2"
110
136
  "github.com/gorilla/mux"
137
+
111
- . "{{ moduleName }}/context"
138
+ "{{ moduleName }}/context"
112
- "{{ moduleName }}/pages"
113
- "{{ moduleName }}/pages/about"
139
+ {{#each allPkgs }}"{{ moduleName }}/pages{{ @key }}"
114
- "{{ moduleName }}/pages/api/todos/_id_"
140
+ {{/each}}
115
141
  )
116
142
 
117
143
  //go:embed assets/*
@@ -122,7 +148,7 @@ func main() {
122
148
  r := mux.NewRouter()
123
149
  r.PathPrefix("/assets/").Handler(http.FileServer(http.FS(assetsFS)))
124
150
 
125
- {{#each routes as |route| }}r.HandleFunc("{{ route.Path }}", Wrap(pages.GET)).Methods("{{ route.Method }}")
151
+ {{#each routes as |route| }}r.HandleFunc("{{ route.Path }}", context.Wrap({{ route.Pkg }}.{{ route.Method }})).Methods("{{ route.Method }}")
126
152
  {{/each}}
127
153
  if !isLambda {
128
154
  println("http server listening on http://localhost:3000")
@@ -137,9 +163,7 @@ func main() {
137
163
  log.Print("running in lambda mode")
138
164
  log.Fatal(gateway.ListenAndServe(":3000", r))
139
165
  }
140
- }
141
-
142
- `, ctx)
166
+ }`, ctx)
143
167
  if err != nil {
144
168
  panic(err)
145
169
  }