You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by el...@apache.org on 2017/01/11 21:34:27 UTC

[3/8] incubator-trafficcontrol git commit: add config option for static http and js file

add config option for static http and js file

(cherry picked from commit 27b7bca521354b6655b1e5a10772e46deb42cced)


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/fcf26424
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/fcf26424
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/fcf26424

Branch: refs/heads/master
Commit: fcf264245eeff033b8ea4062dabeaa13c5cfe81f
Parents: c544779
Author: David Neuman <da...@gmail.com>
Authored: Mon Jan 9 14:00:00 2017 -0700
Committer: David Neuman <da...@gmail.com>
Committed: Wed Jan 11 16:01:41 2017 +0000

----------------------------------------------------------------------
 .../experimental/conf/traffic_monitor.cfg       |  3 +-
 .../traffic_monitor/config/config.go            |  4 +++
 .../traffic_monitor/manager/opsconfig.go        |  2 +-
 .../traffic_monitor/srvhttp/srvhttp.go          | 33 +++++++++++++-------
 4 files changed, 28 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/fcf26424/traffic_monitor/experimental/conf/traffic_monitor.cfg
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/conf/traffic_monitor.cfg b/traffic_monitor/experimental/conf/traffic_monitor.cfg
index a463682..f2c25a0 100644
--- a/traffic_monitor/experimental/conf/traffic_monitor.cfg
+++ b/traffic_monitor/experimental/conf/traffic_monitor.cfg
@@ -15,5 +15,6 @@
 	"log_location_debug": "null",
 	"serve_read_timeout_ms": 10000,
 	"serve_write_timeout_ms": 10000,
-	"http_poll_no_sleep": false
+	"http_poll_no_sleep": false,
+	"static_file_dir": "/opt/traffic_monitor/static/"
 }

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/fcf26424/traffic_monitor/experimental/traffic_monitor/config/config.go
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/traffic_monitor/config/config.go b/traffic_monitor/experimental/traffic_monitor/config/config.go
index b7bb0e2..24ba42a 100644
--- a/traffic_monitor/experimental/traffic_monitor/config/config.go
+++ b/traffic_monitor/experimental/traffic_monitor/config/config.go
@@ -35,6 +35,8 @@ const (
 	LogLocationStderr = "stderr"
 	// LogLocationNull indicates the null IO stream (/dev/null)
 	LogLocationNull = "null"
+	//StaticFileDir is the directory that contains static html and js files.
+	StaticFileDir = "/opt/traffic_monitor/static/"
 )
 
 // Config is the configuration for the application. It includes myriad data, such as polling intervals and log locations.
@@ -57,6 +59,7 @@ type Config struct {
 	ServeWriteTimeout            time.Duration `json:"-"`
 	HealthToStatRatio            uint64        `json:"health_to_stat_ratio"`
 	HTTPPollNoSleep              bool          `json:"http_poll_no_sleep"`
+	StaticFileDir                string        `json:"static_file_dir"`
 }
 
 // DefaultConfig is the default configuration for the application, if no configuration file is given, or if a given config setting doesn't exist in the config file.
@@ -79,6 +82,7 @@ var DefaultConfig = Config{
 	ServeWriteTimeout:            10 * time.Second,
 	HealthToStatRatio:            4,
 	HTTPPollNoSleep:              false,
+	StaticFileDir:                StaticFileDir,
 }
 
 // MarshalJSON marshals custom millisecond durations. Aliasing inspired by http://choly.ca/post/go-json-marshalling/

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/fcf26424/traffic_monitor/experimental/traffic_monitor/manager/opsconfig.go
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/traffic_monitor/manager/opsconfig.go b/traffic_monitor/experimental/traffic_monitor/manager/opsconfig.go
index 81906ed..b57a613 100644
--- a/traffic_monitor/experimental/traffic_monitor/manager/opsconfig.go
+++ b/traffic_monitor/experimental/traffic_monitor/manager/opsconfig.go
@@ -148,7 +148,7 @@ func StartOpsConfigManager(
 				unpolledCaches,
 				monitorConfig,
 			)
