Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Add edit support
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlin Ralchev committed Nov 25, 2019
1 parent c43acdf commit 82b3c76
Show file tree
Hide file tree
Showing 21 changed files with 345,346 additions and 264 deletions.
84 changes: 84 additions & 0 deletions cmd/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cmd

import (
"io"
"net/http"
"os"

"github.com/go-chi/chi"
"github.com/phogolabs/cli"
"github.com/phogolabs/log"
"github.com/phogolabs/log/handler/console"
"github.com/phogolabs/parcello"
"github.com/phogolabs/rest/middleware"
)

// OpenAPIEditor provides a subcommands to edit OpenAPI specification in the browser
type OpenAPIEditor struct{}

// CreateCommand creates a cli.Command that can be used by cli.App.
func (m *OpenAPIEditor) CreateCommand() *cli.Command {
return &cli.Command{
Name: "edit",
Usage: "Edit an OpenAPI specification in the browser",
Description: "Edit an OpenAPI specification in the browser",
Before: m.before,
Action: m.edit,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen-addr",
Usage: "address on which the http server is listening on",
Value: "localhost:8080",
},
&cli.StringFlag{
Name: "file-path, f",
Usage: "path to the open api specification",
Value: "./swagger.yaml",
},
},
}
}

func (m *OpenAPIEditor) before(ctx *cli.Context) error {
log.SetHandler(console.New(ctx.Writer))
return nil
}

func (m *OpenAPIEditor) edit(ctx *cli.Context) error {
router := chi.NewRouter()

router.Use(middleware.StripSlashes)
router.Use(middleware.RealIP)
router.Use(middleware.Recoverer)
router.Use(middleware.NoCache)
router.Use(middleware.Logger)

router.Mount("/", http.FileServer(parcello.ManagerAt("editor")))
// router.Mount("/", http.FileServer(http.Dir("./template/editor")))
router.Mount("/swagger.spec", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, ctx.String("file-path"))
case "POST":
SaveFile(w, r, ctx.String("file-path"))
}
}))

log.Infof("http server is listening on http://%v", ctx.String("listen-addr"))

return http.ListenAndServe(ctx.String("listen-addr"), router)
}

// SaveFile saves the file
func SaveFile(w http.ResponseWriter, r *http.Request, path string) {
spec, err := os.Create(path)
if err != nil {
middleware.GetLogger(r).WithError(err).Error("failed to save the spec")
return
}
defer spec.Close()

if _, err := io.Copy(spec, r.Body); err != nil {
middleware.GetLogger(r).WithError(err).Error("failed to save the spec")
}
}
7 changes: 6 additions & 1 deletion cmd/stride/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/phogolabs/cli"
"github.com/phogolabs/log"
"github.com/phogolabs/stride/cmd"
_ "github.com/phogolabs/stride/template"
)
Expand All @@ -12,11 +13,13 @@ func main() {
var (
generator = &cmd.OpenAPIGenerator{}
viewer = &cmd.OpenAPIViewer{}
editor = &cmd.OpenAPIEditor{}
)

commands := []*cli.Command{
generator.CreateCommand(),
viewer.CreateCommand(),
editor.CreateCommand(),
}

app := &cli.App{
Expand All @@ -30,5 +33,7 @@ func main() {
Commands: commands,
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
log.WithError(err).Fatal("execution failed")
}
}
1 change: 1 addition & 0 deletions cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (m *OpenAPIViewer) view(ctx *cli.Context) error {
router.Use(middleware.LiveReloader)

router.Mount("/", http.FileServer(parcello.ManagerAt("viewer")))
// router.Mount("/", http.FileServer(http.Dir("./template/viewer")))
router.Mount("/swagger.spec", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, ctx.String("file-path"))
}))
Expand Down
5 changes: 4 additions & 1 deletion codegen/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,15 @@ func (r *Resolver) name(parent string, schemaRef *openapi3.SchemaRef) string {
}

key = strings.Join(parts, "-")
return key
}

func camelize(key string) string {
key = inflect.Camelize(key)
key = strings.ReplaceAll(key, "Xml", "XML")
key = strings.ReplaceAll(key, "Json", "JSON")
key = strings.ReplaceAll(key, "Id", "ID")
key = strings.ReplaceAll(key, "Ok", "OK")

return key
}

Expand Down
3 changes: 3 additions & 0 deletions template/editor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module, `swagger-editor-dist`, exposes Swagger-Editor's entire dist folder as a dependency-free npm module.

