func main() { var ( httpAddr = flag.String("http", "0.0.0.0:80", "HTTP service address.") healthAddr = flag.String("health", "0.0.0.0:81", "Health service address.") secret = flag.String("secret", "secret", "JWT signing secret.") ) flag.Parse() log.Println("Starting server...") log.Printf("Health service listening on %s", *healthAddr) log.Printf("HTTP service listening on %s", *httpAddr) errChan := make(chan error, 10) hmux := http.NewServeMux() hmux.HandleFunc("/healthz", health.HealthzHandler) hmux.HandleFunc("/readiness", health.ReadinessHandler) hmux.HandleFunc("/healthz/status", health.HealthzStatusHandler) hmux.HandleFunc("/readiness/status", health.ReadinessStatusHandler) healthServer := manners.NewServer() healthServer.Addr = *healthAddr healthServer.Handler = handlers.LoggingHandler(hmux) go func() { errChan <- healthServer.ListenAndServe() }() mux := http.NewServeMux() mux.HandleFunc("/", handlers.HelloHandler) mux.Handle("/login", handlers.LoginHandler(*secret, user.DB)) mux.Handle("/secure", handlers.JWTAuthHandler(handlers.HelloHandler)) mux.Handle("/version", handlers.VersionHandler(version)) httpServer := manners.NewServer() httpServer.Addr = *httpAddr httpServer.Handler = handlers.LoggingHandler(mux) go func() { errChan <- httpServer.ListenAndServe() }() signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) for { select { case err := <-errChan: if err != nil { log.Fatal(err) } case s := <-signalChan: log.Println(fmt.Sprintf("Captured %v. Exiting...", s)) health.SetReadinessStatus(http.StatusServiceUnavailable) httpServer.BlockingClose() os.Exit(0) } } }
func main() { handler := newHandler() server = manners.NewServer() ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt, os.Kill) go listenForShutdown(ch) server.ListenAndServe(":8080", handler) }
func NewServer() { l4g.Info("Server is initializing...") Srv = &Server{} Srv.Server = manners.NewServer() Srv.Store = store.NewSqlStore() Srv.Router = mux.NewRouter() Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404) }
func main() { go func() { SignalListener() }() handler := MyHTTPHandler() server := manners.NewServer() server.ListenAndServe(":7000", handler) }
func StartClientApi() { handler := rest.ResourceHandler{ EnableRelaxedContentType: true, } //api := clientApi{} err := handler.SetRoutes( rest.RouteObjectMethod("POST", "/jobs", api, "CreateJob"), ) if err != nil { log.Fatal(err) } api.server = manners.NewServer() glog.Info("client api starting") api.server.ListenAndServe(":8080", &handler) }
func newServer(cfg *Config) (*server, error) { log := logrus.New() if cfg.Debug { log.Level = logrus.DebugLevel } log.Formatter = &logrus.TextFormatter{DisableColors: true} u, err := url.Parse(cfg.VSphereURL) if err != nil { return nil, err } if !u.IsAbs() { return nil, fmt.Errorf("vSphere API URL must be absolute") } db, err := newPGDatabase(cfg.DatabaseURL) if err != nil { return nil, err } paths := jupiterbrain.VSpherePaths{ BasePath: cfg.VSphereBasePath, VMPath: cfg.VSphereVMPath, ClusterPath: cfg.VSphereClusterPath, } srv := &server{ addr: cfg.Addr, authToken: cfg.AuthToken, sentryDSN: cfg.SentryDSN, log: log, i: jupiterbrain.NewVSphereInstanceManager(log, u, paths), n: negroni.New(), r: mux.NewRouter(), s: manners.NewServer(), db: db, bootTime: time.Now().UTC(), } return srv, nil }
func (self *Dispatcher) Start() { ctx := api.NewContext(self.config) // Work with Server::Stareter baseListener, err := ss.NewListenerOrDefault("tcp", self.config.Dispatcher.Listen) if err != nil { panic(fmt.Sprintf("Failed to listen at %s: %s", self.config.Dispatcher.Listen, err)) } ctx.Debugf("Listening on %s", baseListener.Addr()) s := manners.NewServer() l := manners.NewListener(baseListener, s) err = http.Serve(l, self) if err != nil { log.Fatal( fmt.Sprintf("Error from server's ListenAndServe: %s\n", err), ) } }
// Start starts the registered service matching the given prefix key. // func (s *Srv) Start(key string) error { app, ok := s.list[key] if !ok { return errors.New("start webservice: key not found") } started := false for _, v := range s.list { started = started || v.started } app.started = true if started { return nil } manners.NewServer() go manners.ListenAndServe(s.URL(), s) return nil }
// NewServer creates a new server instance func NewServer(config *Config) *Server { return &Server{ GracefulServer: manners.NewServer(), Config: config, } }
func (s *Server) Init() { cl = make(chan bool) i404, err := ioutil.ReadFile("templates/static/assets/img/404.jpg") if err != nil { panic(err) } //cookie store s.Store = sessions.NewCookieStore([]byte(s.Config.Secret)) s.Store.Options = &sessions.Options{ Path: "/", Domain: "", MaxAge: 86400 * 7, } s.Cache = NewCache(s.Config.CacheSize*1024*1024, s.Config.CacheFree*1024*1024, true) s.Cache.Start() stats = &statwrap{} go s.watcher() rc := NewReleasesController(s.DB, s.Config, s.Cache) lc := NewLogsController(s.DB) goji.Use(gzip.GzipHandler) goji.Use(s.ServerMiddleWare) goji.Use(s.AuthMiddleWare) goji.Get("/assets/404.jpg", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("content-type", "image/jpeg") w.Write(i404) }) goji.Get("/assets/*", http.FileServer(http.Dir("templates/static"))) goji.Get("/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "templates/index.html") }) goji.Get("/logs", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "templates/logs.html") }) goji.Get("/stats", s.GetStats) goji.Get("/stats/free", s.Force) goji.Get("/db/release/:id/", rc.GetRelease) goji.Get("/db/release/:id/link", rc.GetReleaseLink) goji.Get("/db/release/:id/nzb", rc.GetReleaseNzb) goji.Get("/db/release/:id/image", rc.GetReleaseImage) goji.Get("/db/release/:id/thank", rc.ThankRelease) goji.Get("/db/releases/", rc.GetReleases) goji.Get("/db/tags/", s.GetTags) goji.Get("/db/logs/", lc.GetLogs) goji.Get("/login", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "templates/login.html") }) goji.Get("/login/:key", s.Login) if *webdebug { goji.Handle("/debug/pprof/", pprof.Index) goji.Handle("/debug/pprof/cmdline", pprof.Cmdline) goji.Handle("/debug/pprof/profile", pprof.Profile) goji.Handle("/debug/pprof/symbol", pprof.Symbol) goji.Handle("/debug/pprof/block", pprof.Handler("block").ServeHTTP) goji.Handle("/debug/pprof/heap", pprof.Handler("heap").ServeHTTP) goji.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP) goji.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP) } goji.NotFound(NotFound) goji.Abandon(middleware.Logger) addr := fmt.Sprintf("%s:%s", s.Config.Host, s.Config.Port) log.WithField("tag", TAG).Infof("listening on %s", addr) serv = manners.NewServer() serv.InnerServer = http.Server{ Addr: addr, Handler: goji.DefaultMux, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } err = serv.ListenAndServe(addr, goji.DefaultMux) if err != nil { panic(err) } log.WithField("tag", TAG).Info("closing") }