-			err = httpServer.Run(endpoints, listenAddress, cfg.ServeReadTimeout, cfg.ServeWriteTimeout)
+			err = httpServer.Run(endpoints, listenAddress, cfg.ServeReadTimeout, cfg.ServeWriteTimeout, cfg.StaticFileDir)
 			if err != nil {
 				handleErr(fmt.Errorf("MonitorConfigPoller: error creating HTTP server: %s\n", err))
 				continue

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/fcf26424/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go b/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
index 564f5b3..09a5194 100644
--- a/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
+++ b/traffic_monitor/experimental/traffic_monitor/srvhttp/srvhttp.go
@@ -21,14 +21,16 @@ package srvhttp
 
 import (
 	"fmt"
-	"github.com/apache/incubator-trafficcontrol/traffic_monitor/experimental/common/log"
-	"github.com/hydrogen18/stoppableListener"
 	"io/ioutil"
 	"net"
 	"net/http"
 	"net/url"
+	"strings"
 	"sync"
 	"time"
+
+	"github.com/apache/incubator-trafficcontrol/traffic_monitor/experimental/common/log"
+	"github.com/hydrogen18/stoppableListener"
 )
 
 // GetCommonAPIData calculates and returns API data common to most endpoints
@@ -53,12 +55,12 @@ type Server struct {
 	stoppableListenerWaitGroup sync.WaitGroup
 }
 
-func (s Server) registerEndpoints(sm *http.ServeMux, endpoints map[string]http.HandlerFunc) error {
-	handleRoot, err := s.handleRootFunc()
+func (s *Server) registerEndpoints(sm *http.ServeMux, endpoints map[string]http.HandlerFunc, staticFileDir string) error {
+	handleRoot, err := s.handleRootFunc(staticFileDir)
 	if err != nil {
 		return fmt.Errorf("Error getting root endpoint: %v", err)
 	}
-	handleSortableJs, err := s.handleSortableFunc()
+	handleSortableJs, err := s.handleSortableFunc(staticFileDir)
 	if err != nil {
 		return fmt.Errorf("Error getting sortable endpoint: %v", err)
 	}
@@ -76,7 +78,7 @@ func (s Server) registerEndpoints(sm *http.ServeMux, endpoints map[string]http.H
 // Run runs a new HTTP service at the given addr, making data requests to the given c.
 // Run may be called repeatedly, and each time, will shut down any existing service first.
 // Run is NOT threadsafe, and MUST NOT be called concurrently by multiple goroutines.
-func (s Server) Run(endpoints map[string]http.HandlerFunc, addr string, readTimeout time.Duration, writeTimeout time.Duration) error {
+func (s *Server) Run(endpoints map[string]http.HandlerFunc, addr string, readTimeout time.Duration, writeTimeout time.Duration, staticFileDir string) error {
 	if s.stoppableListener != nil {
 		log.Infof("Stopping Web Server\n")
 		s.stoppableListener.Stop()
@@ -94,7 +96,7 @@ func (s Server) Run(endpoints map[string]http.HandlerFunc, addr string, readTime
 	}
 
 	sm := http.NewServeMux()
-	err = s.registerEndpoints(sm, endpoints)
+	err = s.registerEndpoints(sm, endpoints, staticFileDir)
 	if err != nil {
 		return err
 	}
@@ -134,6 +136,7 @@ func ParametersStr(params url.Values) string {
 	return pp
 }
 
+//CommonAPIDataDataFormat is a common Date format for the API
 const CommonAPIDataDateFormat = "Mon Jan 02 15:04:05 UTC 2006"
 
 // DateStr returns the given time in the format expected by Traffic Monitor 1.0 API users
@@ -141,15 +144,21 @@ func DateStr(t time.Time) string {
 	return t.UTC().Format(CommonAPIDataDateFormat)
 }
 
-func (s Server) handleRootFunc() (http.HandlerFunc, error) {
-	return s.handleFile("index.html")
+func (s *Server) handleRootFunc(staticFileDir string) (http.HandlerFunc, error) {
+	if strings.HasSuffix(staticFileDir, "/") {
+		return s.handleFile(staticFileDir + "index.html")
+	}
+	return s.handleFile(staticFileDir + "/index.html")
 }
 
-func (s Server) handleSortableFunc() (http.HandlerFunc, error) {
-	return s.handleFile("sorttable.js")
+func (s *Server) handleSortableFunc(staticFileDir string) (http.HandlerFunc, error) {
+	if strings.HasSuffix(staticFileDir, "/") {
+		return s.handleFile(staticFileDir + "sorttable.js")
+	}
+	return s.handleFile(staticFileDir + "/sorttable.js")
 }
 
-func (s Server) handleFile(name string) (http.HandlerFunc, error) {
+func (s *Server) handleFile(name string) (http.HandlerFunc, error) {
 	bytes, err := ioutil.ReadFile(name)
 	if err != nil {
 		return nil, err