Exemple #1
0
func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewTodoListAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Simple To Do List API`
	parser.LongDescription = swaggerSpec.Spec().Info.Description

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
Exemple #2
0
func main() {
	swaggerSpec, err := spec.New(swaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	port := os.Getenv("PORT")
	if port == "" {
		port = "0"
	}

	host := os.Getenv("HOST")
	if host == "" {
		host = "localhost"
	}

	api := operations.NewEventListAPI(swaggerSpec)
	configureAPI(api)

	listener, err := net.Listen("tcp", host+":"+port)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("serving event list at http://%s\n", listener.Addr())

	if err := http.Serve(listener, api.Serve()); err != nil {
		log.Fatalln(err)
	}
}
Exemple #3
0
func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewPetstoreAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Swagger Petstore`
	parser.LongDescription = `This is a sample server Petstore server.

[Learn about Swagger](http://swagger.wordnik.com) or join the IRC channel '#swagger' on irc.freenode.net.

For this sample, you can use the api key 'special-key' to test the authorization filters
`

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
Exemple #4
0
func (s *Schema) Generate(indent bool, valid bool) []byte {
	var (
		j   []byte
		err error
	)
	if indent {
		j, err = json.MarshalIndent(s, "", "    ")
	} else {
		j, err = json.Marshal(s)
	}
	if err != nil {
		s.AddError(err)
		return nil
	}
	if valid {
		doc, err := spec.New(j, "")
		if err != nil {
			s.AddError(err)
			return nil
		}
		result := validate.Spec(doc, strfmt.Default)
		if result != nil {
			for _, desc := range result.(*swaggererrors.CompositeError).Errors {
				s.AddError(fmt.Errorf("The swagger spec is invalid against swagger specification %s. %s", doc.Version(), desc.Error()))
			}
			return nil
		}
	}
	return j
}
Exemple #5
0
func main() {
	swaggerSpec, err := spec.New(SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	parser := flags.NewParser(&opts, flags.Default)
	parser.ShortDescription = swaggerSpec.Spec().Info.Title
	parser.LongDescription = swaggerSpec.Spec().Info.Description

	if _, err := parser.Parse(); err != nil {
		os.Exit(1)
	}

	api := operations.NewTodoListAPI(swaggerSpec)
	handler := configureAPI(api)

	httpServer := &graceful.Server{Server: new(http.Server)}
	httpServer.Handler = handler

	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.Host, opts.Port))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("serving todo list at http://%s\n", listener.Addr())
	if err := httpServer.Serve(tcpKeepAliveListener{listener.(*net.TCPListener)}); err != nil {
		log.Fatalln(err)
	}

}
Exemple #6
0
// validateSwagger validates that the given swagger object represents a valid Swagger spec.
func validateSwagger(swagger *genswagger.Swagger) {
	b, err := json.Marshal(swagger)
	Ω(err).ShouldNot(HaveOccurred())
	doc, err := spec.New(b, "")
	Ω(err).ShouldNot(HaveOccurred())
	Ω(doc).ShouldNot(BeNil())
}
func SetupConfig() {
	var conf goStashRestClientConfig
	ParseJsonFileStripComments("./config.json", &conf)
	validateRequiredField("host", &conf.Host)
	validateRequiredField("username", &conf.Username)
	if &conf.Password == nil || len(conf.Password) == 0 {
		fmt.Printf("Enter your password for %s: ", conf.Username)
		bytePassword, err := terminal.ReadPassword(0)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Println()
		conf.Password = string(bytePassword)
	}
	validateRequiredField("password", &conf.Password)

	doc, err := spec.New(apiclient.SwaggerJSON, "")
	if err != nil {
		panic(err)
	}

	transport := httptransport.New(doc)
	transport.Host = conf.Host
	fmt.Println("using host", conf.Host)

	// Helpful to debug
	//	transport.Debug = true

	// Assumes basic auth. TODO enable the config.json to take different mechanisms, OR integrate with swagger spec file what it says is supported.
	transport.DefaultAuthentication = httptransport.BasicAuth(conf.Username, conf.Password)
	apiclient.Default.SetTransport(transport)
}
Exemple #8
0
func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewTaskTrackerAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Issue Tracker API`
	parser.LongDescription = `This application implements a very simple issue tracker.
It's implemented as an API which is described by this swagger spec document.

