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


332e8fb8 Peter John

3 years ago
Update readme.md
Files changed (1) hide show
  1. readme.md +126 -2
readme.md CHANGED
@@ -23,10 +23,134 @@ go get -u -v github.com/pyros2097/gromer/cmd/gromer
23
23
 
24
24
  You need to follow this directory structure similar to nextjs for the api route handlers to be generated
25
25
 
26
- Take a look at the example for now,
27
-
28
26
  [Example](https://github.com/pyros2097/gromer/tree/master/_example)
29
27
 
28
+
29
+ **These are normal page routes**
30
+ ```go
31
+ // /pages/get.go
32
+ package todos_page
33
+
34
+ import (
35
+ "context"
36
+
37
+ . "github.com/pyros2097/gromer"
38
+ _ "github.com/pyros2097/gromer/_example/components"
39
+ "github.com/pyros2097/gromer/_example/pages/api/todos"
40
+ . "github.com/pyros2097/gromer/handlebars"
41
+ )
42
+
43
+ type GetParams struct {
44
+ Filter string `json:"filter"`
45
+ Page int `json:"limit"`
46
+ }
47
+
48
+ func GET(ctx context.Context, params GetParams) (HtmlContent, int, error) {
49
+ index := Default(params.Page, 1)
50
+ todos, status, err := todos.GET(ctx, todos.GetParams{
51
+ Filter: params.Filter,
52
+ Limit: index * 10,
53
+ })
54
+ if err != nil {
55
+ return HtmlErr(status, err)
56
+ }
57
+ return Html(`
58
+ {{#Page title="gromer example"}}
59
+ {{#Header}}{{/Header}}
60
+ <section class="todoapp">
61
+ <section class="main">
62
+ <ul class="todo-list" id="todo-list">
63
+ {{#each todos as |todo|}}
64
+ {{#Todo todo=todo}}{{/Todo}}
65
+ {{/each}}
66
+ </ul>
67
+ </section>
68
+ {{/if}}
69
+ </section>
70
+ {{/Page}}
71
+ `).
72
+ Prop("todos", todos).
73
+ Render()
74
+ }
75
+ ```
76
+
77
+
78
+ **These are API routes**
79
+ ```go
80
+ // /pages/api/todos/get.go
81
+ package todos
82
+
83
+ import (
84
+ "context"
85
+
86
+ . "github.com/pyros2097/gromer"
87
+ "github.com/pyros2097/gromer/_example/services"
88
+ )
89
+
90
+ type GetParams struct {
91
+ Limit int `json:"limit"`
92
+ Filter string `json:"filter"`
93
+ }
94
+
95
+ func GET(ctx context.Context, params GetParams) ([]*services.Todo, int, error) {
96
+ limit := Default(params.Limit, 10)
97
+ todos := services.GetAllTodo(ctx, services.GetAllTodoParams{
98
+ Limit: limit,
99
+ })
100
+ if params.Filter == "completed" {
101
+ newTodos := []*services.Todo{}
102
+ for _, v := range todos {
103
+ if v.Completed {
104
+ newTodos = append(newTodos, v)
105
+ }
106
+ }
107
+ return newTodos, 200, nil
108
+ }
109
+ if params.Filter == "active" {
110
+ newTodos := []*services.Todo{}
111
+ for _, v := range todos {
112
+ if !v.Completed {
113
+ newTodos = append(newTodos, v)
114
+ }
115
+ }
116
+ return newTodos, 200, nil
117
+ }
118
+ return todos, 200, nil
119
+ }
120
+
121
+ ```
122
+
123
+ ```go
124
+ // /pages/api/todos/post.go
125
+ package todos
126
+
127
+ import (
128
+ "context"
129
+ "time"
130
+
131
+ "github.com/google/uuid"
132
+ "github.com/pyros2097/gromer/_example/services"
133
+ )
134
+
135
+ type PostParams struct {
136
+ Text string `json:"text"`
137
+ }
138
+
139
+ func POST(ctx context.Context, b PostParams) (*services.Todo, int, error) {
140
+ todo, err := services.CreateTodo(ctx, services.Todo{
141
+ ID: uuid.New().String(),
142
+ Text: b.Text,
143
+ Completed: false,
144
+ CreatedAt: time.Now(),
145
+ UpdatedAt: time.Now(),
146
+ })
147
+ if err != nil {
148
+ return nil, 500, err
149
+ }
150
+ return todo, 200, nil
151
+ }
152
+ ```
153
+
30
154
  # Templating
31
155
 
32
156
  Gromer uses a handlebars like templating language for components and pages. This is a modified version of this package [velvet](https://github.com/gobuffalo/velvet)