Beispiel #1
0
func main() {
	consumer := consumer.New(configure())

	for event := range consumer.Start() {
		log.Debug("event", log.Data{"event": event})

		err := consumer.Commit(event)
		if err != nil {
			log.Error(err, nil)
		}
	}
}
Beispiel #2
0
// Register creates routes for each static resource
func Register(config render.Config, r *pat.Router) {
	log.Debug("registering not found handler for static package", nil)
	r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		render.HTML(w, http.StatusNotFound, "error", render.DefaultVars(req, map[string]interface{}{"error": "Page not found"}))
	})

	log.Debug("registering static content handlers for static package", nil)
	for _, file := range config.AssetNames()() {
		if strings.HasPrefix(file, "static/") {
			path := strings.TrimPrefix(file, "static")
			log.Trace("registering handler for static asset", log.Data{"path": path})

			var mimeType string
			switch {
			case strings.HasSuffix(path, ".css"):
				mimeType = "text/css"
			case strings.HasSuffix(path, ".js"):
				mimeType = "application/javascript"
			default:
				mimeType = mime.TypeByExtension(path)
			}

			log.Trace("using mime type", log.Data{"type": mimeType})

			r.Path(path).Methods("GET").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
				if b, err := config.Asset()("static" + path); err == nil {
					w.Header().Set("Content-Type", mimeType)
					w.Header().Set("Cache-control", "public, max-age=259200")
					w.WriteHeader(200)
					w.Write(b)
					return
				}
				// This should never happen!
				log.ErrorR(req, errors.New("it happened ¯\\_(ツ)_/¯"), nil)
				r.NotFoundHandler.ServeHTTP(w, req)
			})
		}
	}
}
Beispiel #3
0
func (s *service) Start() {
	chain := alice.New(s.middleware()...).Then(s.router)

	bindAddr := s.config.BindAddr()
	certFile, keyFile := s.config.CertFile(), s.config.KeyFile()

	if len(certFile) > 0 && len(keyFile) > 0 {
		log.Debug("listening tls", log.Data{"addr": bindAddr, "cert": certFile, "key": keyFile})
		err := http.ListenAndServeTLS(bindAddr, certFile, keyFile, chain)
		if err != nil {
			log.Error(err, nil)
			os.Exit(1)
		}
		return
	}

	log.Debug("listening", log.Data{"addr": bindAddr})
	err := http.ListenAndServe(bindAddr, chain)
	if err != nil {
		log.Error(err, nil)
		os.Exit(1)
	}
}
Beispiel #4
0
// New creates a new instance of github.com/unrolled/render.Render
func New(config Config) Renderer {
	log.Debug("creating renderer", nil)

	return Renderer{render.New(render.Options{
		Asset:      config.Asset(),
		AssetNames: config.AssetNames(),
		//Delims:     render.Delims{Left: "[:", Right: ":]"},
		Layout: "layout",
		Funcs: []template.FuncMap{template.FuncMap{
			"map":      htmlform.Map,
			"ext":      htmlform.Extend,
			"fnn":      htmlform.FirstNotNil,
			"arr":      htmlform.Arr,
			"lc":       strings.ToLower,
			"uc":       strings.ToUpper,
			"join":     strings.Join,
			"safehtml": func(s string) template.HTML { return template.HTML(s) },
			"date": func(t *time.Time, layout string) string {
				return t.Format(layout)
			},
		}},
	}), config}
}
Beispiel #5
0
func (kc *kafkaConsumer) Start() chan Message {
	kc.sigChan = make(chan os.Signal, 1)
	msgChan := make(chan Message, 1)

	signal.Notify(kc.sigChan, os.Interrupt)
	go func() {
		<-kc.sigChan
		kc.consumerGroup.Close()
		close(msgChan)
	}()

	cfg := consumergroup.NewConfig()

	cfg.Offsets.Initial = kc.Config.InitialOffset()
	cfg.Offsets.ProcessingTimeout = kc.Config.ProcessingTimeout()

	var zookeeperNodes []string
	url := kc.Config.ZookeeperURL()
	if chroot := kc.Config.ZookeeperChroot(); len(chroot) > 0 {
		url += "/" + chroot
	}
	zookeeperNodes, cfg.Zookeeper.Chroot = kazoo.ParseConnectionString(url)

	var cg *consumergroup.ConsumerGroup
	var err error
	var attempts, curExp int

	for {
		attempts++
		cg, err = consumergroup.JoinConsumerGroup(
			kc.Config.ConsumerGroup(),
			kc.Config.Topics(),
			zookeeperNodes,
			cfg,
		)
		if err != nil {
			log.Error(err, nil)
			if attempts > maxAttempts {
				log.Debug("reached maximum attempts, exiting", nil)
				os.Exit(1)
			}
			if curExp == 0 {
				curExp = 2
			}
			curExp *= 2
			if curExp > maxExp {
				curExp = maxExp
			}
			log.Debug("sleeping", log.Data{"ms": curExp})
			time.Sleep(time.Millisecond * time.Duration(curExp))
			continue
		}
		break
	}

	kc.consumerGroup = cg

	go func() {
		for err := range cg.Errors() {
			log.Error(err, nil)
		}
	}()

	go func() {
		log.Debug("waiting for messages", nil)
		for m := range cg.Messages() {
			log.Debug("message", log.Data{"msg": m})
			msgChan <- saramaMessage{m}
		}
	}()

	return msgChan
}
Beispiel #6
0
// NewFromRender returns a new Renderer using the supplied unrolled.Render
func NewFromRender(r *render.Render, config Config) Renderer {
	log.Debug("creating renderer", nil)

	return Renderer{r, config}
}
Beispiel #7
0
// Init initialises the session store
func Init(cfg Config) {
	log.Debug("initialising session storage", log.Data{"name": cfg.Name(), "secret": cfg.Secret()})
	s = &store{sessions.NewCookieStore(cfg.Secret()), cfg.Name()}
}