mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-07 21:47:09 +00:00
42 lines
950 B
Go
42 lines
950 B
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/ncarlier/webhookd/pkg/auth"
|
|
"github.com/ncarlier/webhookd/pkg/config"
|
|
"github.com/ncarlier/webhookd/pkg/middleware"
|
|
)
|
|
|
|
// NewRouter creates router with declared routes
|
|
func NewRouter(conf *config.Config) *http.ServeMux {
|
|
router := http.NewServeMux()
|
|
authenticator := auth.NewAuthenticator(conf)
|
|
|
|
nextRequestID := func() string {
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
}
|
|
|
|
for _, route := range routes {
|
|
var handler http.Handler
|
|
|
|
handler = route.HandlerFunc(conf)
|
|
handler = middleware.Method(handler, route.Methods)
|
|
handler = middleware.Cors(handler)
|
|
if conf.TLSListenAddr != "" {
|
|
handler = middleware.HSTS(handler)
|
|
}
|
|
handler = middleware.Logger(handler)
|
|
handler = middleware.Tracing(nextRequestID)(handler)
|
|
|
|
if authenticator != nil {
|
|
handler = middleware.Auth(handler, authenticator)
|
|
}
|
|
router.Handle(route.Path, handler)
|
|
}
|
|
|
|
return router
|
|
}
|