コード例 #1
0
func RunGoHttpApiServerCmd() {
	command := &GoHttpApiServerCommand{}
	flag.StringVar(&command.http, "http", ":18080", "listen http port of the server")
	flag.StringVar(&command.https, "https", "", "listen https port of the server")
	flag.BoolVar(&command.randPort, "randPort", false, "if can not listen on default port ,will listen on random port")
	flag.Parse()
	if command.https != "" {
		command.isHttps = true
		command.tcpListenAddr = command.https
	} else {
		command.tcpListenAddr = command.http
	}
	jsonHttpHandler := &JsonHttpHandler{
		ApiManager: DefaultApiManager,
		SessionStoreManager: &sessionStore.Manager{
			Provider: sessionStore.NewMemoryProvider(),
			//Provider: memcacheProvider.New(kmgConfig.DefaultParameter().MemcacheHostList...),
		},
	}
	http.Handle("/api", &HttpApiFilterManager{
		Filters: []HttpApiFilter{
			jsonHttpHandler.Filter,
		},
	})
	http.Handle("/api.deflate", &HttpApiFilterManager{
		Filters: []HttpApiFilter{
			HttpApiDeflateCompressFilter,
			jsonHttpHandler.Filter,
		},
	})
	for _, handlerConfig := range AdditionHttpHandler {
		http.Handle(handlerConfig.Path, handlerConfig.Handler)
	}
	l, err := command.listen()
	kmgConsole.ExitOnErr(err)
	fmt.Printf("Listen on %s\n", l.Addr().String())
	if command.isHttps {
		tlsConfig, err := kmgCrypto.CreateTlsConfig()
		if err != nil {
			kmgConsole.ExitOnErr(fmt.Errorf("fail at kmgTls.CreateTlsConfig,error:%s", err.Error()))
		}
		l = tls.NewListener(l, tlsConfig)
	}
	err = http.Serve(l, nil)
	kmgConsole.ExitOnErr(err)
}
コード例 #2
0
ファイル: GoHttpApiServerCommand.go プロジェクト: iizotop/kmg
func (command *GoHttpApiServerCommand) Execute(context *console.Context) error {
	if command.https != "" {
		command.isHttps = true
		command.tcpListenAddr = command.https
	} else {
		command.tcpListenAddr = command.http
	}
	jsonHttpHandler := &JsonHttpHandler{
		ApiManager: DefaultApiManager,
		SessionStoreManager: &sessionStore.Manager{
			Provider: memcacheProvider.New(defaultParameter.Parameter.MemcacheHostList...),
		},
	}
	http.Handle("/api", &HttpApiFilterManager{
		Filters: []HttpApiFilter{
			jsonHttpHandler.Filter,
		},
	})
	http.Handle("/api.deflate", &HttpApiFilterManager{
		Filters: []HttpApiFilter{
			HttpApiDeflateCompressFilter,
			jsonHttpHandler.Filter,
		},
	})
	for _, handlerConfig := range AdditionHttpHandler {
		http.Handle(handlerConfig.Path, handlerConfig.Handler)
	}
	l, err := command.listen()
	if err != nil {
		return err
	}
	fmt.Fprintf(context.Stdout, "Listen on %s\n", l.Addr().String())
	if command.isHttps {
		tlsConfig, err := kmgCrypto.CreateTlsConfig()
		if err != nil {
			return fmt.Errorf("fail at kmgTls.CreateTlsConfig,error:%s", err.Error())
		}
		l = tls.NewListener(l, tlsConfig)
	}
	return http.Serve(l, nil)
}