route_preinit.go 663 B

123456789101112131415161718192021222324252627282930313233343536
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package server
  5. import (
  6. "errors"
  7. "apiote.xyz/p/szczanieckiej/config"
  8. "log"
  9. "net/http"
  10. "os"
  11. )
  12. func RoutePreinit(cfg config.Config) *http.Server {
  13. srv := &http.Server{Addr: cfg.ListenAddress}
  14. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  15. se := ServerError{
  16. code: http.StatusServiceUnavailable,
  17. err: errors.New("not yet initialised"),
  18. }
  19. sendError(w, r, se)
  20. })
  21. go func() {
  22. if err := srv.ListenAndServe(); err != http.ErrServerClosed {
  23. log.Printf("Preinit ListenAndServe(): %v", err)
  24. os.Exit(1)
  25. }
  26. }()
  27. return srv
  28. }