~repos /gromer
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.
fix lambda
- app_nowasm.go +2 -27
- router.go +53 -0
app_nowasm.go
CHANGED
|
@@ -10,33 +10,10 @@ import (
|
|
|
10
10
|
"path/filepath"
|
|
11
11
|
"strings"
|
|
12
12
|
|
|
13
|
-
"github.com/
|
|
13
|
+
"github.com/aws/aws-lambda-go/lambda"
|
|
14
14
|
"github.com/markbates/pkger"
|
|
15
15
|
)
|
|
16
16
|
|
|
17
|
-
// ServeFiles serves files from the given file system root.
|
|
18
|
-
// The path must end with "/*filepath", files are then served from the local
|
|
19
|
-
// path /defined/root/dir/*filepath.
|
|
20
|
-
// For example if root is "/etc" and *filepath is "passwd", the local file
|
|
21
|
-
// "/etc/passwd" would be served.
|
|
22
|
-
// Internally a http.FileServer is used, therefore http.NotFound is used instead
|
|
23
|
-
// of the Router's NotFound handler.
|
|
24
|
-
// To use the operating system's file system implementation,
|
|
25
|
-
// use http.Dir:
|
|
26
|
-
// router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
|
|
27
|
-
// func (r *Router) ServeFiles(path string, root http.FileSystem) {
|
|
28
|
-
// if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
|
|
29
|
-
// panic("path must end with /*filepath in path '" + path + "'")
|
|
30
|
-
// }
|
|
31
|
-
|
|
32
|
-
// fileServer := http.FileServer(root)
|
|
33
|
-
|
|
34
|
-
// r.GET(path, func(w http.ResponseWriter, req *http.Request, ps Params) {
|
|
35
|
-
// req.URL.Path = ps.ByName("filepath")
|
|
36
|
-
// fileServer.ServeHTTP(w, req)
|
|
37
|
-
// })
|
|
38
|
-
// }
|
|
39
|
-
|
|
40
17
|
func Run() {
|
|
41
18
|
isLambda := os.Getenv("_LAMBDA_SERVER_PORT") != ""
|
|
42
19
|
if !isLambda {
|
|
@@ -53,9 +30,7 @@ func Run() {
|
|
|
53
30
|
}
|
|
54
31
|
if isLambda {
|
|
55
32
|
println("running in lambda mode")
|
|
56
|
-
|
|
33
|
+
lambda.Start(AppRouter.Lambda)
|
|
57
|
-
BinaryContentTypes: []string{"application/wasm", "image/png"},
|
|
58
|
-
})
|
|
59
34
|
} else {
|
|
60
35
|
println("Serving on HTTP port: 1234")
|
|
61
36
|
http.ListenAndServe(":1234", AppRouter)
|
router.go
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package app
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"bytes"
|
|
4
5
|
"context"
|
|
5
6
|
"net/http"
|
|
6
7
|
"strings"
|
|
@@ -8,6 +9,7 @@ import (
|
|
|
8
9
|
"unicode"
|
|
9
10
|
"unicode/utf8"
|
|
10
11
|
|
|
12
|
+
"github.com/aws/aws-lambda-go/events"
|
|
11
13
|
"github.com/pyros2097/wapp/errors"
|
|
12
14
|
)
|
|
13
15
|
|
|
@@ -925,3 +927,54 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
925
927
|
w.WriteHeader(http.StatusNotFound)
|
|
926
928
|
writePage(r.NotFound(NewRenderContext()), w)
|
|
927
929
|
}
|
|
930
|
+
|
|
931
|
+
type lambdaResponse struct {
|
|
932
|
+
StatusCode int `json:"statusCode"`
|
|
933
|
+
Headers map[string]string `json:"headers"`
|
|
934
|
+
MultiValueHeaders map[string][]string `json:"multiValueHeaders"`
|
|
935
|
+
Body string `json:"body"`
|
|
936
|
+
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
func (r *Router) getPage(ui UI) string {
|
|
940
|
+
b := bytes.NewBuffer(nil)
|
|
941
|
+
writePage(ui, b)
|
|
942
|
+
return b.String()
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
func (r *Router) Lambda(ctx context.Context, e events.APIGatewayV2HTTPRequest) (res lambdaResponse) {
|
|
946
|
+
res.StatusCode = 200
|
|
947
|
+
res.Headers = map[string]string{
|
|
948
|
+
"Content-Type": "text/html",
|
|
949
|
+
}
|
|
950
|
+
// Handle errors
|
|
951
|
+
defer func() {
|
|
952
|
+
if rcv := recover(); rcv != nil {
|
|
953
|
+
var err error
|
|
954
|
+
switch x := rcv.(type) {
|
|
955
|
+
case string:
|
|
956
|
+
err = errors.New(x)
|
|
957
|
+
case error:
|
|
958
|
+
err = x
|
|
959
|
+
default:
|
|
960
|
+
err = errors.New("unknown panic")
|
|
961
|
+
}
|
|
962
|
+
res.Body = r.getPage(r.Error(NewRenderContext(), err))
|
|
963
|
+
}
|
|
964
|
+
}()
|
|
965
|
+
|
|
966
|
+
println("raw path: " + e.RawPath)
|
|
967
|
+
path := strings.Replace(e.RawPath, "/Prod", "/", 1)
|
|
968
|
+
if root := r.trees[e.RequestContext.HTTP.Method]; root != nil {
|
|
969
|
+
// TODO: use _ ps save it to context for useParam()
|
|
970
|
+
if handle, _, _ := root.getValue(path, r.getParams); handle != nil {
|
|
971
|
+
res.Body = r.getPage(handle.(RenderFunc)(NewRenderContext()))
|
|
972
|
+
return
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
// Handle 404
|
|
977
|
+
res.StatusCode = http.StatusNotFound
|
|
978
|
+
res.Body = r.getPage(r.NotFound(NewRenderContext()))
|
|
979
|
+
return
|
|
980
|
+
}
|