package web

// GENERATED. DO NOT EDIT.

{{- if $.Imports }}

import (
	{{- range $import := $.Imports }}
	{{$import.Name}} "{{$import.Path}}"
	{{- end }}
)
{{- end }}

// New web server
func New(
	router *router.Router,
	{{- range $resource := $.Resources }}
	{{ $resource.Camel }} *{{ $resource.Import.Name }}.Handler,
	{{- end }}
) *Server {
	{{- if $.Resources }}
	// Register routes
	{{- range $resource := $.Resources }}
	{{ $resource.Camel }}.Register(router)
	{{- end }}
	{{- end }}
	// Compose the middleware together
	middleware := middleware.Compose(
		middleware.MethodOverride(),
		router,
	)
	// 404 at the bottom of the middleware
	handler := middleware.Middleware(http.NotFoundHandler())
	// Return the web server
	return &Server{handler}
}

type Server struct {
	http.Handler
}

func (s *Server) Serve(ctx context.Context, address string) error {
	listener, err := webrt.Listen("WEB", address)
	if err != nil {
		return err
	}
	return webrt.Serve(ctx, listener, s)
}
