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


0e8cede5 Peter John

tag: v1.2.2

v1.2.2

3 years ago
Fix json errors
Files changed (1) hide show
  1. http.go +10 -45
http.go CHANGED
@@ -12,7 +12,6 @@ import (
12
12
  "os"
13
13
  "reflect"
14
14
  "regexp"
15
- "runtime"
16
15
  "runtime/debug"
17
16
  "strconv"
18
17
  "strings"
@@ -22,7 +21,6 @@ import (
22
21
  "github.com/felixge/httpsnoop"
23
22
  "github.com/go-playground/validator/v10"
24
23
  "github.com/google/uuid"
25
- "github.com/gorilla/handlers"
26
24
  "github.com/gorilla/mux"
27
25
  "github.com/pyros2097/gromer/assets"
28
26
  "github.com/pyros2097/gromer/gsx"
@@ -66,23 +64,20 @@ func init() {
66
64
  gsx.RegisterFunc(GetAssetUrl)
67
65
  }
68
66
 
69
- func getFunctionName(temp interface{}) string {
70
- strs := strings.Split((runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name()), ".")
71
- return strs[len(strs)-1]
72
- }
73
-
74
67
  func RespondError(w http.ResponseWriter, r *http.Request, status int, err error) {
75
68
  if r.Header.Get("Content-Type") == "application/json" {
76
69
  validationErrors, ok := eris.Cause(err).(validator.ValidationErrors)
70
+ errorMap := gsx.M{
71
+ "error": err.Error(),
72
+ }
77
73
  if ok {
78
- w.Header().Set("Content-Type", "application/json")
79
- w.WriteHeader(status)
80
- data, _ := json.Marshal(gsx.M{
81
- "error": GetValidationError(validationErrors),
74
+ errorMap["error"] = GetValidationError(validationErrors)
82
- })
83
- w.Write(data)
84
- return
85
75
  }
76
+ w.Header().Set("Content-Type", "application/json")
77
+ w.WriteHeader(status)
78
+ data, _ := json.Marshal(errorMap)
79
+ w.Write(data)
80
+ return
86
81
  }
87
82
  w.Header().Set("Content-Type", "text/html")
88
83
  w.WriteHeader(status) // always write status last
@@ -116,17 +111,12 @@ func RespondError(w http.ResponseWriter, r *http.Request, status int, err error)
116
111
  gsx.Write(c, w, tags)
117
112
  }
118
113
 
119
- func GetRouteParams(route string) []string {
114
+ func PerformRequest(route string, h interface{}, c interface{}, w http.ResponseWriter, r *http.Request, isJson bool) {
120
115
  params := []string{}
121
116
  found := pathParamsRegex.FindAllString(route, -1)
122
117
  for _, v := range found {
123
118
  params = append(params, strings.Replace(strings.Replace(v, "}", "", 1), "{", "", 1))
124
119
  }
125
- return params
126
- }
127
-
128
- func PerformRequest(route string, h interface{}, c interface{}, w http.ResponseWriter, r *http.Request, isJson bool) {
129
- params := GetRouteParams(route)
130
120
  args := []reflect.Value{reflect.ValueOf(c)}
131
121
  funcType := reflect.TypeOf(h)
132
122
  icount := funcType.NumIn()
@@ -258,23 +248,6 @@ func LogMiddleware(next http.Handler) http.Handler {
258
248
  })
259
249
  }
260
250
 
261
- func CorsMiddleware(next http.Handler) http.Handler {
262
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
263
- w.Header().Set("Access-Control-Allow-Origin", "*")
264
- w.Header().Set("Access-Control-Allow-Methods", "*")
265
- w.Header().Set("Access-Control-Allow-Headers", "*")
266
- if r.Method == "OPTIONS" {
267
- w.WriteHeader(200)
268
- return
269
- }
270
- next.ServeHTTP(w, r)
271
- })
272
- }
273
-
274
- func CompressMiddleware(next http.Handler) http.Handler {
275
- return handlers.CompressHandler(next)
276
- }
277
-
278
251
  func CacheMiddleware(next http.Handler) http.Handler {
279
252
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
280
253
  w.Header().Set("Cache-Control", "public, max-age=2592000") // perma cache for 1 month
@@ -362,13 +335,6 @@ func PageRoute(route string, page, action interface{}) {
362
335
  }).Methods("GET", "POST")
363
336
  }
364
337
 
365
- func ApiRoute(router *mux.Router, method, route string, h interface{}) {
366
- router.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
367
- ctx := context.WithValue(context.WithValue(r.Context(), "url", r.URL), "header", r.Header)
368
- PerformRequest(route, h, ctx, w, r, true)
369
- }).Methods(method, "OPTIONS")
370
- }
371
-
372
338
  func GetUrl(ctx context.Context) *url.URL {
373
339
  return ctx.Value("url").(*url.URL)
374
340
  }
@@ -413,7 +379,6 @@ func Init(status StatusComponent, appAssets embed.FS) {
413
379
 
414
380
  staticRouter := baseRouter.NewRoute().Subrouter()
415
381
  staticRouter.Use(CacheMiddleware)
416
- staticRouter.Use(CompressMiddleware)
417
382
  StaticRoute(staticRouter, "/gromer/", assets.FS)
418
383
  StaticRoute(staticRouter, "/assets/", appAssets)
419
384
  IconsRoute(staticRouter, "/icons/", appAssets)