Beispiel #1
0
func PostgresConnectionString(props properties.Properties) string {
	parts := []string{"sslmode=disable"}
	if s := props.Get("postgres.host"); s != "" {
		parts = append(parts, "host="+s)
	}
	if s := props.Get("postgres.port"); s != "" {
		parts = append(parts, "port="+s)
	}
	if s := props.Get("postgres.username"); s != "" {
		parts = append(parts, "user="******"postgres.password"); s != "" {
		parts = append(parts, "password="******"postgres.database"); s != "" {
		parts = append(parts, "dbname="+s)
	}
	return strings.Join(parts, " ")
}
Beispiel #2
0
// NewServer construct rest server listening specific host and port and routes requests by passed routes
func NewServer(props properties.Properties, routes PathHandlers) error {
	bind := props.Get("rest.address") + ":" + props.Get("rest.port")

	log.Println("start rest server on", bind)
	listener, err := net.Listen("tcp", bind)
	if err != nil {
		return err
	}

	s := &server{
		listener: listener,
		m:        &sync.Mutex{},
		alive:    true,
		wg:       &sync.WaitGroup{},
	}

	kit.SafeGo(func() error {
		router := mux.NewRouter()
		for path := range routes {
			mh := handlers.MethodHandler{}
			for method := range routes[path] {
				log.Printf("setup rest handler %v %v\n", method, path)
				mh[method] = s.wrapHandler(routes[path][method])
			}
			router.Path(path).Handler(mh)
		}

		handler := http.Handler(router)

		compression, err := strconv.ParseBool(props.Get("rest.compression"))
		if err == nil && compression {
			log.Println("enable compression for rest server")
			handler = handlers.CompressHandler(handler)
		} else {
			log.Println("no compression for rest server")
		}

		log.Println("rest server serves")
		err = http.Serve(listener, handler)
		if err != nil {
			return err
		}

		return nil
	})

	kit.SafeGo(func() error {
		<-registry.DoneChannel()
		s.Stop()
		return nil
	})

	return nil
}