Skip to content

Commit 5261687

Browse files
authored
feat: added some examples (#50)
1 parent 8bd9b31 commit 5261687

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed

.codacy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ exclude_paths:
99
- "examples/**"
1010
- ".github/**"
1111
- "testdata/**"
12-
- "**.md"
12+
- "**.md"

examples/json/main.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/mojixcoder/kid"
7+
)
8+
9+
type MeRequest struct {
10+
LastName string `json:"last_name"`
11+
}
12+
13+
type MeResponse struct {
14+
FullName string `json:"full_name"`
15+
Age int `json:"age"`
16+
}
17+
18+
func main() {
19+
k := kid.New()
20+
21+
k.Get("/me", meHandler)
22+
23+
k.Run()
24+
}
25+
26+
func meHandler(c *kid.Context) {
27+
var req MeRequest
28+
29+
if err := c.ReadJSON(&req); err != nil {
30+
c.JSON(http.StatusBadRequest, kid.Map{"message": "invalid request body"})
31+
return
32+
}
33+
34+
me := MeResponse{
35+
FullName: "Mojix " + req.LastName,
36+
Age: 23,
37+
}
38+
39+
c.JSON(http.StatusOK, me)
40+
}

examples/main.go

-19
This file was deleted.

examples/path_parameters/main.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/mojixcoder/kid"
8+
)
9+
10+
func main() {
11+
k := kid.New()
12+
13+
k.Get("/greet/{name}", greetHandler)
14+
15+
// It will be matched to any number of paramters. Including zero and one and so on.
16+
k.Get("/path/{*starParam}", starParamHandler)
17+
18+
k.Run()
19+
}
20+
21+
func greetHandler(c *kid.Context) {
22+
name := c.Param("name")
23+
24+
c.String(http.StatusOK, fmt.Sprintf("Hello %s!", name))
25+
}
26+
27+
func starParamHandler(c *kid.Context) {
28+
param := c.Param("starParam")
29+
30+
c.String(http.StatusOK, fmt.Sprintf("Star path parameter: %q", param))
31+
}

examples/simple/main.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/mojixcoder/kid"
7+
)
8+
9+
func main() {
10+
k := kid.New()
11+
12+
k.Get("/greet", greetHandler)
13+
14+
k.Run()
15+
}
16+
17+
func greetHandler(c *kid.Context) {
18+
c.String(http.StatusOK, "Hello World!")
19+
}

0 commit comments

Comments
 (0)