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,
	{{- if $.Actions }}
	controller *controller.Controller,
	{{- end }}
	{{- if $.HasPublic }}
	public public.Middleware,
	{{- end }}
	{{- if $.HasView }}
	view view.Server,
	{{- end }}
	{{- if $.ShowWelcome }}
	welcome welcome.Middleware,
	{{- end }}
) *Server {
	{{- if $.Actions }}
	// Action routing
	{{- range $action := $.Actions }}
	router.{{ $action.Method }}(`{{ $action.Route }}`, controller.{{ $action.CallName }})
	{{- end }}
	{{- end }}
	// Compose the middleware together
	middleware := middleware.Compose(
		middleware.MethodOverride(),
		router,
		{{- if $.ShowWelcome }}
		welcome,
		{{- end }}
		{{- if $.HasView }}
		view,
		{{- end }}
		{{- if $.HasPublic }}
		public,
		{{- end }}
	)
	// 404 at the bottom of the middleware
	handler := middleware.Middleware(http.NotFoundHandler())
	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)
}
