1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

examples: writing an API for the Apache Kafka: add a root handler for routes documentation to make navigation easier and add some other methods that may find them useful for request state and routes description

Former-commit-id: 3775aab2386051b23e127ccc9e3a6accdfdee6d0
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-08-05 13:51:05 +03:00
parent 2b2492abfa
commit e5f6bce86f
8 changed files with 97 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@@ -9,6 +9,8 @@ Read [the fully functional example](src/main.go).
## Screens
![](0_docs.png)
![](1_create_topic.png)
![](2_list_topics.png)

View File

@@ -65,12 +65,60 @@ func main() {
}
}
app.Get("/", docsHandler)
// GET : http://localhost:8080
// POST, GET: http://localhost:8080/api/v1/topics
// POST : http://localhost:8080/apiv1/topics/{topic}/produce?key=my-key
// GET : http://localhost:8080/apiv1/topics/{topic}/consume?partition=0&offset=0 (these url query parameters are optional)
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
// simple use-case, you can use templates and views obviously, see the "_examples/views" examples.
func docsHandler(ctx iris.Context) {
ctx.ContentType("text/html") // or ctx.HTML(fmt.Sprintf(...))
ctx.Writef(`<!DOCTYPE html>
<html>
<head>
<style>
th, td {
border: 1px solid black;
padding: 15px;
text-align: left;
}
</style>
</head>`)
defer ctx.Writef("</html>")
ctx.Writef("<body>")
defer ctx.Writef("</body>")
ctx.Writef(`
<table>
<tr>
<th>Method</th>
<th>Path</th>
<th>Handler</th>
</tr>
`)
defer ctx.Writef(`</table>`)
registeredRoutes := ctx.Application().GetRoutesReadOnly()
for _, r := range registeredRoutes {
if r.Path() == "/" { // don't list the root, current one.
continue
}
ctx.Writef(`
<tr>
<td>%s</td>
<td>%s%s</td>
<td>%s</td>
</tr>
`, r.Method(), ctx.Host(), r.Path(), r.MainHandlerName())
}
}
type httpError struct {
Code int `json:"code"`
Reason string `json:"reason"`