NewServer creates a new server from the given parameters
(ctx devspacecontext.Context, host string, ignoreDownloadError bool, forcePort *int, pipeline types.Pipeline)
| 35 | |
| 36 | // NewServer creates a new server from the given parameters |
| 37 | func NewServer(ctx devspacecontext.Context, host string, ignoreDownloadError bool, forcePort *int, pipeline types.Pipeline) (*Server, error) { |
| 38 | path, err := downloadUI() |
| 39 | if err != nil { |
| 40 | if !ignoreDownloadError { |
| 41 | return nil, errors.Wrap(err, "download ui") |
| 42 | } |
| 43 | |
| 44 | ctx.Log().Warnf("Couldn't download ui: %v", err) |
| 45 | } |
| 46 | |
| 47 | // Find an open port |
| 48 | usePort := DefaultPort |
| 49 | if forcePort != nil { |
| 50 | usePort = *forcePort |
| 51 | |
| 52 | if host == "localhost" { |
| 53 | available, err := port.IsAvailable(fmt.Sprintf(":%d", usePort)) |
| 54 | if !available { |
| 55 | return nil, errors.Errorf("Port %d already in use: %v", usePort, err) |
| 56 | } |
| 57 | } |
| 58 | } else { |
| 59 | if host == "localhost" { |
| 60 | for i := 0; i < 20; i++ { |
| 61 | available, _ := port.IsAvailable(fmt.Sprintf(":%d", usePort)) |
| 62 | if available { |
| 63 | break |
| 64 | } |
| 65 | |
| 66 | usePort++ |
| 67 | if i+1 == 20 { |
| 68 | return nil, err |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Create handler |
| 75 | handler, err := newHandler(ctx, path, pipeline) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | |
| 80 | return &Server{ |
| 81 | Server: &http.Server{ |
| 82 | Addr: host + ":" + strconv.Itoa(usePort), |
| 83 | Handler: handler, |
| 84 | // ReadTimeout: 5 * time.Second, |
| 85 | // WriteTimeout: 10 * time.Second, |
| 86 | // IdleTimeout: 60 * time.Second, |
| 87 | }, |
| 88 | }, nil |
| 89 | } |
| 90 | |
| 91 | // ListenAndServe implements interface |
| 92 | func (s *Server) ListenAndServe() error { |
no test coverage detected