From da6b30b1e0f651747e69c3c8c081ff348140598f Mon Sep 17 00:00:00 2001 From: tiff Date: Sun, 12 Jan 2025 20:02:17 -0500 Subject: [PATCH] Update something --- Dockerfile | 0 cmd/api/main.go | 57 ---------------------------- docker-compose.yml | 0 internal/server/routes.go | 49 ------------------------ internal/server/routes_test.go | 31 --------------- internal/server/server.go => main.go | 0 6 files changed, 137 deletions(-) create mode 100644 Dockerfile delete mode 100644 cmd/api/main.go create mode 100644 docker-compose.yml delete mode 100644 internal/server/routes.go delete mode 100644 internal/server/routes_test.go rename internal/server/server.go => main.go (100%) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/cmd/api/main.go b/cmd/api/main.go deleted file mode 100644 index 4531107..0000000 --- a/cmd/api/main.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - "net/http" - "os/signal" - "syscall" - "time" - - "goload/internal/server" -) - -func gracefulShutdown(apiServer *http.Server, done chan bool) { - // Create context that listens for the interrupt signal from the OS. - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) - defer stop() - - // Listen for the interrupt signal. - <-ctx.Done() - - log.Println("shutting down gracefully, press Ctrl+C again to force") - - // The context is used to inform the server it has 5 seconds to finish - // the request it is currently handling - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := apiServer.Shutdown(ctx); err != nil { - log.Printf("Server forced to shutdown with error: %v", err) - } - - log.Println("Server exiting") - - // Notify the main goroutine that the shutdown is complete - done <- true -} - -func main() { - - server := server.NewServer() - - // Create a done channel to signal when the shutdown is complete - done := make(chan bool, 1) - - // Run graceful shutdown in a separate goroutine - go gracefulShutdown(server, done) - - err := server.ListenAndServe() - if err != nil && err != http.ErrServerClosed { - panic(fmt.Sprintf("http server error: %s", err)) - } - - // Wait for the graceful shutdown to complete - <-done - log.Println("Graceful shutdown complete.") -} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e69de29 diff --git a/internal/server/routes.go b/internal/server/routes.go deleted file mode 100644 index a93638a..0000000 --- a/internal/server/routes.go +++ /dev/null @@ -1,49 +0,0 @@ -package server - -import ( - "encoding/json" - "log" - "net/http" -) - -func (s *Server) RegisterRoutes() http.Handler { - mux := http.NewServeMux() - - // Register routes - mux.HandleFunc("/", s.HelloWorldHandler) - - // Wrap the mux with CORS middleware - return s.corsMiddleware(mux) -} - -func (s *Server) corsMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // Set CORS headers - w.Header().Set("Access-Control-Allow-Origin", "*") // Replace "*" with specific origins if needed - w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH") - w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, X-CSRF-Token") - w.Header().Set("Access-Control-Allow-Credentials", "false") // Set to "true" if credentials are required - - // Handle preflight OPTIONS requests - if r.Method == http.MethodOptions { - w.WriteHeader(http.StatusNoContent) - return - } - - // Proceed with the next handler - next.ServeHTTP(w, r) - }) -} - -func (s *Server) HelloWorldHandler(w http.ResponseWriter, r *http.Request) { - resp := map[string]string{"message": "Hello World"} - jsonResp, err := json.Marshal(resp) - if err != nil { - http.Error(w, "Failed to marshal response", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - if _, err := w.Write(jsonResp); err != nil { - log.Printf("Failed to write response: %v", err) - } -} diff --git a/internal/server/routes_test.go b/internal/server/routes_test.go deleted file mode 100644 index 602ae81..0000000 --- a/internal/server/routes_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package server - -import ( - "io" - "net/http" - "net/http/httptest" - "testing" -) - -func TestHandler(t *testing.T) { - s := &Server{} - server := httptest.NewServer(http.HandlerFunc(s.HelloWorldHandler)) - defer server.Close() - resp, err := http.Get(server.URL) - if err != nil { - t.Fatalf("error making request to server. Err: %v", err) - } - defer resp.Body.Close() - // Assertions - if resp.StatusCode != http.StatusOK { - t.Errorf("expected status OK; got %v", resp.Status) - } - expected := "{\"message\":\"Hello World\"}" - body, err := io.ReadAll(resp.Body) - if err != nil { - t.Fatalf("error reading response body. Err: %v", err) - } - if expected != string(body) { - t.Errorf("expected response body to be %v; got %v", expected, string(body)) - } -} diff --git a/internal/server/server.go b/main.go similarity index 100% rename from internal/server/server.go rename to main.go