Skip to content

mwmahlberg/swagger-ui

Repository files navigation

Embeddable swagger-ui for your Go program

Go Reference Quality Gate Status

This Go module allows you to conveniently serve a swagger UI embedded into your server.

Example

package main

import (
  _ "embed"
  "flag"
  "log"
  "net/http"

  swaggerui "github.com/mwmahlberg/swagger-ui"
)

//go:embed petstore.yaml
var petStore []byte

var bind string

func init() {
  flag.StringVar(&bind, "bind", ":8080", "address to bind to")
}

func main() {

  flag.Parse()

  ui, err := swaggerui.New(swaggerui.Spec(swaggerui.DefaultSpecfileName, petStore))
  if err != nil {
    log.Fatalln(err)
  }

  mux := http.NewServeMux()
  mux.Handle("/api-docs/", http.StripPrefix("/api-docs/", ui))

  mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("dummy api"))
  })

  http.ListenAndServe(bind, mux)
}

Links