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 }
func (d *HttpDirective) Apply(c api.Server, items map[string]interface{}) (bool, error) { if err := api.Remarshal(items, d); err != nil { log.Error("directive.http.remarshal", "error", err) return false, err } host, cf, kf := d.Http, d.CertFile, d.KeyFile if host == "" { host = d.Https } // mb check cert and key files exist and are readable c.Endpoint(host, cf, kf, host == d.Https) return false, nil }
func (d *LocationDirective) SetContentHandler(c api.Server, r *mux.Route) *mux.Route { // r = r.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { L := c.NewLuaState(w, r) defer L.Close() // if file, read once and set string err := L.DoFile("test.lua") // if string, set string // if value in handler map, set handler // if values in handler map, set handlers if err != nil { log.Error("server.request.lua", "path", r.URL, "error", err) } }) return r }