瀏覽代碼

Added log function for subservice

TC 3 年之前
父節點
當前提交
25c22633f3
共有 1 個文件被更改,包括 15 次插入13 次删除
  1. 15 13
      mod/subservice/subservice.go

+ 15 - 13
mod/subservice/subservice.go

@@ -68,7 +68,7 @@ func NewSubServiceRouter(ReservePaths []string, basePort int, userHandler *user.
 	}
 }
 
-//Load and start all the subservices inside this rootpath
+// Load and start all the subservices inside this rootpath
 func (sr *SubServiceRouter) LoadSubservicesFromRootPath(rootpath string) {
 	scanningPath := filepath.ToSlash(filepath.Clean(rootpath)) + "/*"
 
@@ -167,7 +167,7 @@ func (sr *SubServiceRouter) Launch(servicePath string, startupMode bool) error {
 		infocmd := exec.Command(servicePath+"/"+binaryExecPath, "-info")
 		launchConfig, err := infocmd.CombinedOutput()
 		if err != nil {
-			log.Println("*Subservice* startup flag -info return no JSON string and moduleInfo.json does not exists.")
+			sr.logger.PrintAndLog("Subservice", "Missing module startup info for "+servicePath, errors.New("Startup flag -info return no JSON string and moduleInfo.json does not exists for "+servicePath))
 			if startupMode {
 				log.Fatal("Unable to start service: "+binaryname, err)
 			} else {
@@ -232,7 +232,7 @@ func (sr *SubServiceRouter) Launch(servicePath string, startupMode bool) error {
 			ServiceDir: serviceRoot,
 			Process:    cmd,
 		}
-		log.Println("[Subservice] Starting service " + serviceRoot + " in compatibility mode.")
+		sr.logger.PrintAndLog("Subservice", "Starting service "+serviceRoot+" in compatibility mode", nil)
 	} else {
 		//Create a proxy for this service
 		//Get proxy endpoint from startDir dir
@@ -297,6 +297,8 @@ func (sr *SubServiceRouter) Launch(servicePath string, startupMode bool) error {
 			Process:    cmd,
 		}
 
+		sr.logger.PrintAndLog("Subservice", "Subservice Registered: "+thisModuleInfo.Name, nil)
+
 		//Create a new proxy object
 		path, _ := url.Parse("http://localhost:" + intToString(thisServicePort))
 		proxy := reverseproxy.NewReverseProxy(path)
@@ -361,12 +363,12 @@ func (sr *SubServiceRouter) HandleListing(w http.ResponseWriter, r *http.Request
 		Disabled: disabled,
 	})
 	if err != nil {
-		log.Println(err)
+		sr.logger.PrintAndLog("Subservice", "Unable to list subservice folder", err)
 	}
 	sendJSONResponse(w, string(jsonString))
 }
 
-//Kill the subservice that is currently running
+// Kill the subservice that is currently running
 func (sr *SubServiceRouter) HandleKillSubService(w http.ResponseWriter, r *http.Request) {
 	userinfo, _ := sr.userHandler.GetUserInfoFromRequest(w, r)
 	//Require admin permission
@@ -408,13 +410,13 @@ func (sr *SubServiceRouter) HandleStartSubService(w http.ResponseWriter, r *http
 
 }
 
-//Check if the user has permission to access such proxy module
+// Check if the user has permission to access such proxy module
 func (sr *SubServiceRouter) CheckUserPermissionOnSubservice(ss *SubService, u *user.User) bool {
 	moduleName := ss.Info.Name
 	return u.GetModuleAccessPermission(moduleName)
 }
 
-//Check if the target is reverse proxy. If yes, return the proxy handler and the rewritten url in string
+// Check if the target is reverse proxy. If yes, return the proxy handler and the rewritten url in string
 func (sr *SubServiceRouter) CheckIfReverseProxyPath(r *http.Request) (bool, *reverseproxy.ReverseProxy, string, *SubService) {
 	requestURL := r.URL.Path
 
@@ -534,7 +536,7 @@ func (sr *SubServiceRouter) StartSubService(serviceDir string) error {
 	return nil
 }
 
-//Get a list of subservice roots in realpath
+// Get a list of subservice roots in realpath
 func (sr *SubServiceRouter) GetSubserviceRoot() []string {
 	subserviceRoots := []string{}
 	for _, subService := range sr.RunningSubService {
@@ -544,7 +546,7 @@ func (sr *SubServiceRouter) GetSubserviceRoot() []string {
 	return subserviceRoots
 }
 
-//Scan and get the next avaible port for subservice from its basePort
+// Scan and get the next avaible port for subservice from its basePort
 func (sr *SubServiceRouter) GetNextUsablePort() int {
 	basePort := sr.BasePort
 	for sr.CheckIfPortInUse(basePort) {
@@ -589,21 +591,21 @@ func (sr *SubServiceRouter) HandleRoutingRequest(w http.ResponseWriter, r *http.
 	if err != nil {
 		//Check if it is cancelling events.
 		if !strings.Contains(err.Error(), "cancel") {
-			log.Println(subserviceObject.Info.Name + " IS NOT RESPONDING!")
+			sr.logger.PrintAndLog("Subservice", subserviceObject.Info.Name+" IS NOT RESPONDING!", err)
 			sr.RestartSubService(subserviceObject)
 		}
 
 	}
 }
 
-//Handle fail start over when the remote target is not responding
+// Handle fail start over when the remote target is not responding
 func (sr *SubServiceRouter) RestartSubService(ss *SubService) {
 	go func(ss *SubService) {
 		//Kill the original subservice
 		sr.KillSubService(ss.ServiceDir)
-		log.Println("RESTARTING SUBSERVICE " + ss.Info.Name + " IN 10 SECOUNDS")
+		sr.logger.PrintAndLog("Subservice", "RESTARTING SUBSERVICE "+ss.Info.Name+" IN 10 SECOUNDS", nil)
 		time.Sleep(10000 * time.Millisecond)
 		sr.StartSubService(ss.ServiceDir)
-		log.Println("SUBSERVICE " + ss.Info.Name + " RESTARTED")
+		sr.logger.PrintAndLog("Subservice", "SUBSERVICE "+ss.Info.Name+" RESTARTED", nil)
 	}(ss)
 }