Exemplo n.º 1
0
func New(bs []byte) (*Config, error) {
	src := []map[string]interface{}{}
	err := yaml.Unmarshal(bs, &src)
	if err != nil {
		log.Debug("config.init.unmarshal", "error", err)
		return nil, err
	}
	cfg := &Config{SourceItems: src}
	log.Debug("config.init.success", "bytes", len(cfg.SourceItems))
	return cfg, nil
}
Exemplo n.º 2
0
func (s *Server) Run() (err error) {
	if len(s.Endpoints) == 0 {
		return errors.New("no server endpoints; config missing http and/or https?")
	}
	L := s.RootState
	if L == nil {
		return errors.New("no root lua state")
	}
	for _, init := range s.InitFuncs {
		if err := init(L); err != nil {
			return err
		}
	}
	wg := sync.WaitGroup{}
	s.HttpServers = map[string]*http.Server{}
	for _, ep := range s.Endpoints {
		srv := &http.Server{
			Addr:           ep.Addr,
			Handler:        s.Router,
			ReadTimeout:    10 * time.Second,
			WriteTimeout:   10 * time.Second,
			MaxHeaderBytes: 1 << 20,
		}
		s.HttpServers[ep.Addr] = srv
		wg.Add(1)
		if ep.TLS {
			go func() {
				defer wg.Done()
				log.Debug("server.run", "server", srv)
				log.Info("server.run.listen-tls", "addr", srv.Addr)
				if e := srv.ListenAndServeTLS(ep.CertFile, ep.KeyFile); e != nil {
					log.Error("server.run.listen-tls", "error", e)
					err = e
				}
			}()
		} else {
			go func() {
				defer wg.Done()
				log.Debug("server.run", "server", srv)
				log.Info("server.run.listen", "addr", srv.Addr)
				if e := srv.ListenAndServe(); e != nil {
					log.Error("server.run.listen", "error", e)
					err = e
				}
			}()
		}
	}
	wg.Wait()
	return
}
Exemplo n.º 3
0
// New builds a Server from the given Config struct.
//
func New(c *config.Config) (api.Server, error) {
	r := mux.NewRouter()
	s := &Server{
		Config:    c,
		Router:    r,
		RootState: lua.NewState(),
	}

	for _, dir := range directive.All() {
		for i, decl := range c.SourceItems {
			if dir.Accept(decl) {
				dir.Apply(s, decl)
				log.Debug("directive.process", "name", dir.Name(), "block", i)
			}
		}
	}
	log.Debug("server.new", "server", s)
	return s, nil
}