Skip to content

Commit c7c1fce

Browse files
committed
chore(golang): switch to go-chi router
Closes #12 Part of #9
1 parent d21a695 commit c7c1fce

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

examples/go/app.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import "fmt"
44
import "net/http"
55
import "os"
6+
import "github.com/go-chi/chi"
67

78
func main() {
8-
registerRoutes()
9+
r := chi.NewRouter()
10+
registerRoutes(r)
911

1012
fmt.Println("Listen on 3000")
11-
if err := http.ListenAndServe(":3000", nil); err != nil {
13+
if err := http.ListenAndServe(":3000", r); err != nil {
1214
fmt.Fprintf(os.Stderr, "ListenAndServe failed: %v", err)
1315
os.Exit(1)
1416
}

examples/go/routes.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
package main
22

33
import "net/http"
4+
import "github.com/go-chi/chi"
45

5-
func registerRoutes() {
6+
func registerRoutes(r chi.Router) {
67

7-
http.HandleFunc("/v1/categories/count", func(w http.ResponseWriter, _ *http.Request) {
8+
r.Get("/v1/categories/count", func(w http.ResponseWriter, _ *http.Request) {
89
w.Write([]byte("TODO"))
910
})
1011

11-
http.HandleFunc("/v1/collections/:collectionId/categories/count", func(w http.ResponseWriter, _ *http.Request) {
12+
r.Get("/v1/collections/{collectionId}/categories/count", func(w http.ResponseWriter, _ *http.Request) {
1213
w.Write([]byte("TODO"))
1314
})
1415

15-
http.HandleFunc("/v1/categories", func(w http.ResponseWriter, _ *http.Request) {
16+
r.Get("/v1/categories", func(w http.ResponseWriter, _ *http.Request) {
1617
w.Write([]byte("TODO"))
1718
})
1819

19-
http.HandleFunc("/v1/categories/:categoryId", func(w http.ResponseWriter, _ *http.Request) {
20+
r.Post("/v1/categories", func(w http.ResponseWriter, _ *http.Request) {
21+
w.Write([]byte("TODO"))
22+
})
23+
24+
r.Get("/v1/categories/{categoryId}", func(w http.ResponseWriter, _ *http.Request) {
25+
w.Write([]byte("TODO"))
26+
})
27+
28+
r.Put("/v1/categories/{categoryId}", func(w http.ResponseWriter, _ *http.Request) {
29+
w.Write([]byte("TODO"))
30+
})
31+
32+
r.Delete("/v1/categories/{categoryId}", func(w http.ResponseWriter, _ *http.Request) {
2033
w.Write([]byte("TODO"))
2134
})
2235

src/cli.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,22 @@ const flattenQuery = (query) => query.replace(/\n[ ]*/g, ' ');
4848
// "WHERE id = :p.categoryId OR id = :b.id" => "WHERE id = :categoryId OR id = :id"
4949
const removePlaceholders = (query) => query.replace(/:[pb]\./g, ':');
5050

51+
// "/categories/:id" => "/categories/{id}"
52+
// (used only with Golang's go-chi)
53+
const convertPathPlaceholders = (path) => {
54+
return path.replace(/:[^\/]+/g, (placeholder) => '{' + placeholder.slice(1) + '}');
55+
};
56+
5157
const createEndpoints = async (destDir, lang, config) => {
5258
const fileName = `routes.${lang}`
5359
console.log('Generate', fileName);
5460
const resultFile = path.join(destDir, fileName);
5561

5662
for (let endpoint of config) {
5763
let path = endpoint.path;
64+
if (lang === 'go') {
65+
path = convertPathPlaceholders(path)
66+
}
5867
if (endpoint.hasOwnProperty('get')) {
5968
console.log('GET', path, '=>', removePlaceholders(flattenQuery(endpoint.get)));
6069
} else if (endpoint.hasOwnProperty('get_list')) {
@@ -99,7 +108,10 @@ const createEndpoints = async (destDir, lang, config) => {
99108
// "SELECT *\n FROM foo" => "'SELECT * FROM foo'"
100109
"formatQuery": (query) => {
101110
return "'" + removePlaceholders(flattenQuery(query)) + "'";
102-
}
111+
},
112+
113+
// (used only with Golang's go-chi)
114+
"convertPathPlaceholders": convertPathPlaceholders,
103115
}
104116
);
105117

src/templates/app.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import "fmt"
44
import "net/http"
55
import "os"
6+
import "github.com/go-chi/chi"
67

78
func main() {
8-
registerRoutes()
9+
r := chi.NewRouter()
10+
registerRoutes(r)
911

1012
fmt.Println("Listen on 3000")
11-
if err := http.ListenAndServe(":3000", nil); err != nil {
13+
if err := http.ListenAndServe(":3000", r); err != nil {
1214
fmt.Fprintf(os.Stderr, "ListenAndServe failed: %v", err)
1315
os.Exit(1)
1416
}

src/templates/routes.go.ejs

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
package main
22

33
import "net/http"
4+
import "github.com/go-chi/chi"
45

5-
func registerRoutes() {
6-
<% endpoints.forEach(function(endpoint) { %>
7-
http.HandleFunc("<%- endpoint.path %>", func(w http.ResponseWriter, _ *http.Request) {
6+
func registerRoutes(r chi.Router) {
7+
<%
8+
endpoints.forEach(function(endpoint) {
9+
const path = convertPathPlaceholders(endpoint.path);
10+
if (endpoint.hasOwnProperty('get') || endpoint.hasOwnProperty('get_list')) {
11+
%>
12+
r.Get("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
813
w.Write([]byte("TODO"))
914
})
10-
<% }) %>
15+
<%
16+
}
17+
if (endpoint.hasOwnProperty('post')) {
18+
%>
19+
r.Post("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
20+
w.Write([]byte("TODO"))
21+
})
22+
<%
23+
}
24+
if (endpoint.hasOwnProperty('put')) {
25+
%>
26+
r.Put("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
27+
w.Write([]byte("TODO"))
28+
})
29+
<%
30+
}
31+
if (endpoint.hasOwnProperty('delete')) {
32+
%>
33+
r.Delete("<%- path %>", func(w http.ResponseWriter, _ *http.Request) {
34+
w.Write([]byte("TODO"))
35+
})
36+
<%
37+
}
38+
})
39+
%>
1140
}

0 commit comments

Comments
 (0)