Browse Source

Avoid deep nesting. See https://github.com/la5nta/pat/pull/163#discussion_r329475620

blockmurder 1 month ago
parent
commit
18ec5457d5
1 changed files with 22 additions and 22 deletions
  1. 22 22
      http.go

+ 22 - 22
http.go

@@ -351,35 +351,35 @@ func statusHandler(w http.ResponseWriter, req *http.Request) { json.NewEncoder(w
 
 func positionHandler(w http.ResponseWriter, req *http.Request) {
 	// Throw error if GPSd http endpoint is not enabled
-	if config.GPSd.EnableHTTP && config.GPSd.Addr != "" {
-		host, _, _ := net.SplitHostPort(req.RemoteAddr)
-		log.Printf("Location data from GPSd served to %s", host)
-
-		conn, err := gpsd.Dial(config.GPSd.Addr)
-		if err != nil {
-			// do not pass error message to response as GPSd address might be leaked
-			http.Error(w, "GPSd Dial failed", http.StatusInternalServerError)
-			return
-		}
-		defer conn.Close()
+	if !config.GPSd.EnableHTTP || config.GPSd.Addr == "" {
+		http.Error(w, "GPSd not enabled or address not set in config file", http.StatusInternalServerError)
+		return
+	}
 
-		conn.Watch(true)
+	host, _, _ := net.SplitHostPort(req.RemoteAddr)
+	log.Printf("Location data from GPSd served to %s", host)
 
-		pos, err := conn.NextPosTimeout(5 * time.Second)
-		if err != nil {
-			http.Error(w, "GPSd get next position failed: " + err.Error(), http.StatusInternalServerError)
-			return
-		}
+	conn, err := gpsd.Dial(config.GPSd.Addr)
+	if err != nil {
+		// do not pass error message to response as GPSd address might be leaked
+		http.Error(w, "GPSd Dial failed", http.StatusInternalServerError)
+		return
+	}
+	defer conn.Close()
 
-		if config.GPSd.UseServerTime {
-			pos.Time = time.Now()
-		}
+	conn.Watch(true)
 
-		json.NewEncoder(w).Encode(pos)
+	pos, err := conn.NextPosTimeout(5 * time.Second)
+	if err != nil {
+		http.Error(w, "GPSd get next position failed: " + err.Error(), http.StatusInternalServerError)
 		return
 	}
 
-	http.Error(w, "GPSd not enabled or address not set in config file", http.StatusInternalServerError)
+	if config.GPSd.UseServerTime {
+		pos.Time = time.Now()
+	}
+
+	json.NewEncoder(w).Encode(pos)
 	return
 }