示例#1
0
文件: main.go 项目: jak-atx/vic
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)
	}
}
示例#2
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)
	defer api.ServerShutdown()

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

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

	domainSocket := &graceful.Server{Server: new(http.Server)}
	domainSocket.Handler = handler
	domSockListener, err := net.Listen("unix", string(opts.SocketPath))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("serving todo list at unix://%s\n", opts.SocketPath)
	go func() {
		if err := domainSocket.Serve(domSockListener); err != nil {
			log.Fatalln(err)
		}
	}()

	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())
	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 todo list at https://%s\n", tlsListener.Addr())
	wrapped := tls.NewListener(tcpKeepAliveListener{tlsListener.(*net.TCPListener)}, httpsServer.TLSConfig)
	if err := httpsServer.Serve(wrapped); err != nil {
		api.ServerShutdown()
		log.Fatalln(err)
	}

}