2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2024-05-12 08:20:06 +00:00
|
|
|
Copyright CAcert Inc.
|
2023-07-29 15:46:33 +00:00
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
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
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
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.
|
2021-09-11 11:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-29 15:46:33 +00:00
|
|
|
"errors"
|
2024-05-12 10:02:27 +00:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/knadh/koanf"
|
|
|
|
)
|
|
|
|
|
2024-05-12 10:12:27 +00:00
|
|
|
func StartApplication(
|
|
|
|
ctx context.Context, logger *slog.Logger, server *http.Server, publicURL string, config *koanf.Koanf,
|
|
|
|
) error {
|
2021-09-11 11:37:31 +00:00
|
|
|
done := make(chan bool)
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-quit
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.Info("Server is shutting down...")
|
2021-09-11 11:37:31 +00:00
|
|
|
atomic.StoreInt32(&Healthy, 0)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
const shutdownWaitTime = 30 * time.Second
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, shutdownWaitTime)
|
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
server.SetKeepAlivesEnabled(false)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.Error("Could not gracefully shutdown the server", "error", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.Info("Server is ready to handle requests", "public_url", publicURL)
|
2021-09-11 11:37:31 +00:00
|
|
|
atomic.StoreInt32(&Healthy, 1)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if err := server.ListenAndServeTLS(
|
|
|
|
config.String("server.certificate"), config.String("server.key"),
|
2023-07-29 15:46:33 +00:00
|
|
|
); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.Error("Could not listen on requested address", "server_address", server.Addr)
|
|
|
|
|
|
|
|
return fmt.Errorf("listening failed: %w", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.Info("Server stopped")
|
|
|
|
|
|
|
|
return nil
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|