Browse Source

Various haxery

Jakob Borg 8 years ago
parent
commit
c3c6ff0093
1 changed files with 24 additions and 10 deletions
  1. 24 10
      cmd/ursrv/main.go

+ 24 - 10
cmd/ursrv/main.go

@@ -26,6 +26,7 @@ import (
 
 
 var (
 var (
 	useHTTP    = os.Getenv("UR_USE_HTTP") != ""
 	useHTTP    = os.Getenv("UR_USE_HTTP") != ""
+	debug      = os.Getenv("UR_DEBUG") != ""
 	keyFile    = getEnvDefault("UR_KEY_FILE", "key.pem")
 	keyFile    = getEnvDefault("UR_KEY_FILE", "key.pem")
 	certFile   = getEnvDefault("UR_CRT_FILE", "crt.pem")
 	certFile   = getEnvDefault("UR_CRT_FILE", "crt.pem")
 	dbConn     = getEnvDefault("UR_DB_URL", "postgres://user:password@localhost/ur?sslmode=disable")
 	dbConn     = getEnvDefault("UR_DB_URL", "postgres://user:password@localhost/ur?sslmode=disable")
@@ -58,17 +59,13 @@ func (p *IntMap) Scan(src interface{}) error {
 		return errors.New("Type assertion .([]byte) failed.")
 		return errors.New("Type assertion .([]byte) failed.")
 	}
 	}
 
 
-	var i interface{}
+	var i map[string]int
 	err := json.Unmarshal(source, &i)
 	err := json.Unmarshal(source, &i)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 
 
-	*p, ok = i.(map[string]int)
-	if !ok {
-		return errors.New("Type assertion .(map[string]int) failed.")
-	}
-
+	*p = i
 	return nil
 	return nil
 }
 }
 
 
@@ -215,6 +212,15 @@ func (r *report) Validate() error {
 	if len(r.Date) != 8 {
 	if len(r.Date) != 8 {
 		return fmt.Errorf("date not initialized")
 		return fmt.Errorf("date not initialized")
 	}
 	}
+
+	// Some fields may not be null.
+	if r.RescanIntvs == nil {
+		r.RescanIntvs = []int64{}
+	}
+	if r.FolderUsesV3.FsWatcherDelays == nil {
+		r.FolderUsesV3.FsWatcherDelays = []int64{}
+	}
+
 	return nil
 	return nil
 }
 }
 
 
@@ -694,22 +700,30 @@ func newDataHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
 	rep.Date = time.Now().UTC().Format("20060102")
 	rep.Date = time.Now().UTC().Format("20060102")
 
 
 	lr := &io.LimitedReader{R: r.Body, N: 40 * 1024}
 	lr := &io.LimitedReader{R: r.Body, N: 40 * 1024}
-	if err := json.NewDecoder(lr).Decode(&rep); err != nil {
-		log.Println("json decode:", err)
+	bs, _ := ioutil.ReadAll(lr)
+	if err := json.Unmarshal(bs, &rep); err != nil {
+		log.Println("decode:", err)
+		if debug {
+			log.Printf("%s", bs)
+		}
 		http.Error(w, "JSON Decode Error", http.StatusInternalServerError)
 		http.Error(w, "JSON Decode Error", http.StatusInternalServerError)
 		return
 		return
 	}
 	}
 
 
 	if err := rep.Validate(); err != nil {
 	if err := rep.Validate(); err != nil {
 		log.Println("validate:", err)
 		log.Println("validate:", err)
-		log.Printf("%#v", rep)
+		if debug {
+			log.Printf("%#v", rep)
+		}
 		http.Error(w, "Validation Error", http.StatusInternalServerError)
 		http.Error(w, "Validation Error", http.StatusInternalServerError)
 		return
 		return
 	}
 	}
 
 
 	if err := insertReport(db, rep); err != nil {
 	if err := insertReport(db, rep); err != nil {
 		log.Println("insert:", err)
 		log.Println("insert:", err)
-		log.Printf("%#v", rep)
+		if debug {
+			log.Printf("%#v", rep)
+		}
 		http.Error(w, "Database Error", http.StatusInternalServerError)
 		http.Error(w, "Database Error", http.StatusInternalServerError)
 		return
 		return
 	}
 	}