Beispiel #1
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
}
Beispiel #2
0
func (d *InitDirective) Apply(c api.Server, items map[string]interface{}) (bool, error) {
	s, ok := items["init"].(string)
	if !ok {
		return false, errors.New("init value not string")
	}

	sz := strings.TrimSpace(s)
	if bs, err := ioutil.ReadFile(sz); err == nil {
		s = string(bs)
	} else {
		if !strings.HasSuffix(err.Error(), "no such file or directory") {
			log.Info("directive.init.readfile", "error", err)
		}
	}

	c.OnInit(func(L *lua.LState) error {
		L.SetGlobal("motd", lua.LString("hello, world oninit"))
		return nil
	})

	return false, nil
}