Skip to content

Commit

Permalink
Adds graceful shutdown and fixes in Makefile (#14)
Browse files Browse the repository at this point in the history
* Added graceful shutdown

* adds -p flag to mkdir commands
  • Loading branch information
deepto98 authored Apr 16, 2024
1 parent 28ba007 commit 5a2839e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions internal/proxy/server.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package proxy

import (
"context"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"

"waffle/internal/certificate"
Expand Down Expand Up @@ -117,9 +119,27 @@ func (s *Server) Start() error {
ErrorLog: log.New(os.Stderr, "proxy server: ", 0),
}

// Implementing graceful shutdown - if an interrupt signal is received, stop listening on the port
idleConnsClosed := make(chan struct{})

go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint

// We received an interrupt signal, shut down.
log.Println("Shutting down server...")
if err := server.Shutdown(context.Background()); err != nil {
// Error from closing listener(s), or context timeout:
log.Printf("HTTP server Shutdown: %v", err)
}
close(idleConnsClosed)
}()

if err := server.Serve(tcpListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("server serve: %w", err)
}
<-idleConnsClosed

return nil
}
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
certs_windows:
mkdir "./cmd/proxy/.cert"
mkdir -p "./cmd/proxy/.cert"
# Create CA (certificate authority)
openssl ecparam -out ./cmd/proxy/.cert/ca.key -name prime256v1 -genkey
openssl req -new -sha256 -key ./cmd/proxy/.cert/ca.key -out ./cmd/proxy/.cert/ca.csr
Expand All @@ -12,7 +12,7 @@ certs_windows:
openssl x509 -in ./cmd/proxy/.cert/server.crt -text -noout

certs:
mkdir "./cmd/proxy/.cert"
mkdir -p "./cmd/proxy/.cert"
# Create CA (certificate authority)
openssl ecparam -out ./cmd/proxy/.cert/ca.key -name prime256v1 -genkey
openssl req -new -sha256 -key ./cmd/proxy/.cert/ca.key -out ./cmd/proxy/.cert/ca.csr
Expand Down

0 comments on commit 5a2839e

Please sign in to comment.