Documentation ¶
Overview ¶
Package server - HTTP Server.
Example ¶
ctx, cancel := context.WithCancel(context.Background()) mux := http.NewServeMux() mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { var response = map[string]interface{}{ "key": "value", } w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) }) // Add Response Writer handler := writer.Handle(mux) // Start the HTTP server slog.Info("Starting Server ...", slog.String("local", fmt.Sprintf("http://localhost:%s", "8080"))) api := server.Server(ctx, handler, "8080") // Issue Cancellation Handler server.Interrupt(ctx, cancel, api) // <-- Blocking if e := api.ListenAndServe(); e != nil && !(errors.Is(e, http.ErrServerClosed)) { slog.ErrorContext(ctx, "Error During Server's Listen & Serve Call ...", slog.String("error", e.Error())) os.Exit(100) } // --> Exit { slog.InfoContext(ctx, "Graceful Shutdown Complete") // Waiter <-ctx.Done() }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Health http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { response := map[string]string{ "status": "ok", } w.Header().Set("Content-Type", "Application/JSON") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(response) return }
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.