Deliverable for D3.2

server.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package metadata
  2. import (
  3. "log"
  4. "net/http"
  5. "gogs.dyne.org/DECODE/decode-prototype-da/metadata/api"
  6. restful "github.com/emicklei/go-restful"
  7. restfulspec "github.com/emicklei/go-restful-openapi"
  8. )
  9. type Options struct {
  10. Binding string
  11. SwaggerUIPath string
  12. WebServicesURL string
  13. AssetsPath string
  14. }
  15. func Serve(options Options) error {
  16. store := api.NewMetadataStore()
  17. restful.DefaultContainer.Add(api.NewCatalogService(store).WebService())
  18. config := restfulspec.Config{
  19. WebServices: restful.RegisteredWebServices(),
  20. WebServicesURL: options.WebServicesURL,
  21. APIPath: "/apidocs.json",
  22. }
  23. restful.DefaultContainer.Add(restfulspec.NewOpenAPIService(config))
  24. // Add container filter to enable CORS
  25. cors := restful.CrossOriginResourceSharing{
  26. AllowedHeaders: []string{"Content-Type", "Accept"},
  27. AllowedMethods: []string{"GET", "POST"},
  28. }
  29. // install the cors filter
  30. restful.DefaultContainer.Filter(cors.Filter)
  31. // Optionally, you can install the Swagger Service which provides a nice Web UI on your REST API
  32. // You need to download the Swagger HTML5 assets and change the FilePath location in the config below.
  33. // Open http://localhost:8080/apidocs/?url=http://localhost:8080/apidocs.json
  34. http.Handle("/apidocs/", http.StripPrefix("/apidocs/", http.FileServer(http.Dir(options.SwaggerUIPath))))
  35. // Serve the ui
  36. http.Handle("/", http.FileServer(http.Dir(options.AssetsPath)))
  37. log.Printf("start listening on %s", options.Binding)
  38. return http.ListenAndServe(options.Binding, nil)
  39. }