Skip to content

Commit

Permalink
feat: open v1 doc (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ns2Kracy authored Jun 4, 2024
1 parent 4329316 commit 2d41560
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
24 changes: 24 additions & 0 deletions api/index_v1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>CasaOS | Developers</title>

<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">

<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>

<body>
<redoc spec-url='app_management/openapi_v1.yaml' expandResponses='all' jsonSampleExpandLevel='all'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
</body>

</html>
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ var (
//go:embed api/index.html
_docHTML string

//go:embed api/index_v1.html
_docHTMLV1 string

//go:embed api/app_management/openapi.yaml
_docYAML string

//go:embed api/app_management/openapi_v1.yaml
_docYAMLV1 string

//go:embed build/sysroot/etc/casaos/app-management.conf.sample
_confSample string
)
Expand Down Expand Up @@ -129,6 +135,7 @@ func main() {
"/v1/apps",
"/v1/container",
"/v1/app-categories",
route.V1DocPath,
route.V2APIPath,
route.V2DocPath,
}
Expand All @@ -145,13 +152,15 @@ func main() {

v1Router := route.InitV1Router()
v2Router := route.InitV2Router()
v1DocRouter := route.InitV1DocRouter(_docHTMLV1, _docYAMLV1)
v2DocRouter := route.InitV2DocRouter(_docHTML, _docYAML)

mux := &util_http.HandlerMultiplexer{
HandlerMap: map[string]http.Handler{
"v1": v1Router,
"v2": v2Router,
"doc": v2DocRouter,
"v1": v1Router,
"v2": v2Router,
"v1doc": v1DocRouter,
"doc": v2DocRouter,
},
}

Expand Down
24 changes: 23 additions & 1 deletion route/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import (
echo_middleware "github.com/labstack/echo/v4/middleware"
)

func InitV1Router() http.Handler{
const (
V1APIPath = "/v1/app_management"
V1DocPath = "/v1doc" + V1APIPath
)

func InitV1Router() http.Handler {
e := echo.New()
e.Use((echo_middleware.CORSWithConfig(echo_middleware.CORSConfig{
AllowOrigins: []string{"*"},
Expand Down Expand Up @@ -83,3 +88,20 @@ func InitV1Router() http.Handler{

return e
}

func InitV1DocRouter(docHTML string, docYAML string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == V1DocPath {
if _, err := w.Write([]byte(docHTML)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
return
}

if r.URL.Path == V1DocPath+"/openapi_v1.yaml" {
if _, err := w.Write([]byte(docYAML)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
})
}

0 comments on commit 2d41560

Please sign in to comment.