The go-swagger project uses this specification to test the code generation.
This document contains all possible values for a swagger definition.
This means that it exercises the framework relatively well.
`

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
Exemple #9
0
func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewTodoListAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `A To Do list application`
	parser.LongDescription = `The product of a tutorial on goswagger.io`

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		log.Fatalln(err)
	}

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
Exemple #10
0
func main() {
	swaggerSpec, err := spec.New(SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	parser := flags.NewParser(&opts, flags.Default)
	parser.ShortDescription = swaggerSpec.Spec().Info.Title
	parser.LongDescription = swaggerSpec.Spec().Info.Description

	if _, err := parser.Parse(); err != nil {
		os.Exit(1)
	}

	api := operations.NewSimpleToDoListAPI(swaggerSpec)
	handler := configureAPI(api)

	httpServer := &graceful.Server{Server: new(http.Server)}
	httpServer.Handler = handler

	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.Host, opts.Port))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("serving simple to do list at http://%s\n", listener.Addr())
	go func() {
		if err := httpServer.Serve(tcpKeepAliveListener{listener.(*net.TCPListener)}); err != nil {
			log.Fatalln(err)
		}
	}()

	httpsServer := &graceful.Server{Server: new(http.Server)}
	httpsServer.Handler = handler
	httpsServer.TLSConfig = new(tls.Config)
	httpsServer.TLSConfig.NextProtos = []string{"http/1.1"}
	// https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet#Rule_-_Only_Support_Strong_Protocols
	httpsServer.TLSConfig.MinVersion = tls.VersionTLS11
	httpsServer.TLSConfig.Certificates = make([]tls.Certificate, 1)
	httpsServer.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(string(opts.TLSCertificate), string(opts.TLSCertificateKey))
	if err != nil {
		log.Fatal(err)
	}

	if opts.TLSHost == "" {
		opts.TLSHost = opts.Host
	}
	tlsListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.TLSHost, opts.TLSPort))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("serving simple to do list at https://%s\n", tlsListener.Addr())

	wrapped := tls.NewListener(tcpKeepAliveListener{tlsListener.(*net.TCPListener)}, httpsServer.TLSConfig)
	if err := httpsServer.Serve(wrapped); err != nil {
		log.Fatalln(err)
	}
}
Exemple #11
0
func main() {
	swaggerSpec, err := spec.New(SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	parser := flags.NewParser(&opts, flags.Default)
	parser.ShortDescription = swaggerSpec.Spec().Info.Title
	parser.LongDescription = swaggerSpec.Spec().Info.Description

	api := operations.NewGophergalaLearningResourcesAPI(swaggerSpec)
	handler = configureAPI(api)

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		os.Exit(1)
	}

	httpsServer := &graceful.Server{Server: new(http.Server)}
	// Normal resources
	chttp.Handle("/", http.FileServer(http.Dir("public")))

	mux := http.NewServeMux()
	mux.HandleFunc("/", ApiHandler)
	httpsServer.Handler = mux
	httpsServer.TLSConfig = new(tls.Config)
	httpsServer.TLSConfig.NextProtos = []string{"http/1.1"}
	// https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet#Rule_-_Only_Support_Strong_Protocols
	httpsServer.TLSConfig.MinVersion = tls.VersionTLS11
	httpsServer.TLSConfig.Certificates = make([]tls.Certificate, 1)
	httpsServer.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(string(opts.TLSCertificate), string(opts.TLSCertificateKey))
	if err != nil {
		api.ServerShutdown()
		log.Fatal(err)
	}

	tlsListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.TLSHost, opts.TLSPort))
	if err != nil {
		api.ServerShutdown()
		log.Fatalln(err)
	}

	log.Printf("serving gophergala learning resources at https://%s:%d\n", opts.TLSHost, opts.TLSPort)

	wrapped := tls.NewListener(tcpKeepAliveListener{tlsListener.(*net.TCPListener)}, httpsServer.TLSConfig)
	if err := httpsServer.Serve(wrapped); err != nil {
		api.ServerShutdown()
		log.Printf("shutting down gophergala learning resources at https://%s:%d\n", opts.TLSHost, opts.TLSPort)
		log.Fatalln(err)
	}
	go func() {

		<-httpsServer.StopChan()
		api.ServerShutdown()
	}()
}
Exemple #12
0
// validateSwagger validates that the given swagger object represents a valid Swagger spec.
func validateSwagger(swagger *genswagger.Swagger) {
	b, err := json.Marshal(swagger)
	Ω(err).ShouldNot(HaveOccurred())
	doc, err := spec.New(b, "")
	Ω(err).ShouldNot(HaveOccurred())
	err = validate.Spec(doc, strfmt.NewFormats())
	Ω(err).ShouldNot(HaveOccurred())
}
Exemple #13
0
// validateSwagger validates that the given swagger object represents a valid Swagger spec.
func validateSwagger(swagger *genswagger.Swagger) {
	b, err := json.Marshal(swagger)
	Ω(err).ShouldNot(HaveOccurred())
	doc, err := spec.New(b, "")
	Ω(err).ShouldNot(HaveOccurred())
	Ω(doc).ShouldNot(BeNil())
	// TBD calling the swagger validator below causes Travis to hang...
	// err = validate.Spec(doc, strfmt.NewFormats())
	// Ω(err).ShouldNot(HaveOccurred())
}
Exemple #14
0
func main() {
	swaggerSpec, err := spec.New(restapi.SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	api := operations.NewPortLayerAPI(swaggerSpec)
	server := restapi.NewServer(api)
	defer server.Shutdown()

	parser := flags.NewParser(server, flags.Default)
	parser.ShortDescription = `Port Layer API`
	parser.LongDescription = `Port Layer API`

	server.ConfigureFlags()
	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		os.Exit(1)
	}

	server.ConfigureAPI()

	// BEGIN
	// Set the Interface name to instruct listeners to bind on this interface
	options.Interface = "bridge"
	// Start the DNS Server
	dnsserver := dns.NewServer(options)
	if dnsserver != nil {
		dnsserver.Start()
	}

	// handle the signals and gracefully shutdown the server
	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		<-sig

		dnsserver.Stop()

		server.Stop()
	}()

	go func() {
		dnsserver.Wait()
	}()
	// END

	if err := server.Serve(); err != nil {
		log.Fatalln(err)
	}
}
// NewHTTPClient creates a new simple to do list HTTP client.
func NewHTTPClient(formats strfmt.Registry) *SimpleToDoList {
	swaggerSpec, err := spec.New(SwaggerJSON, "")
	if err != nil {
		// the swagger spec is valid because it was used to generated this code.
		panic(err)
	}
	if formats == nil {
		formats = strfmt.Default
	}
	return New(httptransport.New(swaggerSpec), formats)
}
Exemple #16
0
// NewPetstore creates a new petstore api handler
func NewPetstore() (http.Handler, error) {
	spec, err := spec.New(json.RawMessage([]byte(swaggerJSON)), "")
	if err != nil {
		return nil, err
	}
	api := untyped.NewAPI(spec)

	api.RegisterOperation("getAllPets", getAllPets)
	api.RegisterOperation("createPet", createPet)
	api.RegisterOperation("deletePet", deletePet)
	api.RegisterOperation("getPetById", getPetByID)

	return middleware.Serve(spec, api), nil
}
Exemple #17
0
func main() {
	swaggerSpec, err := spec.New(SwaggerJSON, "")
	if err != nil {
		log.Fatalln(err)
	}

	parser := flags.NewParser(&opts, flags.Default)
	parser.ShortDescription = swaggerSpec.Spec().Info.Title
	parser.LongDescription = swaggerSpec.Spec().Info.Description

	api := operations.NewTodoListAPI(swaggerSpec)
	handler := configureAPI(api)

	for _, optsGroup := range api.CommandLineOptionsGroups {
		parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)
	}

	if _, err := parser.Parse(); err != nil {
		os.Exit(1)
	}

	httpServer := &graceful.Server{Server: new(http.Server)}
	httpServer.Handler = handler

	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.Host, opts.Port))
	if err != nil {
		api.ServerShutdown()
		log.Fatalln(err)
	}

	fmt.Printf("serving todo list at http://%s\n", listener.Addr())
	if err := httpServer.Serve(tcpKeepAliveListener{listener.(*net.TCPListener)}); err != nil {
		api.ServerShutdown()
		log.Fatalln(err)
	}

	go func() {

		<-httpServer.StopChan()

		api.ServerShutdown()
	}()
}