summaryrefslogtreecommitdiff
path: root/server/server.go
blob: 92f590cc94f78fe25ed22bc94f8bb3ccacafa94d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2022 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package server

import (
	"context"
	"errors"
	"log/slog"
	"net"
	"net/http"
	"time"
)

// Server is a general purpose HTTP server Runnable for a manager.
type Server struct {
	// Name is an optional string that describes the purpose of the server. It is used in logs to distinguish
	// among multiple servers.
	Name string

	// Server is the HTTP server to run. It is required.
	Server *http.Server

	// Listener is an optional listener to use. If not set, the server start a listener using the server.Addr.
	// Using a listener is useful when the port reservation needs to happen in advance of this runnable starting.
	Listener net.Listener

	// ShutdownTimeout is an optional duration that indicates how long to wait for the server to shutdown gracefully. If not set,
	// the server will wait indefinitely for all connections to close.
	ShutdownTimeout *time.Duration
}

// Start starts the server. It will block until the server is stopped or an error occurs.
func (s *Server) Start(ctx context.Context) error {
	serverShutdown := make(chan struct{})

	logger := slog.With("addr", s.addr())
	if s.Name != "" {
		logger = logger.With("name", s.Name)
	}

	go func() {
		<-ctx.Done()
		logger.Info("shutting down server")
		shutdownCtx := context.Background()
		if s.ShutdownTimeout != nil {
			var shutdownCancel context.CancelFunc
			shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), *s.ShutdownTimeout)
			defer shutdownCancel()
		}

		if err := s.Server.Shutdown(shutdownCtx); err != nil {
			logger.Error("error shutting down server", "error", err)
		}
		close(serverShutdown)
	}()

	logger.Info("starting server")
	if err := s.serve(); err != nil && !errors.Is(err, http.ErrServerClosed) {
		return err
	}

	<-serverShutdown
	return nil
}

func (s *Server) addr() string {
	if s.Listener != nil {
		return s.Listener.Addr().String()
	}

	return s.Server.Addr
}

func (s *Server) serve() error {
	if s.Listener != nil {
		return s.Server.Serve(s.Listener)
	}

	return s.Server.ListenAndServe()
}