85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
//go:build e2e
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"lincodex/backend/internal/adapter/secondary/mail/resend/resendfixture"
|
|
)
|
|
|
|
const listenAddress = ":8025"
|
|
|
|
func main() {
|
|
if len(os.Args) == 2 && os.Args[1] == "healthcheck" {
|
|
if err := healthcheck(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
fixture := resendfixture.NewService()
|
|
server := &http.Server{
|
|
Addr: listenAddress,
|
|
Handler: fixture.Handler(),
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
IdleTimeout: 30 * time.Second,
|
|
MaxHeaderBytes: 16 << 10,
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
serverErrors := make(chan error, 1)
|
|
go func() {
|
|
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
|
serverErrors <- errors.New("main.run - mail fixture server stopped unexpectedly")
|
|
}
|
|
}()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
return errors.New("main.run - mail fixture shutdown failed")
|
|
}
|
|
return nil
|
|
case err := <-serverErrors:
|
|
return err
|
|
}
|
|
}
|
|
|
|
func healthcheck() error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://127.0.0.1:8025/healthz", nil)
|
|
if err != nil {
|
|
return errors.New("main.healthcheck - could not build request")
|
|
}
|
|
response, err := http.DefaultClient.Do(request)
|
|
if err != nil {
|
|
return errors.New("main.healthcheck - mail fixture is not ready")
|
|
}
|
|
defer func() { _ = response.Body.Close() }()
|
|
if response.StatusCode != http.StatusNoContent {
|
|
return errors.New("main.healthcheck - mail fixture is not ready")
|
|
}
|
|
return nil
|
|
}
|