-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support http client- and server
- Loading branch information
1 parent
6e0e05a
commit 3862498
Showing
6 changed files
with
177 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/soerenschneider/dyndns/internal/common" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
type HttpServer struct { | ||
address string | ||
requests chan common.UpdateRecordRequest | ||
|
||
// optional | ||
certFile string | ||
keyFile string | ||
} | ||
|
||
type WebhookOpts func(*HttpServer) error | ||
|
||
func New(address string, requestsChan chan common.UpdateRecordRequest, opts ...WebhookOpts) (*HttpServer, error) { | ||
if len(address) == 0 { | ||
return nil, errors.New("empty address provided") | ||
} | ||
|
||
w := &HttpServer{ | ||
address: address, | ||
} | ||
|
||
var errs error | ||
for _, opt := range opts { | ||
if err := opt(w); err != nil { | ||
errs = multierr.Append(errs, err) | ||
} | ||
} | ||
|
||
return w, errs | ||
} | ||
|
||
func (s *HttpServer) IsTLSConfigured() bool { | ||
return len(s.certFile) > 0 && len(s.keyFile) > 0 | ||
} | ||
|
||
func (s *HttpServer) handle(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
w.WriteHeader(400) | ||
} | ||
|
||
data, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
payload := common.UpdateRecordRequest{} | ||
if err := json.Unmarshal(data, &payload); err != nil { | ||
return | ||
} | ||
|
||
s.requests <- payload | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func (s *HttpServer) Listen(ctx context.Context, events chan bool, wg *sync.WaitGroup) error { | ||
wg.Add(1) | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/update", s.handle) | ||
|
||
server := http.Server{ | ||
Addr: s.address, | ||
Handler: mux, | ||
ReadTimeout: 3 * time.Second, | ||
ReadHeaderTimeout: 3 * time.Second, | ||
WriteTimeout: 3 * time.Second, | ||
IdleTimeout: 30 * time.Second, | ||
} | ||
|
||
errChan := make(chan error) | ||
go func() { | ||
if s.IsTLSConfigured() { | ||
if err := server.ListenAndServeTLS(s.certFile, s.keyFile); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
errChan <- fmt.Errorf("can not start webhook server: %w", err) | ||
} | ||
} else { | ||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
errChan <- fmt.Errorf("can not start webhook server: %w", err) | ||
} | ||
} | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
log.Info().Msg("Stopping webhook server") | ||
err := server.Shutdown(ctx) | ||
wg.Done() | ||
return err | ||
case err := <-errChan: | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
func WithTLS(certFile, keyFile string) func(s *HttpServer) error { | ||
return func(s *HttpServer) error { | ||
if len(certFile) == 0 { | ||
return errors.New("empty certfile") | ||
} | ||
|
||
if len(keyFile) == 0 { | ||
return errors.New("empty keyfile") | ||
} | ||
|
||
s.certFile = certFile | ||
s.keyFile = keyFile | ||
return nil | ||
} | ||
} |