Use `swagger-editor` instead, if you'd like to have npm install dependencies for you.
Binary file added template/editor/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added template/editor/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 104 additions & 0 deletions template/editor/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!DOCTYPE html>
<!-- HTML for static distribution bundle build -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger Editor</title>
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<link href="./swagger-editor.css" rel="stylesheet">
<style>
* {
box-sizing: border-box;
}

body {
font-family: Roboto, sans-serif;
font-size: 9px;
line-height: 1.42857143;
color: #444;
margin: 0px;
}

#swagger-editor {
font-size: 1.3em;
}

.container {
height: 100%;
max-width: 880px;
margin-left: auto;
margin-right: auto;
}

#editor-wrapper {
height: 100%;
border: 1em solid #000;
border: none;
}

.Pane2 {
overflow-y: scroll;
}

#swagger-editor #ace-editor.ace_editor {
height: calc(100vh) !important;
}

#swagger-editor .SplitPane.vertical {
height: calc(100%) !important;
}
</style>
</head>

<body>
<div id="swagger-editor"></div>
<script src="./swagger-editor-bundle.js"> </script>
<script src="./swagger-editor-standalone-preset.js"> </script>
<script>
const addr = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port + "/swagger.spec";

function post(url = '', data = '') {
return fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-yaml',
},
redirect: 'follow',
referrer: 'no-referrer',
body: data,
});
}

const UpdatePlugin = function(system) {
return {
statePlugins: {
spec: {
wrapActions: {
updateSpec: (oriAction, system) => (str) => {
post(addr, str);
return oriAction(str);
}
}
}
}
}
}

function liveReload() {
const editor = SwaggerEditorBundle({
url: addr,
dom_id: '#swagger-editor',
plugins: SwaggerEditorStandalonePreset().slice(4).concat(UpdatePlugin)
})

window.editor = editor
}

window.onload = liveReload;
</script>
</body>
</html>
72 changes: 72 additions & 0 deletions template/editor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"_from": "swagger-editor-dist",
"_id": "swagger-editor-dist@3.6.28",
"_inBundle": false,
"_integrity": "sha512-xVqL4ON6Ygw7ceNTplTITVcuociemqBg7ZcNXa02BrGY/yf5cH7lRSNecFUpK1ZBhJ04GzFhEGEvXty9eZnhlw==",
"_location": "/swagger-editor-dist",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "swagger-editor-dist",
"name": "swagger-editor-dist",
"escapedName": "swagger-editor-dist",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/swagger-editor-dist/-/swagger-editor-dist-3.6.28.tgz",
"_shasum": "ee0a55a2988a366857c46f27f676c62830d7beef",
"_spec": "swagger-editor-dist",
"_where": "/Users/ralch/go/src/github.com/phogolabs/stride/template",
"bugs": {
"url": "https://github.com/swagger-api/swagger-editor/issues"
},
"bundleDependencies": false,
"contributors": [
{
"url": "in alphabetical order"
},
{
"name": "Anna Bodnia",
"email": "anna.bodnia@gmail.com"
},
{
"name": "Buu Nguyen",
"email": "buunguyen@gmail.com"
},
{
"name": "Josh Ponelat",
"email": "jponelat@gmail.com"
},
{
"name": "Kyle Shockey",
"email": "kyleshockey1@gmail.com"
},
{
"name": "Robert Barnwell",
"email": "robert@robertismy.name"
},
{
"name": "Sahar Jafari",
"email": "shr.jafari@gmail.com"
}
],
"dependencies": {},
"deprecated": false,
"description": "This module, `swagger-editor-dist`, exposes Swagger-Editor's entire dist folder as a dependency-free npm module.",
"devDependencies": {},
"homepage": "https://github.com/swagger-api/swagger-editor#readme",
"license": "Apache-2.0",
"main": "swagger-editor.js",
"name": "swagger-editor-dist",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/swagger-api/swagger-editor.git"
},
"version": "3.6.28"
}
123 changes: 123 additions & 0 deletions template/editor/swagger-editor-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions template/editor/swagger-editor-bundle.js.map

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions template/editor/swagger-editor-standalone-preset.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions template/editor/swagger-editor-standalone-preset.js.map

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions template/editor/swagger-editor.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions template/editor/swagger-editor.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions template/editor/swagger-editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions template/editor/swagger-editor.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions template/editor/validation.worker.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions template/editor/validation.worker.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 82b3c76

Please sign in to comment.