| 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 } |
| 63 | select { |
| 64 | case s.connectionOpsLock<-struct{}{}: defer func() { <-s.connectionOpsLock }(); break |
| 65 | case <-time.NewTimer( time.Second ).C: http.Error( writer, http.StatusText( http.StatusConflict ), http.StatusConflict ); return |
| 66 | } |
| 67 | |
| 68 | switch code := s.connection.Code(); code { |
| 69 | case connection.Clean: |
| 70 | if err := s.connection.Init(); err != nil { writer.Write( Result{ Error: &Error{ Code: CodeRoute, Message: err.Error() } }.Json() ); return } |
| 71 | writer.Write( Result{ Result: s.connection.State() }.Json() ) |
| 72 | default: |
| 73 | writer.Write( Result{ Error: &Error{ Code: CodeRoute, Message: "bad state: " + code } }.Json() ) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func ( s *Server ) connect( writer http.ResponseWriter, request *http.Request ) { |
| 78 | if request.Method != "GET" { http.Error( writer, http.StatusText( http.StatusNotFound ), http.StatusNotFound ); return } |