( writer http.ResponseWriter, request *http.Request )
| 28 | ) |
| 29 | |
| 30 | func ( s *Server ) configuration( writer http.ResponseWriter, request *http.Request ) { |
| 31 | select { |
| 32 | case s.connectionOpsLock<-struct{}{}: defer func() { <-s.connectionOpsLock }(); break |
| 33 | case <-time.NewTimer( time.Second ).C: http.Error( writer, http.StatusText( http.StatusConflict ), http.StatusConflict ); return |
| 34 | } |
| 35 | switch request.Method { |
| 36 | case "GET": |
| 37 | writer.WriteHeader( http.StatusOK ) |
| 38 | encoder := json.NewEncoder( writer ) |
| 39 | s.connection.Lock(); defer s.connection.Unlock() |
| 40 | if err := encoder.Encode( s.connection.Config ); err != nil { log.Println( "Serv: [ERR] Configure failed: ", err ); return } |
| 41 | log.Println( "Serv: Configuration sent to", request.RemoteAddr ) |
| 42 | s.connection.StateNotify( &connection.State{ Code: connection.ConfigurationGet, Timestamp: time.Now() } ) |
| 43 | case "POST": |
| 44 | logBuffer := &bytes.Buffer{} |
| 45 | decoder := json.NewDecoder( io.TeeReader( io.LimitReader( request.Body, 8192 ), logBuffer ) ) |
| 46 | s.connection.Lock(); defer s.connection.Unlock() |
| 47 | if err := decoder.Decode( s.connection.Config ); err != nil { |
| 48 | log.Println( "Serv: [ERR] Configure failed:", err ) |
| 49 | writer.WriteHeader( http.StatusBadRequest ) |
| 50 | writer.Write( Result{ Error: &Error{ Code: CodeConfig, Message: err.Error() } }.Json() ) |
| 51 | return |
| 52 | } |
| 53 | log.Println( "Serv: Configured from", request.RemoteAddr, "with", logBuffer.String() ) |
| 54 | writer.WriteHeader( http.StatusOK ) |
| 55 | writer.Write( Result{ Result: true }.Json() ) |
| 56 | s.connection.StateNotify( &connection.State{ Code: connection.ConfigurationSet, Timestamp: time.Now()} ) |
| 57 | default: http.Error( writer, "not found", http.StatusNotFound ) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func ( s *Server ) route( writer http.ResponseWriter, request *http.Request ) { |
| 62 | if request.Method != "GET" { http.Error( writer, http.StatusText( http.StatusNotFound ), http.StatusNotFound ); return } |
nothing calls this directly
no test coverage detected