route /api/v1/events/slack differently when INITIALIZE_SLACK=true
parent
052a093ad7
commit
2372fa8bb9
34
main.go
34
main.go
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -9,8 +10,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -58,19 +57,40 @@ func listenAndServe(ctx context.Context, cfg Config) chan error {
|
||||||
func newHandler(cfg Config) http.HandlerFunc {
|
func newHandler(cfg Config) http.HandlerFunc {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.Handle("POST /api/v1/events/{src}/{$}", http.HandlerFunc(newHandlerPostAPIV1Events(cfg)))
|
mux.Handle("POST /api/v1/events/slack", http.HandlerFunc(newHandlerPostAPIV1EventsSlack(cfg)))
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
b, _ := io.ReadAll(r.Body)
|
b, _ := io.ReadAll(r.Body)
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
log.Printf("%s %s | %s", r.Method, r.URL, b)
|
log.Printf("%s %s | %s", r.Method, r.URL, b)
|
||||||
|
|
||||||
mux.ServeHTTP(w, r)
|
mux.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHandlerPostAPIV1Events(cfg Config) http.HandlerFunc {
|
func newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {
|
||||||
|
if cfg.InitializeSlack {
|
||||||
|
return handlerPostAPIV1EventsSlackInitialize
|
||||||
|
}
|
||||||
|
return _newHandlerPostAPIV1EventsSlack(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlerPostAPIV1EventsSlackInitialize(w http.ResponseWriter, r *http.Request) {
|
||||||
|
b, _ := io.ReadAll(r.Body)
|
||||||
|
var challenge struct {
|
||||||
|
Token string
|
||||||
|
Challenge string
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(b, &challenge); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(map[string]any{"challenge": challenge.Challenge})
|
||||||
|
}
|
||||||
|
|
||||||
|
func _newHandlerPostAPIV1EventsSlack(cfg Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
src := path.Base(strings.TrimSuffix(r.URL.Path, "/"))
|
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||||
log.Printf("%s %s | src=%s", r.Method, r.URL, src)
|
|
||||||
json.NewEncoder(w).Encode(map[string]any{"src": src})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue