aboutsummaryrefslogtreecommitdiff
path: root/transport/http.go
diff options
context:
space:
mode:
Diffstat (limited to 'transport/http.go')
-rw-r--r--transport/http.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/transport/http.go b/transport/http.go
new file mode 100644
index 0000000..663bb31
--- /dev/null
+++ b/transport/http.go
@@ -0,0 +1,40 @@
+package transport
+
+import (
+ "context"
+ "crypto/tls"
+ "net"
+ "net/http"
+)
+
+type HTTP struct {
+ Bind string
+ TLS *tls.Config
+}
+
+func (h *HTTP) Run(ctx context.Context, resolver Resolver) error {
+ srv := http.Server{
+ Addr: h.Bind,
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ w.WriteHeader(http.StatusMethodNotAllowed)
+ return
+ }
+ w.WriteHeader(http.StatusOK)
+ w.Header().Set("Content-Type", "application/json")
+ resolver.Resolve(ctx, r.Body, w)
+ }),
+ BaseContext: func(l net.Listener) context.Context {
+ return ctx
+ },
+ TLSConfig: h.TLS,
+ }
+ go func() {
+ <-ctx.Done()
+ srv.Close()
+ }()
+ if err := srv.ListenAndServe(); err != http.ErrServerClosed {
+ return err
+ }
+ return nil
+}