Skip to content

Commit

Permalink
Merge pull request #2 from swaggest/v3-dev
Browse files Browse the repository at this point in the history
Swagger UI v3
  • Loading branch information
vearutop authored Jul 31, 2018
2 parents 919188f + 5a0515a commit 7d2d94c
Show file tree
Hide file tree
Showing 73 changed files with 1,533 additions and 1,382 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Swagger UI

[![GoDoc](https://godoc.org/github.com/lazada/swgui?status.svg)](https://godoc.org/github.com/lazada/swgui)
[![GoDoc](https://godoc.org/github.com/swaggest/swgui?status.svg)](https://godoc.org/github.com/swaggest/swgui)

Package `swgui` (Swagger UI) provide a HTTP handler to serve Swagger UI.
All assets are embedded in GO source code, so just build and run.
Expand All @@ -11,13 +11,13 @@ All assets are embedded in GO source code, so just build and run.
package main

import (
"http"
"net/http"

"github.com/lazada/swgui"
"github.com/swaggest/swgui/v3"
)

func main() {
http.Handle("/", swgui.NewHandler("Page title", "path/to/swagger.json", "/"))
http.Handle("/", v3.NewHandler("My API", "/swagger.json", "/"))
http.ListenAndServe(":8080", nil)
}
```
Expand All @@ -26,7 +26,7 @@ func main() {

Install `swgui-server`

go get github.com/lazada/swgui/...
go get github.com/swaggest/swgui/...

Start server

Expand Down
1,293 changes: 0 additions & 1,293 deletions bindata_assetfs.go

This file was deleted.

12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package swgui

// Config is used for Swagger UI handler configuration
type Config struct {
Title string `json:"title"` // title of index file
SwaggerJSON string `json:"swaggerJsonUrl"` // url to swagger.json document specification
BasePath string `json:"basePath"` // base url to docs

ShowTopBar bool `json:"showTopBar"` // Show navigation top bar, hidden by default
JsonEditor bool `json:"jsonEditor"` // Enable visual json editor support (experimental, can fail with complex schemas)
PreAuthorizeApiKey map[string]string `json:"preAuthorizeApiKey"` // Map of security name to key value
}
19 changes: 3 additions & 16 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
// Package swgui provide a http FileSystem handler for swagger UI interface
//
// File `bindata_assetfs.go` is generated by go-bindata-assetfs (https://github.com/elazarl/go-bindata-assetfs) tool,
// which help to embed all static files into a go source file.
//
// How to generate file:
//
// NOTE:
// * File `bindata_assetfs.go` ONLY need to re-generate file again when we update swagger-ui (swagger/ui/static)
// How to generate asset files:
//
// 1. Install tools:
//
// go get -u github.com/jteeuwen/go-bindata/...
// go get -u github.com/elazarl/go-bindata-assetfs/...
// go get -u github.com/shurcooL/vfsgen
//
// 2. Generate file:
//
// At root folder, run command:
//
// go-bindata-assetfs -pkg=swgui static/...
// gofmt -w bindata_assetfs.go
//
// Or just run: `go generate`
//
// Enjoy it ;)
// go generate
//
package swgui
4 changes: 2 additions & 2 deletions generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swgui

//go:generate go-bindata-assetfs -pkg $GOPACKAGE static/...
//go:generate gofmt -w bindata_assetfs.go
//go:generate go run ./v2/gen/gen.go
//go:generate go run ./v3/gen/gen.go
6 changes: 0 additions & 6 deletions glide.yaml

This file was deleted.

53 changes: 0 additions & 53 deletions handler.go

This file was deleted.

20 changes: 15 additions & 5 deletions swgui-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ import (
"fmt"
"net/http"

"github.com/lazada/swgui"
"github.com/swaggest/swgui/v2"
"github.com/swaggest/swgui/v3"
)

var (
host string
port uint
title string
host string
port uint
title string
version = "v3"
)

func init() {
flag.StringVar(&host, "host", "", "Host")
flag.UintVar(&port, "port", 8080, "Port")
flag.StringVar(&title, "title", "API Document", "Page title")
flag.StringVar(&version, "version", "v3", "Swagger UI version (v2/v3)")

flag.Parse()
}

func main() {
http.Handle("/", swgui.NewHandler(title, "/swagger.json", "/"))
var h http.Handler
if version != "v3" {
h = v2.NewHandler(title, "/swagger.json", "/")
} else {
h = v3.NewHandler(title, "/swagger.json", "/")
}
http.Handle("/", h)
fmt.Printf("Listening at %s:%d\n", host, port)
http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)
}
20 changes: 20 additions & 0 deletions v2/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"log"
"net/http"

"github.com/shurcooL/vfsgen"
)

func main() {
var fs http.FileSystem = http.Dir("./v2/static")

err := vfsgen.Generate(fs, vfsgen.Options{
PackageName: "v2",
Filename: "v2/static.go",
})
if err != nil {
log.Fatalln(err)
}
}
59 changes: 59 additions & 0 deletions v2/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package v2

import (
"encoding/json"
"html/template"
"net/http"

"github.com/swaggest/swgui"
)

var staticServer = http.FileServer(assets)

// Handler handle swagger UI request
type Handler struct {
swgui.Config

ConfigJson template.JS

tpl *template.Template
staticServer http.Handler
}

// NewHandler returns a HTTP handler for swagger UI
func NewHandler(title, swaggerJSONPath string, basePath string) *Handler {
h := &Handler{}
h.Title = title
h.SwaggerJSON = swaggerJSONPath
h.BasePath = basePath

j, _ := json.Marshal(h.Config)
h.ConfigJson = template.JS(j)
h.tpl, _ = template.New("index").Parse(indexTpl)
h.staticServer = http.StripPrefix(basePath, staticServer)
return h
}

// NewHandlerWithConfig returns a HTTP handler for swagger UI
func NewHandlerWithConfig(config swgui.Config) *Handler {
h := &Handler{
Config: config,
}
j, _ := json.Marshal(h.Config)
h.ConfigJson = template.JS(j)
h.tpl, _ = template.New("index").Parse(indexTpl)
h.staticServer = http.StripPrefix(h.BasePath, staticServer)
return h
}

// ServeHTTP implement http.Handler interface, to handle swagger UI request
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != h.BasePath {
h.staticServer.ServeHTTP(w, r)
return
}

if err := h.tpl.Execute(w, h); err != nil {
http.NotFound(w, r)
}
}
5 changes: 3 additions & 2 deletions index.tpl.go → v2/index.tpl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swgui
package v2

var indexTpl = `
<!DOCTYPE html>
Expand Down Expand Up @@ -41,7 +41,8 @@ var indexTpl = `
window.location = "http://" + window.location.host + window.location.pathname;
}
$(function () {
var jsonEditorEnabled = false;
var cfg = {{ .ConfigJson }};
var jsonEditorEnabled = false;
{{if .JsonEditor }}
if (window.localStorage && window.localStorage.getItem('jsonEditorEnabled')) {
jsonEditorEnabled = true;
Expand Down
609 changes: 609 additions & 0 deletions v2/static.go

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 7d2d94c

Please sign in to comment.