Repository navigation
Expand file tree
/
Copy pathdoc.go
More file actions
153 lines (147 loc) · 5.79 KB
/
Copy pathdoc.go
File metadata and controls
153 lines (147 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* MIT License
*
* Copyright (c) 2025 Jonas Kaninda
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Package okapi is a modern, minimalist HTTP web framework for Go,
// inspired by FastAPI's elegance. Designed for simplicity, performance,
// and developer happiness, it helps you build fast, scalable, and well-documented APIs
// with minimal boilerplate.
//
// The framework is named after the okapi (/oʊˈkɑːpiː/), a rare and graceful mammal
// native to the rainforests of the northeastern Democratic Republic of the Congo.
// Just like its namesake — which resembles a blend of giraffe and zebra — Okapi blends
// simplicity and strength in a unique, powerful package.
//
// Key Features:
//
// - Intuitive & Expressive API:
// Clean, declarative syntax for effortless route and middleware definition.
//
// - Automatic Request Binding:
// Seamlessly parse JSON, XML, form data, query params, headers, and path variables into structs.
//
// - Built-in Auth & Security:
// Native support for JWT, OAuth2, Basic Auth, and custom middleware.
//
// - First-Class Documentation:
// OpenAPI 3.0 & Swagger UI integrated out of the box—auto-generate API docs with minimal effort.
//
// - Modern Tooling:
// Route grouping, middleware chaining, static file serving, templating engine support,
// CORS management, fine-grained timeout controls.
//
// - Developer Experience:
// Minimal boilerplate, clear error handling, structured logging, and easy testing.
//
// Okapi is built for speed, simplicity, and real-world use—whether you're prototyping or running in production.
//
// For more information and documentation, visit: https://github.com/jkaninda/okapi
package okapi
import (
"html/template"
"net/http"
)
const (
redoc = `
<!DOCTYPE html>
<html>
<head>
<title> {{.Title }}</title>
<!-- needed for adaptive design -->
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
<!--
Redoc doesn't change outer page styles
-->
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-URL='/openapi.json'></redoc>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
</html>
`
swagger = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title> {{.Title }}</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.27.1/swagger-ui.css" />
<link rel="icon" type="image/png" sizes="32x32" href="https://unpkg.com/swagger-ui-dist@5.27.1/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="https://unpkg.com/swagger-ui-dist@5.27.1/favicon-16x16.png">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.27.1/swagger-ui-bundle.js" charset="UTF-8"></script>
<script src="https://unpkg.com/swagger-ui-dist@5.27.1/swagger-ui-standalone-preset.js" charset="UTF-8"></script>
<script src="https://unpkg.com/swagger-ui-dist@5.27.1/swagger-initializer.js" charset="UTF-8"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/openapi.json',
dom_id: '#swagger-ui',
deepLinking: true,
});
};
</script>
</body>
</html>
`
)
var (
redocTemplate = template.Must(template.New("redoc").Parse(redoc))
swaggerTemplate = template.Must(template.New("swagger").Parse(swagger))
)
// registerDocRoutes registers the OpenAPI documentation routes for the Okapi instance.
func (o *Okapi) registerDocRoutes(title string) {
// Register the OpenAPI JSON route
o.Get(openApiDocPath, func(c *Context) error {
return c.JSON(http.StatusOK, o.openapiSpec)
}).internalRoute().Hide() // Hide the route from the OpenAPI documentation
o.Get("/openapi.yaml", func(c *Context) error {
return c.YAML(http.StatusOK, o.openapiSpec)
}).internalRoute().Hide()
// Register the swagger route
o.Get(openApiDocPrefix, func(c *Context) error {
return c.renderHTML(http.StatusOK, swaggerTemplate, M{"Title": title})
},
).internalRoute().Hide() // Hide the route from the OpenAPI documentation
// TODO: remove this route in the next major release
o.Get("/docs/index.html", func(c *Context) error {
return c.renderHTML(http.StatusOK, swaggerTemplate, M{"Title": title})
},
).internalRoute().Hide() // Hide the route from the OpenAPI documentation
// Register the Redoc route
o.Get("/redoc", func(c *Context) error {
return c.renderHTML(http.StatusOK, redocTemplate, M{"Title": title})
},
).internalRoute().Hide() // Hide the route from the OpenAPI documentation
}