Пример #1
0
func main() {
	http.HandleFunc("/favicon.ico", iconHandler)
	indexHandler := http.HandlerFunc(index)
	aboutHandler := http.HandlerFunc(about)
	logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(err)
	}
	http.Handle("/", handlers.LoggingHandler(logFile, handlers.CompressHandler(indexHandler)))
	http.Handle("/about", handlers.LoggingHandler(logFile, handlers.CompressHandler(aboutHandler)))
	server := &http.Server{
		Addr: ":8080",
	}
	log.Println("Listening...")
	server.ListenAndServe()
}
Пример #2
0
func startWebserver() {
	processEnv()

	router = mux.NewRouter()
	router.HandleFunc("/ws", wrap(wsHandler))

	dockerRouter := router.PathPrefix(fmt.Sprintf("/api/%v/docker", apiVersion)).Subrouter()
	dockerRouter.HandleFunc("/containers", wrap(dockerClient.ContainersHandler))
	dockerRouter.HandleFunc("/containers/graph", wrap(dockerClient.ContainerGraphHandler))
	dockerRouter.HandleFunc("/container/{id}", wrap(dockerClient.ContainerHandler))
	dockerRouter.HandleFunc("/images", wrap(dockerClient.ImagesHandler))
	dockerRouter.HandleFunc("/image/history/{id}", wrap(dockerClient.HistoryHandler))
	dockerRouter.HandleFunc("/info", wrap(dockerClient.InfoHandler))

	consulRouter := router.PathPrefix(fmt.Sprintf("/api/%v/consul", apiVersion)).Subrouter()
	consulRouter.HandleFunc("/datacenters", wrap(consulRegistry.DatacentersHandler))
	consulRouter.HandleFunc("/nodes", wrap(consulRegistry.NodesHandler))
	consulRouter.HandleFunc("/nodes/{dc}", wrap(consulRegistry.NodesHandler))
	consulRouter.HandleFunc("/node/{name}", wrap(consulRegistry.NodeHandler))
	consulRouter.HandleFunc("/health/{name}", wrap(consulRegistry.HealthHandler))
	consulRouter.HandleFunc("/health/{name}/{dc}", wrap(consulRegistry.HealthHandler))

	http.Handle("/", router)
	loggedRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
	panic(http.ListenAndServe(addr, handlers.CompressHandler(loggedRouter)))
}
Пример #3
0
func StartWebServer() error {
	conf, err := config.GetConfig()
	if err != nil {
		return err
	}

	var hystrixTimeout time.Duration
	conf.Hystrix.Timeout = strings.TrimSpace(conf.Hystrix.Timeout)
	if conf.Hystrix.Timeout != "" {
		hystrixTimeout, err = time.ParseDuration(conf.Hystrix.Timeout)
		if err != nil || hystrixTimeout < time.Millisecond {
			hystrixTimeout = time.Second
			log15.Error("Use default time", "module", "hystrix", "timeout", hystrixTimeout)
		}
	}

	hystrix.ConfigureCommand("waitFor", hystrix.CommandConfig{
		Timeout:                int(int64(hystrixTimeout) / int64(time.Millisecond)), // converted into Millisecond.
		MaxConcurrentRequests:  conf.Hystrix.MaxConcurrentRequests,
		ErrorPercentThreshold:  conf.Hystrix.ErrorPercentThreshold,
		RequestVolumeThreshold: conf.Hystrix.RequestVolumeThreshold,
		SleepWindow:            conf.Hystrix.SleepWindow,
	})

	e := echo.New()
	e.Post("/api/v1/tweet", createTweetV1)
	e.Get("/api/v1/tweets/:id", getAllTweetForV1)
	e.Get("/api/v1/wait/:timeout", waitFor)
	e.Get("/api/v1/wait_protected/:timeout", waitForProtected)
	e.Static("/", "www/static/")
	logsrv := log15.New("pid", os.Getpid(), "addr", conf.Web.Address)
	return listenAndServer(logsrv, conf.Web.Address, handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(e.Router())))
}
Пример #4
0
// registerWebRouter - registers web router for serving minio browser.
func registerWebRouter(mux *router.Router, web *webAPIHandlers) {
	// Initialize a new json2 codec.
	codec := json2.NewCodec()

	// Minio browser router.
	webBrowserRouter := mux.NewRoute().PathPrefix(reservedBucket).Subrouter()

	// Initialize json rpc handlers.
	webRPC := jsonrpc.NewServer()
	webRPC.RegisterCodec(codec, "application/json")
	webRPC.RegisterCodec(codec, "application/json; charset=UTF-8")
	webRPC.RegisterService(web, "Web")

	// RPC handler at URI - /minio/webrpc
	webBrowserRouter.Methods("POST").Path("/webrpc").Handler(webRPC)
	webBrowserRouter.Methods("PUT").Path("/upload/{bucket}/{object:.+}").HandlerFunc(web.Upload)
	webBrowserRouter.Methods("GET").Path("/download/{bucket}/{object:.+}").Queries("token", "{token:.*}").HandlerFunc(web.Download)

	// Add compression for assets.
	compressedAssets := handlers.CompressHandler(http.StripPrefix(reservedBucket, http.FileServer(assetFS())))

	// Serve javascript files and favicon from assets.
	webBrowserRouter.Path(fmt.Sprintf("/{assets:[^/]+.js|%s}", specialAssets)).Handler(compressedAssets)

	// Serve index.html for rest of the requests.
	webBrowserRouter.Path("/{index:.*}").Handler(indexHandler{http.StripPrefix(reservedBucket, http.FileServer(assetFS()))})
}
Пример #5
0
func TestHTTPPublisher(t *testing.T) {
	var (
		token = "abcdefg"
		id    = "1234567"
		rpt   = report.MakeReport()
		done  = make(chan struct{})
	)

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if want, have := xfer.AuthorizationHeader(token), r.Header.Get("Authorization"); want != have {
			t.Errorf("want %q, have %q", want, have)
		}
		if want, have := id, r.Header.Get(xfer.ScopeProbeIDHeader); want != have {
			t.Errorf("want %q, have %q", want, have)
		}
		var have report.Report

		reader := r.Body
		var err error
		if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
			reader, err = gzip.NewReader(r.Body)
			if err != nil {
				http.Error(w, err.Error(), http.StatusBadRequest)
				return
			}
			defer reader.Close()
		}

		if err := gob.NewDecoder(reader).Decode(&have); err != nil {
			t.Error(err)
			return
		}
		if want := rpt; !reflect.DeepEqual(want, have) {
			t.Error(test.Diff(want, have))
			return
		}
		w.WriteHeader(http.StatusOK)
		close(done)
	})

	s := httptest.NewServer(handlers.CompressHandler(handler))
	defer s.Close()

	p, err := xfer.NewHTTPPublisher(s.URL, token, id)
	if err != nil {
		t.Fatal(err)
	}
	if err := p.Publish(rpt); err != nil {
		t.Error(err)
	}

	select {
	case <-done:
	case <-time.After(time.Millisecond):
		t.Error("timeout")
	}
}
Пример #6
0
func RegisterMiddleware(apiRouter *mux.Router) http.Handler {
	var apiHandler http.Handler = apiRouter

	apiHandler = handlers.CompressHandler(apiHandler)
	apiHandler = handlers.CORS()(apiHandler)
	apiHandler = ResponseHeaders(apiHandler)

	return apiHandler
}
Пример #7
0
// NewServer construct rest server listening specific host and port and routes requests by passed routes
func NewServer(props properties.Properties, routes PathHandlers) error {
	bind := props.Get("rest.address") + ":" + props.Get("rest.port")

	log.Println("start rest server on", bind)
	listener, err := net.Listen("tcp", bind)
	if err != nil {
		return err
	}

	s := &server{
		listener: listener,
		m:        &sync.Mutex{},
		alive:    true,
		wg:       &sync.WaitGroup{},
	}

	kit.SafeGo(func() error {
		router := mux.NewRouter()
		for path := range routes {
			mh := handlers.MethodHandler{}
			for method := range routes[path] {
				log.Printf("setup rest handler %v %v\n", method, path)
				mh[method] = s.wrapHandler(routes[path][method])
			}
			router.Path(path).Handler(mh)
		}

		handler := http.Handler(router)

		compression, err := strconv.ParseBool(props.Get("rest.compression"))
		if err == nil && compression {
			log.Println("enable compression for rest server")
			handler = handlers.CompressHandler(handler)
		} else {
			log.Println("no compression for rest server")
		}

		log.Println("rest server serves")
		err = http.Serve(listener, handler)
		if err != nil {
			return err
		}

		return nil
	})

	kit.SafeGo(func() error {
		<-registry.DoneChannel()
		s.Stop()
		return nil
	})

	return nil
}
Пример #8
0
// Make sure Stopping a client works even if the connection or the remote app
// gets stuck for whatever reason.
// See https://github.com/weaveworks/scope/issues/1576
func TestStop(t *testing.T) {
	var (
		rpt            = report.MakeReport()
		stopHanging    = make(chan struct{})
		receivedReport = make(chan struct{})
	)

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		close(receivedReport)
		<-stopHanging
	})

	s := httptest.NewServer(handlers.CompressHandler(handler))
	defer s.Close()

	u, err := url.Parse(s.URL)
	if err != nil {
		t.Fatal(err)
	}

	pc := ProbeConfig{
		Token:    "",
		ProbeID:  "",
		Insecure: false,
	}

	p, err := NewAppClient(pc, u.Host, s.URL, nil)
	if err != nil {
		t.Fatal(err)
	}

	rp := NewReportPublisher(p)

	// Make sure the app received our report and is stuck
	for done := false; !done; {
		select {
		case <-receivedReport:
			done = true
		default:
			if err := rp.Publish(rpt); err != nil {
				t.Error(err)
			}
			time.Sleep(10 * time.Millisecond)
		}
	}

	// Close the client while the app is stuck
	p.Stop()

	// Let the server go so that the test can end
	close(stopHanging)
}
Пример #9
0
func New(conf *config.Config, db db.DB) (*Server, error) {
	s := &Server{
		config: conf,
		db:     db,
	}

	if !s.config.Server.Development {
		tmpl, err := s.loadTemplates()
		if err != nil {
			return nil, err
		}
		s.tmpl = tmpl
	}

	n := negroni.Classic()

	csrfHandler := csrf.Protect(
		[]byte(s.config.Server.CSRFAuthKey),
		csrf.Secure(!s.config.Server.Development),
		csrf.FieldName("_csrf"),
	)
	n.UseFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		csrfHandler(next).ServeHTTP(w, r)
	})

	n.UseFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		handlers.HTTPMethodOverrideHandler(next).ServeHTTP(w, r)
	})
	n.UseFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		handlers.CompressHandler(next).ServeHTTP(w, r)
	})

	n.UseFunc(s.userAuthMiddleware)

	r := httprouter.New()
	r.ServeFiles("/static/*filepath", http.Dir(path.Join(DataDir, "public")))
	r.GET("/signin", s.wrapHandler(s.HandleGetSignIn))
	r.POST("/signin", s.wrapHandler(s.HandlePostSignIn))
	r.POST("/signout", s.wrapHandler(s.HandlePostSignOut))
	r.GET("/logs", s.wrapHandler(s.HandleGetLogs))
	r.POST("/logs", s.wrapHandler(s.HandlePostLog))
	r.GET("/logs/:id/download", s.wrapHandler(s.HandleDownloadLog))
	r.GET("/logs/:id", s.wrapHandler(s.HandleGetLog))
	r.PATCH("/logs/:id", s.wrapHandler(s.HandlePatchLog))
	r.DELETE("/logs/:id", s.wrapHandler(s.HandleDeleteLog))
	r.GET("/", s.wrapHandler(s.HandleDashboard))
	n.UseHandler(r)

	s.handler = n
	return s, nil
}
Пример #10
0
func main() {
	log.Infof("Starting Helphone API service on port %s", port)

	router := service.NewRouter()
	h := service.MuxWrapper{
		IsReady: false,
		Router:  router,
	}

	go manager.Init()

	handler := handlers.CompressHandler(handlers.ProxyHeaders(cors.Default().Handler(h.Router)))
	log.Fatal(http.ListenAndServe(":"+port, handler))
}
Пример #11
0
//Setup setups handlers for 2ch interface.
func Setup(s *cgi.LoggingServeMux) {
	log.Println("start 2ch interface")
	rtr := mux.NewRouter()

	cgi.RegistToRouter(rtr, "/2ch/", boardApp)
	cgi.RegistToRouter(rtr, "/2ch/dat/{datkey:[^\\.]+}.dat", threadApp)
	cgi.RegistToRouter(rtr, "/2ch/{board:[^/]+}/subject.txt", subjectApp)
	cgi.RegistToRouter(rtr, "/2ch/subject.txt", subjectApp)
	cgi.RegistToRouter(rtr, "/2ch/{board:[^/]+}/head.txt", headApp)
	cgi.RegistToRouter(rtr, "/2ch/head.txt", headApp)
	s.Handle("/2ch/", handlers.CompressHandler(rtr))

	s.RegistCompressHandler("/test/bbs.cgi", postCommentApp)
}
Пример #12
0
func main() {

	r := httprouter.New()

	// Routes
	r.GET("/", HomeHandler)
	r.POST("/", UserRedirectHandler)

	r.GET("/stream/:username", StreamHandler)

	// Static assets
	r.ServeFiles("/static/*filepath", http.Dir("static"))

	fmt.Println("Serving HTTP on port 3000")
	http.ListenAndServe(":3000", handlers.CompressHandler(r))
}
Пример #13
0
func main() {
	log.SetOutput(os.Stderr)

	port := flag.String("port", "8080", "server listening tcp port")
	dbServer := flag.String("server", "localhost", "database server")
	dbName := flag.String("db", "estia", "database name")
	siteType := flag.String("type", "dir", "site path type zip or dir")
	sitePath := flag.String("path", "wwwroot", "path containing site")
	flag.Parse()

	server = *dbServer
	database = *dbName

	db, err := mgo.Dial(server)
	if err != nil {
		log.Panic(err)
	}
	defer db.Close()

	router := NewRouter()
	if *siteType == "zip" {
		rd, err := zip.OpenReader(*sitePath)
		if err != nil {
			log.Fatal(err)
		}
		fs := zipfs.New(rd, *sitePath)
		router.PathPrefix("/").Handler(http.FileServer(httpfs.New(fs)))
	} else {
		router.PathPrefix("/").Handler(http.FileServer(http.Dir(*sitePath)))
	}

	withLog := handlers.LoggingHandler(os.Stdout, router)

	withdb := WithDB(db, withLog)

	withcors := handlers.CORS()(withdb)

	withGz := handlers.CompressHandler(withcors)

	log.Printf(
		"%s\t%s",
		"Server listening on ",
		*port,
	)

	log.Fatal(http.ListenAndServeTLS(":"+*port, "cert.pem", "key.pem", context.ClearHandler(withGz)))
}
Пример #14
0
func main() {
	configRead()

	r := mux.NewRouter()
	r.HandleFunc("/{template}", Handler)

	fmt.Printf("accepting connections on %s:%s\n", Addr, Port)

	err := http.ListenAndServe(
		fmt.Sprintf("%s:%s", Addr, Port),
		handlers.LoggingHandler(
			os.Stdout,
			handlers.CompressHandler(r)))
	if err != nil {
		log.Fatal(err)
	}
}
Пример #15
0
func dummyServer(t *testing.T, expectedToken, expectedID string, expectedVersion string, expectedReport report.Report, done chan struct{}) *httptest.Server {
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if have := r.Header.Get("Authorization"); fmt.Sprintf("Scope-Probe token=%s", expectedToken) != have {
			t.Errorf("want %q, have %q", expectedToken, have)
		}

		if have := r.Header.Get(xfer.ScopeProbeIDHeader); expectedID != have {
			t.Errorf("want %q, have %q", expectedID, have)
		}

		if have := r.Header.Get(xfer.ScopeProbeVersionHeader); expectedVersion != have {
			t.Errorf("want %q, have %q", expectedID, have)
		}

		var have report.Report

		reader := r.Body
		var err error
		if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
			reader, err = gzip.NewReader(r.Body)
			if err != nil {
				http.Error(w, err.Error(), http.StatusBadRequest)
				return
			}
			defer reader.Close()
		}

		decoder := codec.NewDecoder(reader, &codec.MsgpackHandle{})
		if err := decoder.Decode(&have); err != nil {
			t.Error(err)
			return
		}
		if !reflect.DeepEqual(expectedReport, have) {
			t.Error(test.Diff(expectedReport, have))
			return
		}
		w.WriteHeader(http.StatusOK)
		done <- struct{}{}
	})

	return httptest.NewServer(handlers.CompressHandler(handler))
}
Пример #16
0
func TestAppClientDetails(t *testing.T) {
	var (
		id      = "foobarbaz"
		version = "imalittleteapot"
		want    = xfer.Details{ID: id, Version: version}
	)

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		encoder := codec.NewEncoder(w, &codec.JsonHandle{})
		if err := encoder.Encode(want); err != nil {
			t.Fatal(err)
		}
	})

	s := httptest.NewServer(handlers.CompressHandler(handler))
	defer s.Close()

	u, err := url.Parse(s.URL)
	if err != nil {
		t.Fatal(err)
	}

	pc := ProbeConfig{
		Token:    "",
		ProbeID:  "",
		Insecure: false,
	}
	p, err := NewAppClient(pc, u.Host, s.URL, nil)
	if err != nil {
		t.Fatal(err)
	}
	defer p.Stop()

	have, err := p.Details()
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(want, have) {
		t.Error(test.Diff(want, have))
		return
	}
}
Пример #17
0
func ServerAction(c *cli.Context) {
	router := httprouter.New()
	router.POST("/convert", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		file, _, err := r.FormFile("messages")
		defer file.Close()
		WebCheck(w, err)

		b, err := ioutil.ReadAll(file)
		WebCheck(w, err)
		fbData := FromHTML(string(b))

		w.Header().Set("Content-type", "application/json")
		w.Write([]byte(ToJSON(fbData, false)))
	})
	router.ServeFiles("/*filepath", http.Dir("public"))

	addr := GetAddr()

	fmt.Println("Listening on", addr)
	check(http.ListenAndServe(addr, handlers.CompressHandler(router)))
}
Пример #18
0
func main() {
	// Init bufferpool, used for rendering templates
	bufpool = bpool.NewBufferPool(48)

	// Load config file
	if _, err := toml.DecodeFile("config.ini", &conf); err != nil {
		log.Fatal("Couldn't parse config file: ", err)
	}

	// Setup remote repository
	repo = newRepo(
		conf.QuadStore.Endpoint,
		time.Duration(conf.QuadStore.OpenTimeout)*time.Millisecond,
		time.Duration(conf.QuadStore.ReadTimeout)*time.Millisecond,
	)

	// Parse Query bank
	qBank = sparql.LoadBank(bytes.NewBufferString(queries))

	// Register metrics
	status = registerMetrics()

	// HTTP routing
	mux := http.NewServeMux()
	var handler mainHandler
	mux.HandleFunc("/robots.txt", serveFile("data/robots.txt"))
	mux.HandleFunc("/css/styles.css", serveFile("data/css/styles.css"))
	mux.HandleFunc("/favicon.ico", serveFile("data/favicon.ico"))
	mux.HandleFunc("/.status", statusHandler)
	mux.HandleFunc("/literals", literalsHandler)
	mux.Handle("/", Timed(CountedByStatusXX(handler, "status", metrics.DefaultRegistry),
		"responseTime",
		metrics.DefaultRegistry))

	fmt.Printf("Listening on port %d ...\n", conf.ServePort)
	err := http.ListenAndServe(fmt.Sprintf(":%d", conf.ServePort), handlers.CompressHandler(mux))
	if err != nil {
		log.Println(err)
	}
}
Пример #19
0
//Setup setups handlers for thread.cgi
func Setup(s *cgi.LoggingServeMux) {
	rtr := mux.NewRouter()

	cgi.RegistToRouter(rtr, cfg.ThreadURL+"/", printThreadIndex)

	reg := cfg.ThreadURL + "/{datfile:thread_[0-9A-F]+}/{id:[0-9a-f]{32}}/s{stamp:\\d+}.{thumbnailSize:\\d+x\\d+}.{suffix:.*}"
	cgi.RegistToRouter(rtr, reg, printAttach)

	reg = cfg.ThreadURL + "/{datfile:thread_[0-9A-F]+}/{id:[0-9a-f]{32}}/{stamp:\\d+}.{suffix:.*}"
	cgi.RegistToRouter(rtr, reg, printAttach)

	reg = cfg.ThreadURL + "/{path:[^/]+}{end:/?$}"
	cgi.RegistToRouter(rtr, reg, printThread)

	reg = cfg.ThreadURL + "/{path:[^/]+}/{id:[0-9a-f]{8}}{end:$}"
	cgi.RegistToRouter(rtr, reg, printThread)

	reg = cfg.ThreadURL + "/{path:[^/]+}/p{page:[0-9]+}{end:$}"
	cgi.RegistToRouter(rtr, reg, printThread)

	s.Handle(cfg.ThreadURL+"/", handlers.CompressHandler(rtr))
}
Пример #20
0
func main() {
	var (
		clientID      = flag.String("client-id", "", "soundcloud client id")
		clientSecret  = flag.String("client-secret", "", "soundcloud client secret")
		port          = flag.String("port", "3000", "address to bind the server on")
		callbackToken = flag.String("callback-token", "testToken", "OAuth token used to protect against CSRF attacks")
		appURL        = flag.String("app-url", "http://localhost:3000", "url of the app")
		store         = sessions.NewCookieStore([]byte("secret key"))
	)
	flag.Parse()

	config := &oauth2.Config{
		ClientID:     *clientID,
		ClientSecret: *clientSecret,
		RedirectURL:  *appURL + "/oauth2callback",
		Scopes: []string{
			"non-expiring",
		},
		Endpoint: oauth2.Endpoint{
			AuthURL:  "https://soundcloud.com/connect",
			TokenURL: "https://api.soundcloud.com/oauth2/token",
		},
	}
	m := pat.New()

	m.Get("/public/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, r.URL.Path[1:])
	})

	m.Get("/stream", getStream(store))
	m.Get("/oauth2callback", handleOAuth2Callback(store, config, *callbackToken))
	m.Post("/authorize", handleAuthorize(config, *callbackToken))
	m.Get("/", index(*clientID))

	handler := handlers.CompressHandler(handlers.LoggingHandler(os.Stdout, m))
	log.Printf("Listening on %s\n", *port)
	log.Fatal(http.ListenAndServe(":"+*port, handler))
}
Пример #21
0
func serveHTTP() {
	template_vars = TemplateVars{
		Addr:      *addr,
		TargetURL: *target_url,
		UseSleep:  *use_sleep,
	}

	mux := http.NewServeMux()
	mux.Handle("/", serveHome())
	mux.Handle("/redirect", serveRedirect())
	mux.Handle("/sleep", serveSleep())

	s := &http.Server{
		Addr:           *addr,
		Handler:        handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(context.ClearHandler(mux))),
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	log.Println("Serving HTTP on address", *addr)
	log.Fatal(s.ListenAndServe())
}
Пример #22
0
func main() {

	models.OpenDB("sqlite3", "./goblog.db")

	// My Customized httprouter
	webapp := app.NewApp()

	// `auth` group login and authentication methods
	auth := controllers.AuthMiddware()

	// simple cache
	redisPool := cache.NewCachePool("127.0.0.1:6379")
	cacheBackend := cache.NewCache("blog_", redisPool, time.Hour*12)

	// static file
	webapp.ServeFiles("/static/*filepath", http.Dir("static/"))

	// template use for angularjs
	webapp.ServeFiles("/templates/*filepath", http.Dir("static/templates/"))

	// call origin `httprouter.GET`
	webapp.Router.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		http.ServeFile(w, r, "static/templates/base.html")
	})

	// use email and password login return JWT token
	webapp.POST("/login", auth.LoginController)

	webapp.GET("/categorys", controllers.CategorysController)

	// return articles by category max limit 10
	webapp.Handler("GET", "/category/:category_id", controllers.ArticlesController(10))

	// return and cache owner data
	webapp.Handler(
		"GET",
		"/owner",
		app.CacheHanle(
			cacheBackend,
			app.MiddlewareFunc(controllers.OwnerController),
		),
	)

	// return and cache article
	webapp.Handler(
		"GET",
		"/article/:article_id",
		app.CacheHanle(
			cacheBackend,
			app.MiddlewareFunc(controllers.GetArticle)))

	// require user token and at least editor permissions
	// create article
	webapp.Handler(
		"POST",
		"/article",
		app.RequireUserHandle(
			auth.ValidateUser,
			app.AccessHandle(
				auth.ValidateAuthority,
				models.EDITOR,
				app.MiddlewareFunc(controllers.PostArticle)),
		),
	)

	// require user token and at least admin permissions
	// delete article and cache
	webapp.Handler(
		"DELETE",
		"/article/:article_id",
		app.RequireUserHandle(
			auth.ValidateUser,
			app.AccessHandle(
				auth.ValidateAuthority,
				models.ADMIN,
				app.DeleteCacheHanle(
					cacheBackend,
					app.MiddlewareFunc(controllers.DeleteArticle)),
			),
		),
	)

	// require user token and at least admin permissions
	// update article and delete cache
	webapp.Handler(
		"PUT",
		"/article/:article_id",
		app.RequireUserHandle(
			auth.ValidateUser,
			app.AccessHandle(
				auth.ValidateAuthority,
				models.ADMIN,
				app.DeleteCacheHanle(
					cacheBackend,
					app.MiddlewareFunc(controllers.ModifyArticle)),
			),
		),
	)

	logging.Logger.Info("Start listen...")
	log.Fatal(http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(webapp))))
}
Пример #23
0
// The main function for the API server.
func main() {
	flag.Parse()

	// Print the version if requested.
	if *printVersion {
		fmt.Println(VERSION)
		os.Exit(0)
	}

	debug := ioutil.Discard
	if *debugMode {
		debug = os.Stdout
	}

	logging.InitLogging(debug, os.Stdout, os.Stdout, os.Stderr, os.Stderr)

	initPeopleMap()

	r := mux.NewRouter().StrictSlash(true)

	// Print a list of urls as hyperlinks.
	r.Handle(*baseUrl+"/", apiHandler(handlers.MethodHandler{
		"GET": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			enc := json.NewEncoder(w)
			enc.Encode(&struct {
				JsonApi map[string]string `json:"jsonapi"`
				Links   map[string]string `json:"links"`
			}{
				map[string]string{
					"version": "1.0",
				},
				map[string]string{
					"people": *baseUrl + "/people/",
				},
			})
		}),
	}))

	// An API handler to get basic info about me.
	r.Handle(*baseUrl+"/people/", apiHandler(handlers.MethodHandler{
		"GET": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			enc := json.NewEncoder(w)

			p := make([]Resource, 0, len(PeopleMap))
			for _, v := range PeopleMap {
				p = append(p, v)
			}

			enc.Encode(&struct {
				Data []Resource `json:"data"`
			}{
				p,
			})
		}),
	}))

	r.Handle(*baseUrl+"/people/{id}", apiHandler(handlers.MethodHandler{
		"GET": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			enc := json.NewEncoder(w)
			vars := mux.Vars(r)
			p, ok := PeopleMap[vars["id"]]
			if ok {
				enc.Encode(&p)
			} else {
				w.WriteHeader(404)
				enc.Encode(&NotFound)
			}
		}),
	}))

	r.HandleFunc("/_status/healthz", healthHandler)
	r.HandleFunc("/_status/readiness", readinessHandler)
	r.HandleFunc("/_status/version", versionHandler)

	logging.Info.Printf("API service listening on %s...", *addr)

	// Support GZip Compression
	h := handlers.CompressHandler(r)

	s := &http.Server{
		Addr:    *addr,
		Handler: h,
	}
	if *certFile != "" && *keyFile != "" {
		log.Fatal(s.ListenAndServeTLS(*certFile, *keyFile))
	} else {
		log.Fatal(s.ListenAndServe())
	}
}
func main() {
	var config Config
	var filename string

	if len(os.Args) < 2 {
		filename = "plotter.ini"
	} else {
		filename = os.Args[1]
	}

	rawConfig, err := ini.Load(filename)
	if err != nil {
		fmt.Printf("Could not parse %s: %v\n", filename, err)
		return
	}

	/* Validate the configuration file. */
	defaultSect := rawConfig.Section("")
	for requiredKey, _ := range configRequiredKeys {
		if !defaultSect.HasKey(requiredKey) {
			fmt.Printf("Configuration file is missing required key \"%s\"\n", requiredKey)
			return
		}
	}

	rawConfig.NameMapper = ini.TitleUnderscore
	err = rawConfig.MapTo(&config)
	if err != nil {
		fmt.Printf("Could not map configuration file: %v\n", err)
		return
	}

	mdServer = config.MetadataServer
	csvURL = config.CsvUrl
	csvMaxPoints = config.CsvMaxPointsPerStream

	mongoConn, err := mgo.Dial(config.MongoServer)
	if err != nil {
		fmt.Printf("Could not connect to MongoDB Server at address %s\n", config.MongoServer)
		os.Exit(1)
	}

	plotterDBConn := mongoConn.DB("mr_plotter")
	permalinkConn = plotterDBConn.C("permalinks")
	accountConn = plotterDBConn.C("accounts")

	dr = NewDataRequester(config.DbAddr, int(config.NumDataConn), config.MaxDataRequests, time.Duration(config.DbDataTimeoutSeconds)*time.Second, false)
	if dr == nil {
		os.Exit(1)
	}
	br = NewDataRequester(config.DbAddr, int(config.NumBracketConn), config.MaxBracketRequests, time.Duration(config.DbBracketTimeoutSeconds)*time.Second, true)
	if br == nil {
		os.Exit(1)
	}

	go purgeSessionsPeriodically(config.SessionExpirySeconds, config.SessionPurgeIntervalSeconds)

	go logWaitingRequests(os.Stdout, time.Duration(config.OutstandingRequestLogInterval)*time.Second)

	go logNumGoroutines(os.Stdout, time.Duration(config.NumGoroutinesLogInterval)*time.Second)

	token64len = base64.StdEncoding.EncodedLen(TOKEN_BYTE_LEN)
	token64dlen = base64.StdEncoding.DecodedLen(token64len)
	permalinklen = base64.URLEncoding.EncodedLen(MONGO_ID_LEN)
	permalinkdlen = base64.URLEncoding.DecodedLen(permalinklen)

	http.Handle("/", http.FileServer(http.Dir(config.PlotterDir)))
	http.HandleFunc("/dataws", datawsHandler)
	http.HandleFunc("/data", dataHandler)
	http.HandleFunc("/bracketws", bracketwsHandler)
	http.HandleFunc("/bracket", bracketHandler)
	http.HandleFunc("/metadata", metadataHandler)
	http.HandleFunc("/permalink", permalinkHandler)
	http.HandleFunc("/csv", csvHandler)
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/logoff", logoffHandler)
	http.HandleFunc("/changepw", changepwHandler)
	http.HandleFunc("/checktoken", checktokenHandler)

	var loggedHandler http.Handler = httpHandlers.CompressHandler(httpHandlers.CombinedLoggingHandler(os.Stdout, http.DefaultServeMux))

	var portStrHTTP string = fmt.Sprintf(":%d", config.HttpPort)
	var portStrHTTPS string = fmt.Sprintf(":%d", config.HttpsPort)
	if config.UseHttp && config.UseHttps {
		go func() {
			log.Fatal(http.ListenAndServeTLS(portStrHTTPS, config.CertFile, config.KeyFile, loggedHandler))
			os.Exit(1)
		}()

		if config.HttpsRedirect {
			var redirect http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				var url *url.URL = r.URL
				url.Scheme = "https"
				url.Host = r.Host + portStrHTTPS
				http.Redirect(w, r, url.String(), http.StatusFound)
			})
			var loggedRedirect http.Handler = httpHandlers.CompressHandler(httpHandlers.CombinedLoggingHandler(os.Stdout, redirect))
			log.Fatal(http.ListenAndServe(portStrHTTP, loggedRedirect))
		} else {
			log.Fatal(http.ListenAndServe(portStrHTTP, loggedHandler))
		}
	} else if config.UseHttps {
		log.Fatal(http.ListenAndServeTLS(portStrHTTPS, config.CertFile, config.KeyFile, loggedHandler))
	} else if config.UseHttp {
		log.Fatal(http.ListenAndServe(portStrHTTP, loggedHandler))
	}
	os.Exit(1)
}
Пример #25
0
func main() {
	go dummyAllocations()
	log.Fatal(http.ListenAndServe(":8080", handlers.CompressHandler(http.DefaultServeMux)))
}
Пример #26
0
func main() {
	initFlags()

	if len(options.Configs) > 0 {
		for _, configDef := range options.Configs {
			log.Printf("Creating config file:  %v => %v\n", configDef.template, configDef.output)
			createConfig(configDef.template, configDef.output)
		}
		log.Println()
	}

	if len(options.Services) > 0 {
		tlsConfig := &tls.Config{
			RootCAs:            x509.NewCertPool(),
			InsecureSkipVerify: options.SkipCertValidation,
		}
		transport := &http.Transport{TLSClientConfig: tlsConfig}
		if len(options.CACerts) > 0 {
			for _, caFile := range options.CACerts {
				// Load our trusted certificate path
				pemData, err := ioutil.ReadFile(caFile)
				if err != nil {
					log.Fatal("Couldn't read CA file, ", caFile, ": ", err)
				}
				if ok := tlsConfig.RootCAs.AppendCertsFromPEM(pemData); !ok {
					log.Fatal("Couldn't load PEM data from CA file, ", caFile)
				}
			}
		}
		for _, serviceDef := range options.Services {
			actualHost, port, err := validateServiceHost(serviceDef.url.Host)
			if err != nil {
				if options.FailOnUnknownServices {
					log.Fatalf("Unknown service host: %s", serviceDef.url.Host)
				} else {
					log.Printf("Unknown service host: %s", serviceDef.url.Host)
				}
			} else {
				if len(port) > 0 {
					actualHost += ":" + port
				}
				serviceDef.url.Host = actualHost
			}
			log.Printf("Creating service proxy: %v => %v\n", serviceDef.prefix, serviceDef.url.String())
			rp := httputil.NewSingleHostReverseProxy(serviceDef.url)
			rp.Transport = transport
			http.Handle(serviceDef.prefix, http.StripPrefix(serviceDef.prefix, rp))
		}
		log.Println()
	}

	if options.ServeWww {
		httpDir := http.Dir(options.StaticDir)
		staticHandler := http.FileServer(httpDir)
		if options.StaticCacheMaxAge > 0 {
			staticHandler = maxAgeHandler(options.StaticCacheMaxAge.Seconds(), staticHandler)
		}

		if len(options.DefaultPage) > 0 {
			staticHandler = defaultPageHandler(options.DefaultPage, httpDir, staticHandler)
		}
		if options.CompressHandler {
			staticHandler = handlers.CompressHandler(staticHandler)
		}
		http.Handle(options.StaticPrefix, staticHandler)
	}

	log.Printf("Listening on :%d\n", options.Port)
	log.Println()

	registerMimeTypes()

	srv := &http.Server{
		Addr: fmt.Sprintf(":%d", options.Port),
	}
	http2.ConfigureServer(srv, &http2.Server{})

	var handler http.Handler = http.DefaultServeMux

	if options.AccessLogging {
		handler = handlers.CombinedLoggingHandler(os.Stdout, handler)
	}

	srv.Handler = handler

	if len(options.TlsCertFile) > 0 && len(options.TlsKeyFile) > 0 {
		log.Fatal(srv.ListenAndServeTLS(options.TlsCertFile, options.TlsKeyFile))
	} else {
		log.Fatal(srv.ListenAndServe())
	}
}
Пример #27
0
func init() {
	r := mux.NewRouter()

	r.NewRoute().
		Methods("POST").
		Path("/api/login").
		Handler(handleIdentity(http.HandlerFunc(api.HandleLogin)))
	r.NewRoute().
		Methods("POST").
		Path("/api/logout").
		Handler(handleIdentity(http.HandlerFunc(api.HandleLogout)))

	r.NewRoute().
		Methods("GET").
		Path("/api/accounts").
		Handler(handleIdentity(http.HandlerFunc(api.ServeAccountList)))
	r.NewRoute().
		Methods("POST").
		Path("/api/accounts").
		Handler(handleIdentity(http.HandlerFunc(api.CreateAccount)))
	r.NewRoute().
		Methods("POST").
		Path("/api/accounts/import").
		Handler(handleIdentity(http.HandlerFunc(api.ImportAccounts)))
	r.NewRoute().
		Methods("GET").
		Path("/api/accounts/me").
		Handler(handleIdentity(http.HandlerFunc(api.ServeAccountMe)))
	r.NewRoute().
		Methods("GET").
		Path("/api/accounts/by_handle").
		Handler(handleIdentity(http.HandlerFunc(api.ServeAccountByHandle)))
	r.NewRoute().
		Methods("GET").
		Path("/api/accounts/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.ServeAccount)))
	r.NewRoute().
		Methods("PUT").
		Path("/api/accounts/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.UpdateAccount)))
	r.NewRoute().
		Methods("PATCH").
		Path("/api/accounts/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.UpdateAccountPart)))
	r.NewRoute().
		Methods("DELETE").
		Path("/api/accounts/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.DeleteAccount)))

	r.NewRoute().
		Methods("GET").
		Path("/api/contests/1").
		Handler(handleIdentity(http.HandlerFunc(api.ServeContest)))
	r.NewRoute().
		Methods("PUT").
		Path("/api/contests/1").
		Handler(handleIdentity(http.HandlerFunc(api.UpdateContest)))

	r.NewRoute().
		Methods("GET").
		Path("/api/standings").
		Handler(handleIdentity(http.HandlerFunc(api.ServeStandingList)))

	r.NewRoute().
		Methods("GET").
		Path("/api/clarifications").
		Handler(handleIdentity(http.HandlerFunc(api.ServeClarificationList)))
	r.NewRoute().
		Methods("POST").
		Path("/api/clarifications").
		Handler(handleIdentity(http.HandlerFunc(api.CreateClarification)))
	r.NewRoute().
		Methods("GET").
		Path("/api/clarifications/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.ServeClarification)))
	r.NewRoute().
		Methods("PUT").
		Path("/api/clarifications/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.UpdateClarification)))
	r.NewRoute().
		Methods("DELETE").
		Path("/api/clarifications/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.DeleteClarification)))

	r.NewRoute().
		Methods("GET").
		Path("/api/problems").
		Handler(handleIdentity(http.HandlerFunc(api.ServeProblemList)))
	r.NewRoute().
		Methods("POST").
		Path("/api/problems").
		Handler(handleIdentity(http.HandlerFunc(api.CreateProblem)))
	r.NewRoute().
		Methods("GET").
		Path("/api/problems/by_slug").
		Handler(handleIdentity(http.HandlerFunc(api.ServeProblemBySlug)))
	r.NewRoute().
		Methods("GET").
		Path("/api/problems/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.ServeProblem)))
	r.NewRoute().
		Methods("PUT").
		Path("/api/problems/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.UpdateProblem)))
	r.NewRoute().
		Methods("DELETE").
		Path("/api/problems/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.DeleteProblem)))
	r.NewRoute().
		Methods("GET").
		Path("/api/problems/{id}/tests/{no}/answer").
		Handler(handleIdentity(http.HandlerFunc(api.ServeProblemTestAnswer)))

	r.NewRoute().
		Methods("GET").
		Path("/api/submissions").
		Handler(handleIdentity(http.HandlerFunc(api.ServeSubmissionList)))
	r.NewRoute().
		Methods("POST").
		Path("/api/submissions").
		Handler(handleIdentity(http.HandlerFunc(api.CreateSubmission)))
	r.NewRoute().
		Methods("GET").
		Path("/api/submissions/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.ServeSubmission)))
	r.NewRoute().
		Methods("PUT").
		Path("/api/submissions/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.UpdateSubmission)))
	r.NewRoute().
		Methods("GET").
		Path("/api/submissions/{id}/source").
		Handler(handleIdentity(http.HandlerFunc(api.ServeSubmissionSource)))
	r.NewRoute().
		Methods("GET").
		Path("/api/submissions/{id}/tests/{no}/output").
		Handler(handleIdentity(http.HandlerFunc(api.ServeSubmissionTestOutput)))
	r.NewRoute().
		Methods("POST").
		Path("/api/submissions/{id}/reset").
		Handler(handleIdentity(http.HandlerFunc(api.ResetSubmission)))
	r.NewRoute().
		Methods("POST").
		Path("/api/submissions/{id}/judge").
		Handler(handleIdentity(http.HandlerFunc(api.JudgeSubmission)))

	r.NewRoute().
		Methods("POST").
		Path("/api/executions").
		Handler(handleIdentity(http.HandlerFunc(api.CreateExecution)))
	r.NewRoute().
		Methods("GET").
		Path("/api/executions/{id}").
		Handler(handleIdentity(http.HandlerFunc(api.ServeExecution)))
	r.NewRoute().
		Methods("POST").
		Path("/api/executions/{id}/apply").
		Handler(handleIdentity(http.HandlerFunc(api.ApplyExecution)))
	r.NewRoute().
		Methods("GET").
		Path("/api/executions/{id}/tests/{no}/output").
		Handler(handleIdentity(http.HandlerFunc(api.ServeExecutionTestOutput)))

	r.NewRoute().
		Methods("GET").
		Path("/api/activities").
		Handler(handleIdentity(http.HandlerFunc(api.ServeActivityList)))

	r.NewRoute().
		Methods("GET").
		Path("/api/notifications").
		Handler(handleIdentity(http.HandlerFunc(api.ServeNotificationList)))

	r.NewRoute().
		Methods("GET").
		PathPrefix("/api").
		Handler(http.NotFoundHandler())

	r.NewRoute().
		Methods("GET").
		PathPrefix("/assets").
		Handler(http.StripPrefix("/assets", http.HandlerFunc(ui.ServeAsset)))
	r.NewRoute().
		Methods("GET").
		PathPrefix("/").
		Handler(handleIdentity(http.HandlerFunc(ui.ServeIndex)))

	http.Handle("/hub", handlePanic(handleIdentity(http.HandlerFunc(hub.HandleConnect))))
	http.Handle("/", handlePanic(http.TimeoutHandler(handlers.CompressHandler(r), 8*time.Second, "")))
}
Пример #28
0
func main() {

	z := flag.String("z", "", "zipper")
	port := flag.Int("p", 8080, "port")
	l := flag.Int("l", 20, "concurrency limit")
	cacheType := flag.String("cache", "mem", "cache type to use")
	mc := flag.String("mc", "", "comma separated memcached server list")
	memsize := flag.Int("memsize", 0, "in-memory cache size in MB (0 is unlimited)")
	cpus := flag.Int("cpus", 0, "number of CPUs to use")
	tz := flag.String("tz", "", "timezone,offset to use for dates with no timezone")
	graphiteHost := flag.String("graphite", "", "graphite destination host")
	logdir := flag.String("logdir", "/var/log/carbonapi/", "logging directory")
	logtostdout := flag.Bool("stdout", false, "log also to stdout")
	interval := flag.Duration("i", 60*time.Second, "interval to report internal statistics to graphite")
	idleconns := flag.Int("idleconns", 10, "max idle connections")
	pidFile := flag.String("pid", "", "pidfile (default: empty, don't create pidfile)")

	flag.Parse()

	if *logdir == "" {
		mlog.SetRawStream(os.Stdout)
	} else {
		mlog.SetOutput(*logdir, "carbonapi", *logtostdout)
	}

	expvar.NewString("BuildVersion").Set(BuildVersion)
	logger.Logln("starting carbonapi", BuildVersion)

	if p := os.Getenv("PORT"); p != "" {
		*port, _ = strconv.Atoi(p)
	}

	Limiter = newLimiter(*l)

	if *z == "" {
		logger.Fatalln("no zipper provided")
	}

	if _, err := url.Parse(*z); err != nil {
		logger.Fatalln("unable to parze zipper:", err)
	}

	logger.Logln("using zipper", *z)
	Zipper = zipper{
		z: *z,
		client: &http.Client{
			Transport: &http.Transport{
				MaxIdleConnsPerHost: *idleconns,
			},
		},
	}

	switch *cacheType {
	case "memcache":
		if *mc == "" {
			logger.Fatalln("memcache cache requested but no memcache servers provided")
		}

		servers := strings.Split(*mc, ",")
		logger.Logln("using memcache servers:", servers)
		queryCache = &memcachedCache{client: memcache.New(servers...)}
		findCache = &memcachedCache{client: memcache.New(servers...)}

	case "mem":
		qcache := &expireCache{ec: ecache.New(uint64(*memsize * 1024 * 1024))}
		queryCache = qcache
		go queryCache.(*expireCache).ec.ApproximateCleaner(10 * time.Second)

		findCache = &expireCache{ec: ecache.New(0)}
		go findCache.(*expireCache).ec.ApproximateCleaner(10 * time.Second)

		Metrics.CacheSize = expvar.Func(func() interface{} {
			return qcache.ec.Size()
		})
		expvar.Publish("cache_size", Metrics.CacheSize)

		Metrics.CacheItems = expvar.Func(func() interface{} {
			return qcache.ec.Items()
		})
		expvar.Publish("cache_items", Metrics.CacheItems)

	case "null":
		queryCache = &nullCache{}
		findCache = &nullCache{}
	}

	if *tz != "" {
		fields := strings.Split(*tz, ",")
		if len(fields) != 2 {
			logger.Fatalf("expected two fields for tz,seconds, got %d", len(fields))
		}

		var err error
		offs, err := strconv.Atoi(fields[1])
		if err != nil {
			logger.Fatalf("unable to parse seconds: %s: %s", fields[1], err)
		}

		defaultTimeZone = time.FixedZone(fields[0], offs)
		logger.Logf("using fixed timezone %s, offset %d ", defaultTimeZone.String(), offs)
	}

	if *cpus != 0 {
		logger.Logln("using GOMAXPROCS", *cpus)
		runtime.GOMAXPROCS(*cpus)
	}

	if envhost := os.Getenv("GRAPHITEHOST") + ":" + os.Getenv("GRAPHITEPORT"); envhost != ":" || *graphiteHost != "" {

		var host string

		switch {
		case envhost != ":" && *graphiteHost != "":
			host = *graphiteHost
		case envhost != ":":
			host = envhost
		case *graphiteHost != "":
			host = *graphiteHost
		}

		logger.Logln("Using graphite host", host)

		logger.Logln("setting stats interval to", *interval)

		// register our metrics with graphite
		graphite := g2g.NewGraphite(host, *interval, 10*time.Second)

		hostname, _ := os.Hostname()
		hostname = strings.Replace(hostname, ".", "_", -1)

		graphite.Register(fmt.Sprintf("carbon.api.%s.requests", hostname), Metrics.Requests)
		graphite.Register(fmt.Sprintf("carbon.api.%s.request_cache_hits", hostname), Metrics.RequestCacheHits)

		graphite.Register(fmt.Sprintf("carbon.api.%s.find_requests", hostname), Metrics.FindRequests)
		graphite.Register(fmt.Sprintf("carbon.api.%s.find_cache_hits", hostname), Metrics.FindCacheHits)

		graphite.Register(fmt.Sprintf("carbon.api.%s.render_requests", hostname), Metrics.RenderRequests)

		graphite.Register(fmt.Sprintf("carbon.api.%s.memcache_timeouts", hostname), Metrics.MemcacheTimeouts)

		if Metrics.CacheSize != nil {
			graphite.Register(fmt.Sprintf("carbon.api.%s.cache_size", hostname), Metrics.CacheSize)
			graphite.Register(fmt.Sprintf("carbon.api.%s.cache_items", hostname), Metrics.CacheItems)
		}

		go mstats.Start(*interval)

		graphite.Register(fmt.Sprintf("carbon.api.%s.alloc", hostname), &mstats.Alloc)
		graphite.Register(fmt.Sprintf("carbon.api.%s.total_alloc", hostname), &mstats.TotalAlloc)
		graphite.Register(fmt.Sprintf("carbon.api.%s.num_gc", hostname), &mstats.NumGC)
		graphite.Register(fmt.Sprintf("carbon.api.%s.pause_ns", hostname), &mstats.PauseNS)

	}

	render := func(w http.ResponseWriter, r *http.Request) {
		var stats renderStats
		t0 := time.Now()
		renderHandler(w, r, &stats)
		since := time.Since(t0)
		logger.Logln(r.RequestURI, since.Nanoseconds()/int64(time.Millisecond), stats.zipperRequests)
	}

	if *pidFile != "" {
		pidfile.SetPidfilePath(*pidFile)
		err := pidfile.Write()
		if err != nil {
			logger.Fatalln("error during pidfile.Write():", err)
		}
	}

	r := http.DefaultServeMux
	r.HandleFunc("/render/", render)
	r.HandleFunc("/render", render)

	r.HandleFunc("/metrics/find/", findHandler)
	r.HandleFunc("/metrics/find", findHandler)

	r.HandleFunc("/info/", passthroughHandler)
	r.HandleFunc("/info", passthroughHandler)

	r.HandleFunc("/lb_check", lbcheckHandler)
	r.HandleFunc("/", usageHandler)

	logger.Logln("listening on port", *port)
	handler := handlers.CompressHandler(r)
	handler = handlers.CORS()(handler)
	handler = handlers.CombinedLoggingHandler(mlog.GetOutput(), handler)

	err := gracehttp.Serve(&http.Server{
		Addr:    ":" + strconv.Itoa(*port),
		Handler: handler,
	})

	if err != nil {
		logger.Fatalln(err)
	}
}
Пример #29
0
func main() {

	// Set up command line flags.
	addr := flag.String("listen", ":3000", "IP address/port to listen on")
	cacheDir := flag.String("cache-dir", fmt.Sprintf("%s/%s", os.TempDir(), serverName), "Directory for caching static resources")
	disableCdn := flag.Bool("disable-cdn", false, "Disable the CDN functionality")
	disableProxy := flag.Bool("disable-proxy", false, "Disable the CDN functionality")
	cdnPrefix := flag.String("prefix-cdn", "/cdn/", "The prefix for the fetching CDN resources")
	proxyPrefix := flag.String("prefix-proxy", "/assets/", "The prefix for handling GET proxy requests")
	debug := flag.Bool("debug", true, "Publish stats to /debug/vars")

	flag.StringVar(&documentRoot, "docroot", "", "Document root to serve static files. Default is none (disabled)")
	flag.BoolVar(&useFileCache, "cache", true, "Enable filesystem caching")
	flag.Parse()

	tmpDir = *cacheDir

	// Make sure cache directory exists.
	// and check if we can write to file cache.
	initCacheDir()

	r := mux.NewRouter()

	// CDN proxy subrouter
	if !*disableCdn && len(*cdnPrefix) > 0 {

		cdnCacheDir := fmt.Sprintf("%s/libraries", tmpDir)
		cdnRouter := r.PathPrefix(*cdnPrefix).Subrouter()

		log.Infof("Serving cdn files from %s%s", *addr, *cdnPrefix)
		if useFileCache {
			log.Infof("Caching cdn files in %s", cdnCacheDir)
		}

		cdn := gocdn.CDN{
			Prefix:        *cdnPrefix,
			CacheDuration: 100,
			Cors:          true,
			CacheDir:      cdnCacheDir,
			UseFileCache:  useFileCache,
		}

		cdnRouter.HandleFunc(`/{package}/{version}/{path:[^?]+}`, cdn.Handler)

	}

	if !*disableProxy && len(*proxyPrefix) > 0 {
		// General proxy subrouter.
		log.Infof("Serving assets via proxy from %s%s ", *addr, *proxyPrefix)
		proxyRouter := r.PathPrefix(*proxyPrefix).Subrouter()
		proxyRouter.HandleFunc(`/{domain}/{path:.+(jpe?g|png|gif|webp|tiff|bmp)}`, respRemoteImgHandler)
		proxyRouter.HandleFunc(`/{domain}/{path:[^?]+}`, proxyHandler)
	}

	if documentRoot != "" {
		log.Infof("Serving static files from %s", documentRoot)
		r.HandleFunc(`/{.+(jpe?g|png|gif|webp|tiff|bmp)}`, respLocalImgHandler)
		r.PathPrefix("/").Handler(http.FileServer(http.Dir(documentRoot)))
		http.Handle("/", r)
	}

	if *debug {
		log.Infof("Publishing stats to /debug/vars")
		r.Handle("/debug/vars", http.DefaultServeMux)
	}

	log.Infof("Ready to listen on %s", *addr)
	log.Errorf("Server died: %s", http.ListenAndServe(*addr, handlers.CompressHandler(r)))

}