package server

import (
	"context"
	"net/http"
	"regexp"

	user "github.com/fieldkit/cloud/server/api/gen/user"
	goahttp "goa.design/goa/v3/http"
	goa "goa.design/goa/v3/pkg"
	"goa.design/plugins/v3/cors"
)

type Server struct {
	Mounts                  []*MountPoint
	Roles                   http.Handler
	UploadPhoto             http.Handler
	DownloadPhoto           http.Handler
	Login                   http.Handler
	RecoveryLookup          http.Handler
	Recovery                http.Handler
	Resume                  http.Handler
	Logout                  http.Handler
	Refresh                 http.Handler
	SendValidation          http.Handler
	Validate                http.Handler
	Add                     http.Handler
	Update                  http.Handler
	ChangePassword          http.Handler
	AcceptTnc               http.Handler
	GetCurrent              http.Handler
	ListByProject           http.Handler
	IssueTransmissionToken  http.Handler
	ProjectRoles            http.Handler
	AdminTermsAndConditions http.Handler
	AdminDelete             http.Handler
	AdminSearch             http.Handler
	CORS                    http.Handler
}

type ErrorNamer interface {
	ErrorName() string
}

type MountPoint struct {

	Method string

	Verb string

	Pattern string
}

func New(
	e *user.Endpoints,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) *Server {
	return &Server{
		Mounts: []*MountPoint{
			{"Roles", "GET", "/roles"},
			{"UploadPhoto", "POST", "/user/media"},
			{"DownloadPhoto", "GET", "/user/{userId}/media"},
			{"Login", "POST", "/login"},
			{"RecoveryLookup", "POST", "/user/recovery/lookup"},
			{"Recovery", "POST", "/user/recovery"},
			{"Resume", "POST", "/user/resume"},
			{"Logout", "POST", "/logout"},
			{"Refresh", "POST", "/refresh"},
			{"SendValidation", "POST", "/users/{userId}/validate-email"},
			{"Validate", "GET", "/validate"},
			{"Add", "POST", "/users"},
			{"Update", "PATCH", "/users/{userId}"},
			{"ChangePassword", "PATCH", "/users/{userId}/password"},
			{"AcceptTnc", "PATCH", "/users/{userId}/accept-tnc"},
			{"GetCurrent", "GET", "/user"},
			{"ListByProject", "GET", "/users/project/{projectId}"},
			{"IssueTransmissionToken", "GET", "/user/transmission-token"},
			{"ProjectRoles", "GET", "/projects/roles"},
			{"AdminTermsAndConditions", "DELETE", "/admin/user/tnc"},
			{"AdminDelete", "DELETE", "/admin/user"},
			{"AdminSearch", "POST", "/admin/users/search"},
			{"CORS", "OPTIONS", "/roles"},
			{"CORS", "OPTIONS", "/user/media"},
			{"CORS", "OPTIONS", "/user/{userId}/media"},
			{"CORS", "OPTIONS", "/login"},
			{"CORS", "OPTIONS", "/user/recovery/lookup"},
			{"CORS", "OPTIONS", "/user/recovery"},
			{"CORS", "OPTIONS", "/user/resume"},
			{"CORS", "OPTIONS", "/logout"},
			{"CORS", "OPTIONS", "/refresh"},
			{"CORS", "OPTIONS", "/users/{userId}/validate-email"},
			{"CORS", "OPTIONS", "/validate"},
			{"CORS", "OPTIONS", "/users"},
			{"CORS", "OPTIONS", "/users/{userId}"},
			{"CORS", "OPTIONS", "/users/{userId}/password"},
			{"CORS", "OPTIONS", "/users/{userId}/accept-tnc"},
			{"CORS", "OPTIONS", "/user"},
			{"CORS", "OPTIONS", "/users/project/{projectId}"},
			{"CORS", "OPTIONS", "/user/transmission-token"},
			{"CORS", "OPTIONS", "/projects/roles"},
			{"CORS", "OPTIONS", "/admin/user/tnc"},
			{"CORS", "OPTIONS", "/admin/user"},
			{"CORS", "OPTIONS", "/admin/users/search"},
		},
		Roles:                   NewRolesHandler(e.Roles, mux, decoder, encoder, errhandler, formatter),
		UploadPhoto:             NewUploadPhotoHandler(e.UploadPhoto, mux, decoder, encoder, errhandler, formatter),
		DownloadPhoto:           NewDownloadPhotoHandler(e.DownloadPhoto, mux, decoder, encoder, errhandler, formatter),
		Login:                   NewLoginHandler(e.Login, mux, decoder, encoder, errhandler, formatter),
		RecoveryLookup:          NewRecoveryLookupHandler(e.RecoveryLookup, mux, decoder, encoder, errhandler, formatter),
		Recovery:                NewRecoveryHandler(e.Recovery, mux, decoder, encoder, errhandler, formatter),
		Resume:                  NewResumeHandler(e.Resume, mux, decoder, encoder, errhandler, formatter),
		Logout:                  NewLogoutHandler(e.Logout, mux, decoder, encoder, errhandler, formatter),
		Refresh:                 NewRefreshHandler(e.Refresh, mux, decoder, encoder, errhandler, formatter),
		SendValidation:          NewSendValidationHandler(e.SendValidation, mux, decoder, encoder, errhandler, formatter),
		Validate:                NewValidateHandler(e.Validate, mux, decoder, encoder, errhandler, formatter),
		Add:                     NewAddHandler(e.Add, mux, decoder, encoder, errhandler, formatter),
		Update:                  NewUpdateHandler(e.Update, mux, decoder, encoder, errhandler, formatter),
		ChangePassword:          NewChangePasswordHandler(e.ChangePassword, mux, decoder, encoder, errhandler, formatter),
		AcceptTnc:               NewAcceptTncHandler(e.AcceptTnc, mux, decoder, encoder, errhandler, formatter),
		GetCurrent:              NewGetCurrentHandler(e.GetCurrent, mux, decoder, encoder, errhandler, formatter),
		ListByProject:           NewListByProjectHandler(e.ListByProject, mux, decoder, encoder, errhandler, formatter),
		IssueTransmissionToken:  NewIssueTransmissionTokenHandler(e.IssueTransmissionToken, mux, decoder, encoder, errhandler, formatter),
		ProjectRoles:            NewProjectRolesHandler(e.ProjectRoles, mux, decoder, encoder, errhandler, formatter),
		AdminTermsAndConditions: NewAdminTermsAndConditionsHandler(e.AdminTermsAndConditions, mux, decoder, encoder, errhandler, formatter),
		AdminDelete:             NewAdminDeleteHandler(e.AdminDelete, mux, decoder, encoder, errhandler, formatter),
		AdminSearch:             NewAdminSearchHandler(e.AdminSearch, mux, decoder, encoder, errhandler, formatter),
		CORS:                    NewCORSHandler(),
	}
}

func (s *Server) Service() string { return "user" }

func (s *Server) Use(m func(http.Handler) http.Handler) {
	s.Roles = m(s.Roles)
	s.UploadPhoto = m(s.UploadPhoto)
	s.DownloadPhoto = m(s.DownloadPhoto)
	s.Login = m(s.Login)
	s.RecoveryLookup = m(s.RecoveryLookup)
	s.Recovery = m(s.Recovery)
	s.Resume = m(s.Resume)
	s.Logout = m(s.Logout)
	s.Refresh = m(s.Refresh)
	s.SendValidation = m(s.SendValidation)
	s.Validate = m(s.Validate)
	s.Add = m(s.Add)
	s.Update = m(s.Update)
	s.ChangePassword = m(s.ChangePassword)
	s.AcceptTnc = m(s.AcceptTnc)
	s.GetCurrent = m(s.GetCurrent)
	s.ListByProject = m(s.ListByProject)
	s.IssueTransmissionToken = m(s.IssueTransmissionToken)
	s.ProjectRoles = m(s.ProjectRoles)
	s.AdminTermsAndConditions = m(s.AdminTermsAndConditions)
	s.AdminDelete = m(s.AdminDelete)
	s.AdminSearch = m(s.AdminSearch)
	s.CORS = m(s.CORS)
}

func Mount(mux goahttp.Muxer, h *Server) {
	MountRolesHandler(mux, h.Roles)
	MountUploadPhotoHandler(mux, h.UploadPhoto)
	MountDownloadPhotoHandler(mux, h.DownloadPhoto)
	MountLoginHandler(mux, h.Login)
	MountRecoveryLookupHandler(mux, h.RecoveryLookup)
	MountRecoveryHandler(mux, h.Recovery)
	MountResumeHandler(mux, h.Resume)
	MountLogoutHandler(mux, h.Logout)
	MountRefreshHandler(mux, h.Refresh)
	MountSendValidationHandler(mux, h.SendValidation)
	MountValidateHandler(mux, h.Validate)
	MountAddHandler(mux, h.Add)
	MountUpdateHandler(mux, h.Update)
	MountChangePasswordHandler(mux, h.ChangePassword)
	MountAcceptTncHandler(mux, h.AcceptTnc)
	MountGetCurrentHandler(mux, h.GetCurrent)
	MountListByProjectHandler(mux, h.ListByProject)
	MountIssueTransmissionTokenHandler(mux, h.IssueTransmissionToken)
	MountProjectRolesHandler(mux, h.ProjectRoles)
	MountAdminTermsAndConditionsHandler(mux, h.AdminTermsAndConditions)
	MountAdminDeleteHandler(mux, h.AdminDelete)
	MountAdminSearchHandler(mux, h.AdminSearch)
	MountCORSHandler(mux, h.CORS)
}

func MountRolesHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/roles", f)
}

func NewRolesHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRolesRequest(mux, decoder)
		encodeResponse = EncodeRolesResponse(encoder)
		encodeError    = EncodeRolesError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "roles")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountUploadPhotoHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/media", f)
}

func NewUploadPhotoHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeUploadPhotoRequest(mux, decoder)
		encodeResponse = EncodeUploadPhotoResponse(encoder)
		encodeError    = EncodeUploadPhotoError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "upload photo")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		data := &user.UploadPhotoRequestData{Payload: payload.(*user.UploadPhotoPayload), Body: r.Body}
		res, err := endpoint(ctx, data)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountDownloadPhotoHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/user/{userId}/media", f)
}

func NewDownloadPhotoHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeDownloadPhotoRequest(mux, decoder)
		encodeResponse = EncodeDownloadPhotoResponse(encoder)
		encodeError    = EncodeDownloadPhotoError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "download photo")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountLoginHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/login", f)
}

func NewLoginHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeLoginRequest(mux, decoder)
		encodeResponse = EncodeLoginResponse(encoder)
		encodeError    = EncodeLoginError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "login")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountRecoveryLookupHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/recovery/lookup", f)
}

func NewRecoveryLookupHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRecoveryLookupRequest(mux, decoder)
		encodeResponse = EncodeRecoveryLookupResponse(encoder)
		encodeError    = EncodeRecoveryLookupError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "recovery lookup")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountRecoveryHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/recovery", f)
}

func NewRecoveryHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRecoveryRequest(mux, decoder)
		encodeResponse = EncodeRecoveryResponse(encoder)
		encodeError    = EncodeRecoveryError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "recovery")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountResumeHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/resume", f)
}

func NewResumeHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeResumeRequest(mux, decoder)
		encodeResponse = EncodeResumeResponse(encoder)
		encodeError    = EncodeResumeError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "resume")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountLogoutHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/logout", f)
}

func NewLogoutHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeLogoutRequest(mux, decoder)
		encodeResponse = EncodeLogoutResponse(encoder)
		encodeError    = EncodeLogoutError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "logout")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountRefreshHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/refresh", f)
}

func NewRefreshHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRefreshRequest(mux, decoder)
		encodeResponse = EncodeRefreshResponse(encoder)
		encodeError    = EncodeRefreshError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "refresh")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountSendValidationHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/users/{userId}/validate-email", f)
}

func NewSendValidationHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeSendValidationRequest(mux, decoder)
		encodeResponse = EncodeSendValidationResponse(encoder)
		encodeError    = EncodeSendValidationError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "send validation")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountValidateHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/validate", f)
}

func NewValidateHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeValidateRequest(mux, decoder)
		encodeResponse = EncodeValidateResponse(encoder)
		encodeError    = EncodeValidateError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "validate")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountAddHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/users", f)
}

func NewAddHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAddRequest(mux, decoder)
		encodeResponse = EncodeAddResponse(encoder)
		encodeError    = EncodeAddError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "add")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountUpdateHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("PATCH", "/users/{userId}", f)
}

func NewUpdateHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeUpdateRequest(mux, decoder)
		encodeResponse = EncodeUpdateResponse(encoder)
		encodeError    = EncodeUpdateError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "update")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountChangePasswordHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("PATCH", "/users/{userId}/password", f)
}

func NewChangePasswordHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeChangePasswordRequest(mux, decoder)
		encodeResponse = EncodeChangePasswordResponse(encoder)
		encodeError    = EncodeChangePasswordError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "change password")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountAcceptTncHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("PATCH", "/users/{userId}/accept-tnc", f)
}

func NewAcceptTncHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAcceptTncRequest(mux, decoder)
		encodeResponse = EncodeAcceptTncResponse(encoder)
		encodeError    = EncodeAcceptTncError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "accept tnc")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountGetCurrentHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/user", f)
}

func NewGetCurrentHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeGetCurrentRequest(mux, decoder)
		encodeResponse = EncodeGetCurrentResponse(encoder)
		encodeError    = EncodeGetCurrentError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "get current")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountListByProjectHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/users/project/{projectId}", f)
}

func NewListByProjectHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeListByProjectRequest(mux, decoder)
		encodeResponse = EncodeListByProjectResponse(encoder)
		encodeError    = EncodeListByProjectError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "list by project")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountIssueTransmissionTokenHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/user/transmission-token", f)
}

func NewIssueTransmissionTokenHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeIssueTransmissionTokenRequest(mux, decoder)
		encodeResponse = EncodeIssueTransmissionTokenResponse(encoder)
		encodeError    = EncodeIssueTransmissionTokenError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "issue transmission token")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountProjectRolesHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/projects/roles", f)
}

func NewProjectRolesHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		encodeResponse = EncodeProjectRolesResponse(encoder)
		encodeError    = EncodeProjectRolesError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "project roles")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		var err error
		res, err := endpoint(ctx, nil)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountAdminTermsAndConditionsHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("DELETE", "/admin/user/tnc", f)
}

func NewAdminTermsAndConditionsHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAdminTermsAndConditionsRequest(mux, decoder)
		encodeResponse = EncodeAdminTermsAndConditionsResponse(encoder)
		encodeError    = EncodeAdminTermsAndConditionsError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "admin terms and conditions")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountAdminDeleteHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("DELETE", "/admin/user", f)
}

func NewAdminDeleteHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAdminDeleteRequest(mux, decoder)
		encodeResponse = EncodeAdminDeleteResponse(encoder)
		encodeError    = EncodeAdminDeleteError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "admin delete")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountAdminSearchHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/admin/users/search", f)
}

func NewAdminSearchHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAdminSearchRequest(mux, decoder)
		encodeResponse = EncodeAdminSearchResponse(encoder)
		encodeError    = EncodeAdminSearchError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "admin search")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

func MountCORSHandler(mux goahttp.Muxer, h http.Handler) {
	h = handleUserOrigin(h)
	f, ok := h.(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("OPTIONS", "/roles", f)
	mux.Handle("OPTIONS", "/user/media", f)
	mux.Handle("OPTIONS", "/user/{userId}/media", f)
	mux.Handle("OPTIONS", "/login", f)
	mux.Handle("OPTIONS", "/user/recovery/lookup", f)
	mux.Handle("OPTIONS", "/user/recovery", f)
	mux.Handle("OPTIONS", "/user/resume", f)
	mux.Handle("OPTIONS", "/logout", f)
	mux.Handle("OPTIONS", "/refresh", f)
	mux.Handle("OPTIONS", "/users/{userId}/validate-email", f)
	mux.Handle("OPTIONS", "/validate", f)
	mux.Handle("OPTIONS", "/users", f)
	mux.Handle("OPTIONS", "/users/{userId}", f)
	mux.Handle("OPTIONS", "/users/{userId}/password", f)
	mux.Handle("OPTIONS", "/users/{userId}/accept-tnc", f)
	mux.Handle("OPTIONS", "/user", f)
	mux.Handle("OPTIONS", "/users/project/{projectId}", f)
	mux.Handle("OPTIONS", "/user/transmission-token", f)
	mux.Handle("OPTIONS", "/projects/roles", f)
	mux.Handle("OPTIONS", "/admin/user/tnc", f)
	mux.Handle("OPTIONS", "/admin/user", f)
	mux.Handle("OPTIONS", "/admin/users/search", f)
}

func NewCORSHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
	})
}

func handleUserOrigin(h http.Handler) http.Handler {
	spec0 := regexp.MustCompile("(.+[.])?127.0.0.1:\\d+")
	spec1 := regexp.MustCompile("(.+[.])?192.168.\\d+.\\d+:\\d+")
	spec2 := regexp.MustCompile("(.+[.])?fieldkit.org:\\d+")
	spec3 := regexp.MustCompile("(.+[.])?local.fkdev.org:\\d+")
	spec4 := regexp.MustCompile("(.+[.])?localhost:\\d+")
	origHndlr := h.(http.HandlerFunc)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		origin := r.Header.Get("Origin")
		if origin == "" {

			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec0) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec1) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec2) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec3) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec4) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https:
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https:
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https:
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https:
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https:
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https:
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {

				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		origHndlr(w, r)
		return
	})
}
// Code generated by goa v3.2.4, DO NOT EDIT.
//
// user HTTP server
//
// Command:
// $ goa gen github.com/fieldkit/cloud/server/api/design

package server

import (
	"context"
	"net/http"
	"regexp"

	user "github.com/fieldkit/cloud/server/api/gen/user"
	goahttp "goa.design/goa/v3/http"
	goa "goa.design/goa/v3/pkg"
	"goa.design/plugins/v3/cors"
)

// Server lists the user service endpoint HTTP handlers.
type Server struct {
	Mounts                  []*MountPoint
	Roles                   http.Handler
	UploadPhoto             http.Handler
	DownloadPhoto           http.Handler
	Login                   http.Handler
	RecoveryLookup          http.Handler
	Recovery                http.Handler
	Resume                  http.Handler
	Logout                  http.Handler
	Refresh                 http.Handler
	SendValidation          http.Handler
	Validate                http.Handler
	Add                     http.Handler
	Update                  http.Handler
	ChangePassword          http.Handler
	AcceptTnc               http.Handler
	GetCurrent              http.Handler
	ListByProject           http.Handler
	IssueTransmissionToken  http.Handler
	ProjectRoles            http.Handler
	AdminTermsAndConditions http.Handler
	AdminDelete             http.Handler
	AdminSearch             http.Handler
	CORS                    http.Handler
}

// ErrorNamer is an interface implemented by generated error structs that
// exposes the name of the error as defined in the design.
type ErrorNamer interface {
	ErrorName() string
}

// MountPoint holds information about the mounted endpoints.
type MountPoint struct {
	// Method is the name of the service method served by the mounted HTTP handler.
	Method string
	// Verb is the HTTP method used to match requests to the mounted handler.
	Verb string
	// Pattern is the HTTP request path pattern used to match requests to the
	// mounted handler.
	Pattern string
}

// New instantiates HTTP handlers for all the user service endpoints using the
// provided encoder and decoder. The handlers are mounted on the given mux
// using the HTTP verb and path defined in the design. errhandler is called
// whenever a response fails to be encoded. formatter is used to format errors
// returned by the service methods prior to encoding. Both errhandler and
// formatter are optional and can be nil.
func New(
	e *user.Endpoints,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) *Server {
	return &Server{
		Mounts: []*MountPoint{
			{"Roles", "GET", "/roles"},
			{"UploadPhoto", "POST", "/user/media"},
			{"DownloadPhoto", "GET", "/user/{userId}/media"},
			{"Login", "POST", "/login"},
			{"RecoveryLookup", "POST", "/user/recovery/lookup"},
			{"Recovery", "POST", "/user/recovery"},
			{"Resume", "POST", "/user/resume"},
			{"Logout", "POST", "/logout"},
			{"Refresh", "POST", "/refresh"},
			{"SendValidation", "POST", "/users/{userId}/validate-email"},
			{"Validate", "GET", "/validate"},
			{"Add", "POST", "/users"},
			{"Update", "PATCH", "/users/{userId}"},
			{"ChangePassword", "PATCH", "/users/{userId}/password"},
			{"AcceptTnc", "PATCH", "/users/{userId}/accept-tnc"},
			{"GetCurrent", "GET", "/user"},
			{"ListByProject", "GET", "/users/project/{projectId}"},
			{"IssueTransmissionToken", "GET", "/user/transmission-token"},
			{"ProjectRoles", "GET", "/projects/roles"},
			{"AdminTermsAndConditions", "DELETE", "/admin/user/tnc"},
			{"AdminDelete", "DELETE", "/admin/user"},
			{"AdminSearch", "POST", "/admin/users/search"},
			{"CORS", "OPTIONS", "/roles"},
			{"CORS", "OPTIONS", "/user/media"},
			{"CORS", "OPTIONS", "/user/{userId}/media"},
			{"CORS", "OPTIONS", "/login"},
			{"CORS", "OPTIONS", "/user/recovery/lookup"},
			{"CORS", "OPTIONS", "/user/recovery"},
			{"CORS", "OPTIONS", "/user/resume"},
			{"CORS", "OPTIONS", "/logout"},
			{"CORS", "OPTIONS", "/refresh"},
			{"CORS", "OPTIONS", "/users/{userId}/validate-email"},
			{"CORS", "OPTIONS", "/validate"},
			{"CORS", "OPTIONS", "/users"},
			{"CORS", "OPTIONS", "/users/{userId}"},
			{"CORS", "OPTIONS", "/users/{userId}/password"},
			{"CORS", "OPTIONS", "/users/{userId}/accept-tnc"},
			{"CORS", "OPTIONS", "/user"},
			{"CORS", "OPTIONS", "/users/project/{projectId}"},
			{"CORS", "OPTIONS", "/user/transmission-token"},
			{"CORS", "OPTIONS", "/projects/roles"},
			{"CORS", "OPTIONS", "/admin/user/tnc"},
			{"CORS", "OPTIONS", "/admin/user"},
			{"CORS", "OPTIONS", "/admin/users/search"},
		},
		Roles:                   NewRolesHandler(e.Roles, mux, decoder, encoder, errhandler, formatter),
		UploadPhoto:             NewUploadPhotoHandler(e.UploadPhoto, mux, decoder, encoder, errhandler, formatter),
		DownloadPhoto:           NewDownloadPhotoHandler(e.DownloadPhoto, mux, decoder, encoder, errhandler, formatter),
		Login:                   NewLoginHandler(e.Login, mux, decoder, encoder, errhandler, formatter),
		RecoveryLookup:          NewRecoveryLookupHandler(e.RecoveryLookup, mux, decoder, encoder, errhandler, formatter),
		Recovery:                NewRecoveryHandler(e.Recovery, mux, decoder, encoder, errhandler, formatter),
		Resume:                  NewResumeHandler(e.Resume, mux, decoder, encoder, errhandler, formatter),
		Logout:                  NewLogoutHandler(e.Logout, mux, decoder, encoder, errhandler, formatter),
		Refresh:                 NewRefreshHandler(e.Refresh, mux, decoder, encoder, errhandler, formatter),
		SendValidation:          NewSendValidationHandler(e.SendValidation, mux, decoder, encoder, errhandler, formatter),
		Validate:                NewValidateHandler(e.Validate, mux, decoder, encoder, errhandler, formatter),
		Add:                     NewAddHandler(e.Add, mux, decoder, encoder, errhandler, formatter),
		Update:                  NewUpdateHandler(e.Update, mux, decoder, encoder, errhandler, formatter),
		ChangePassword:          NewChangePasswordHandler(e.ChangePassword, mux, decoder, encoder, errhandler, formatter),
		AcceptTnc:               NewAcceptTncHandler(e.AcceptTnc, mux, decoder, encoder, errhandler, formatter),
		GetCurrent:              NewGetCurrentHandler(e.GetCurrent, mux, decoder, encoder, errhandler, formatter),
		ListByProject:           NewListByProjectHandler(e.ListByProject, mux, decoder, encoder, errhandler, formatter),
		IssueTransmissionToken:  NewIssueTransmissionTokenHandler(e.IssueTransmissionToken, mux, decoder, encoder, errhandler, formatter),
		ProjectRoles:            NewProjectRolesHandler(e.ProjectRoles, mux, decoder, encoder, errhandler, formatter),
		AdminTermsAndConditions: NewAdminTermsAndConditionsHandler(e.AdminTermsAndConditions, mux, decoder, encoder, errhandler, formatter),
		AdminDelete:             NewAdminDeleteHandler(e.AdminDelete, mux, decoder, encoder, errhandler, formatter),
		AdminSearch:             NewAdminSearchHandler(e.AdminSearch, mux, decoder, encoder, errhandler, formatter),
		CORS:                    NewCORSHandler(),
	}
}

// Service returns the name of the service served.
func (s *Server) Service() string { return "user" }

// Use wraps the server handlers with the given middleware.
func (s *Server) Use(m func(http.Handler) http.Handler) {
	s.Roles = m(s.Roles)
	s.UploadPhoto = m(s.UploadPhoto)
	s.DownloadPhoto = m(s.DownloadPhoto)
	s.Login = m(s.Login)
	s.RecoveryLookup = m(s.RecoveryLookup)
	s.Recovery = m(s.Recovery)
	s.Resume = m(s.Resume)
	s.Logout = m(s.Logout)
	s.Refresh = m(s.Refresh)
	s.SendValidation = m(s.SendValidation)
	s.Validate = m(s.Validate)
	s.Add = m(s.Add)
	s.Update = m(s.Update)
	s.ChangePassword = m(s.ChangePassword)
	s.AcceptTnc = m(s.AcceptTnc)
	s.GetCurrent = m(s.GetCurrent)
	s.ListByProject = m(s.ListByProject)
	s.IssueTransmissionToken = m(s.IssueTransmissionToken)
	s.ProjectRoles = m(s.ProjectRoles)
	s.AdminTermsAndConditions = m(s.AdminTermsAndConditions)
	s.AdminDelete = m(s.AdminDelete)
	s.AdminSearch = m(s.AdminSearch)
	s.CORS = m(s.CORS)
}

// Mount configures the mux to serve the user endpoints.
func Mount(mux goahttp.Muxer, h *Server) {
	MountRolesHandler(mux, h.Roles)
	MountUploadPhotoHandler(mux, h.UploadPhoto)
	MountDownloadPhotoHandler(mux, h.DownloadPhoto)
	MountLoginHandler(mux, h.Login)
	MountRecoveryLookupHandler(mux, h.RecoveryLookup)
	MountRecoveryHandler(mux, h.Recovery)
	MountResumeHandler(mux, h.Resume)
	MountLogoutHandler(mux, h.Logout)
	MountRefreshHandler(mux, h.Refresh)
	MountSendValidationHandler(mux, h.SendValidation)
	MountValidateHandler(mux, h.Validate)
	MountAddHandler(mux, h.Add)
	MountUpdateHandler(mux, h.Update)
	MountChangePasswordHandler(mux, h.ChangePassword)
	MountAcceptTncHandler(mux, h.AcceptTnc)
	MountGetCurrentHandler(mux, h.GetCurrent)
	MountListByProjectHandler(mux, h.ListByProject)
	MountIssueTransmissionTokenHandler(mux, h.IssueTransmissionToken)
	MountProjectRolesHandler(mux, h.ProjectRoles)
	MountAdminTermsAndConditionsHandler(mux, h.AdminTermsAndConditions)
	MountAdminDeleteHandler(mux, h.AdminDelete)
	MountAdminSearchHandler(mux, h.AdminSearch)
	MountCORSHandler(mux, h.CORS)
}

// MountRolesHandler configures the mux to serve the "user" service "roles"
// endpoint.
func MountRolesHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/roles", f)
}

// NewRolesHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "roles" endpoint.
func NewRolesHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRolesRequest(mux, decoder)
		encodeResponse = EncodeRolesResponse(encoder)
		encodeError    = EncodeRolesError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "roles")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountUploadPhotoHandler configures the mux to serve the "user" service
// "upload photo" endpoint.
func MountUploadPhotoHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/media", f)
}

// NewUploadPhotoHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "upload photo" endpoint.
func NewUploadPhotoHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeUploadPhotoRequest(mux, decoder)
		encodeResponse = EncodeUploadPhotoResponse(encoder)
		encodeError    = EncodeUploadPhotoError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "upload photo")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		data := &user.UploadPhotoRequestData{Payload: payload.(*user.UploadPhotoPayload), Body: r.Body}
		res, err := endpoint(ctx, data)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountDownloadPhotoHandler configures the mux to serve the "user" service
// "download photo" endpoint.
func MountDownloadPhotoHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/user/{userId}/media", f)
}

// NewDownloadPhotoHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "download photo" endpoint.
func NewDownloadPhotoHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeDownloadPhotoRequest(mux, decoder)
		encodeResponse = EncodeDownloadPhotoResponse(encoder)
		encodeError    = EncodeDownloadPhotoError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "download photo")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountLoginHandler configures the mux to serve the "user" service "login"
// endpoint.
func MountLoginHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/login", f)
}

// NewLoginHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "login" endpoint.
func NewLoginHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeLoginRequest(mux, decoder)
		encodeResponse = EncodeLoginResponse(encoder)
		encodeError    = EncodeLoginError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "login")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountRecoveryLookupHandler configures the mux to serve the "user" service
// "recovery lookup" endpoint.
func MountRecoveryLookupHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/recovery/lookup", f)
}

// NewRecoveryLookupHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "recovery lookup" endpoint.
func NewRecoveryLookupHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRecoveryLookupRequest(mux, decoder)
		encodeResponse = EncodeRecoveryLookupResponse(encoder)
		encodeError    = EncodeRecoveryLookupError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "recovery lookup")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountRecoveryHandler configures the mux to serve the "user" service
// "recovery" endpoint.
func MountRecoveryHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/recovery", f)
}

// NewRecoveryHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "recovery" endpoint.
func NewRecoveryHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRecoveryRequest(mux, decoder)
		encodeResponse = EncodeRecoveryResponse(encoder)
		encodeError    = EncodeRecoveryError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "recovery")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountResumeHandler configures the mux to serve the "user" service "resume"
// endpoint.
func MountResumeHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/user/resume", f)
}

// NewResumeHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "resume" endpoint.
func NewResumeHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeResumeRequest(mux, decoder)
		encodeResponse = EncodeResumeResponse(encoder)
		encodeError    = EncodeResumeError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "resume")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountLogoutHandler configures the mux to serve the "user" service "logout"
// endpoint.
func MountLogoutHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/logout", f)
}

// NewLogoutHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "logout" endpoint.
func NewLogoutHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeLogoutRequest(mux, decoder)
		encodeResponse = EncodeLogoutResponse(encoder)
		encodeError    = EncodeLogoutError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "logout")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountRefreshHandler configures the mux to serve the "user" service "refresh"
// endpoint.
func MountRefreshHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/refresh", f)
}

// NewRefreshHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "refresh" endpoint.
func NewRefreshHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeRefreshRequest(mux, decoder)
		encodeResponse = EncodeRefreshResponse(encoder)
		encodeError    = EncodeRefreshError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "refresh")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountSendValidationHandler configures the mux to serve the "user" service
// "send validation" endpoint.
func MountSendValidationHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/users/{userId}/validate-email", f)
}

// NewSendValidationHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "send validation" endpoint.
func NewSendValidationHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeSendValidationRequest(mux, decoder)
		encodeResponse = EncodeSendValidationResponse(encoder)
		encodeError    = EncodeSendValidationError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "send validation")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountValidateHandler configures the mux to serve the "user" service
// "validate" endpoint.
func MountValidateHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/validate", f)
}

// NewValidateHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "validate" endpoint.
func NewValidateHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeValidateRequest(mux, decoder)
		encodeResponse = EncodeValidateResponse(encoder)
		encodeError    = EncodeValidateError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "validate")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountAddHandler configures the mux to serve the "user" service "add"
// endpoint.
func MountAddHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/users", f)
}

// NewAddHandler creates a HTTP handler which loads the HTTP request and calls
// the "user" service "add" endpoint.
func NewAddHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAddRequest(mux, decoder)
		encodeResponse = EncodeAddResponse(encoder)
		encodeError    = EncodeAddError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "add")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountUpdateHandler configures the mux to serve the "user" service "update"
// endpoint.
func MountUpdateHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("PATCH", "/users/{userId}", f)
}

// NewUpdateHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "update" endpoint.
func NewUpdateHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeUpdateRequest(mux, decoder)
		encodeResponse = EncodeUpdateResponse(encoder)
		encodeError    = EncodeUpdateError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "update")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountChangePasswordHandler configures the mux to serve the "user" service
// "change password" endpoint.
func MountChangePasswordHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("PATCH", "/users/{userId}/password", f)
}

// NewChangePasswordHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "change password" endpoint.
func NewChangePasswordHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeChangePasswordRequest(mux, decoder)
		encodeResponse = EncodeChangePasswordResponse(encoder)
		encodeError    = EncodeChangePasswordError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "change password")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountAcceptTncHandler configures the mux to serve the "user" service "accept
// tnc" endpoint.
func MountAcceptTncHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("PATCH", "/users/{userId}/accept-tnc", f)
}

// NewAcceptTncHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "accept tnc" endpoint.
func NewAcceptTncHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAcceptTncRequest(mux, decoder)
		encodeResponse = EncodeAcceptTncResponse(encoder)
		encodeError    = EncodeAcceptTncError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "accept tnc")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountGetCurrentHandler configures the mux to serve the "user" service "get
// current" endpoint.
func MountGetCurrentHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/user", f)
}

// NewGetCurrentHandler creates a HTTP handler which loads the HTTP request and
// calls the "user" service "get current" endpoint.
func NewGetCurrentHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeGetCurrentRequest(mux, decoder)
		encodeResponse = EncodeGetCurrentResponse(encoder)
		encodeError    = EncodeGetCurrentError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "get current")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountListByProjectHandler configures the mux to serve the "user" service
// "list by project" endpoint.
func MountListByProjectHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/users/project/{projectId}", f)
}

// NewListByProjectHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "list by project" endpoint.
func NewListByProjectHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeListByProjectRequest(mux, decoder)
		encodeResponse = EncodeListByProjectResponse(encoder)
		encodeError    = EncodeListByProjectError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "list by project")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountIssueTransmissionTokenHandler configures the mux to serve the "user"
// service "issue transmission token" endpoint.
func MountIssueTransmissionTokenHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/user/transmission-token", f)
}

// NewIssueTransmissionTokenHandler creates a HTTP handler which loads the HTTP
// request and calls the "user" service "issue transmission token" endpoint.
func NewIssueTransmissionTokenHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeIssueTransmissionTokenRequest(mux, decoder)
		encodeResponse = EncodeIssueTransmissionTokenResponse(encoder)
		encodeError    = EncodeIssueTransmissionTokenError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "issue transmission token")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountProjectRolesHandler configures the mux to serve the "user" service
// "project roles" endpoint.
func MountProjectRolesHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("GET", "/projects/roles", f)
}

// NewProjectRolesHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "project roles" endpoint.
func NewProjectRolesHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		encodeResponse = EncodeProjectRolesResponse(encoder)
		encodeError    = EncodeProjectRolesError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "project roles")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		var err error
		res, err := endpoint(ctx, nil)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountAdminTermsAndConditionsHandler configures the mux to serve the "user"
// service "admin terms and conditions" endpoint.
func MountAdminTermsAndConditionsHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("DELETE", "/admin/user/tnc", f)
}

// NewAdminTermsAndConditionsHandler creates a HTTP handler which loads the
// HTTP request and calls the "user" service "admin terms and conditions"
// endpoint.
func NewAdminTermsAndConditionsHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAdminTermsAndConditionsRequest(mux, decoder)
		encodeResponse = EncodeAdminTermsAndConditionsResponse(encoder)
		encodeError    = EncodeAdminTermsAndConditionsError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "admin terms and conditions")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountAdminDeleteHandler configures the mux to serve the "user" service
// "admin delete" endpoint.
func MountAdminDeleteHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("DELETE", "/admin/user", f)
}

// NewAdminDeleteHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "admin delete" endpoint.
func NewAdminDeleteHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAdminDeleteRequest(mux, decoder)
		encodeResponse = EncodeAdminDeleteResponse(encoder)
		encodeError    = EncodeAdminDeleteError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "admin delete")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountAdminSearchHandler configures the mux to serve the "user" service
// "admin search" endpoint.
func MountAdminSearchHandler(mux goahttp.Muxer, h http.Handler) {
	f, ok := handleUserOrigin(h).(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("POST", "/admin/users/search", f)
}

// NewAdminSearchHandler creates a HTTP handler which loads the HTTP request
// and calls the "user" service "admin search" endpoint.
func NewAdminSearchHandler(
	endpoint goa.Endpoint,
	mux goahttp.Muxer,
	decoder func(*http.Request) goahttp.Decoder,
	encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
	errhandler func(context.Context, http.ResponseWriter, error),
	formatter func(err error) goahttp.Statuser,
) http.Handler {
	var (
		decodeRequest  = DecodeAdminSearchRequest(mux, decoder)
		encodeResponse = EncodeAdminSearchResponse(encoder)
		encodeError    = EncodeAdminSearchError(encoder, formatter)
	)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
		ctx = context.WithValue(ctx, goa.MethodKey, "admin search")
		ctx = context.WithValue(ctx, goa.ServiceKey, "user")
		payload, err := decodeRequest(r)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		res, err := endpoint(ctx, payload)
		if err != nil {
			if err := encodeError(ctx, w, err); err != nil {
				errhandler(ctx, w, err)
			}
			return
		}
		if err := encodeResponse(ctx, w, res); err != nil {
			errhandler(ctx, w, err)
		}
	})
}

// MountCORSHandler configures the mux to serve the CORS endpoints for the
// service user.
func MountCORSHandler(mux goahttp.Muxer, h http.Handler) {
	h = handleUserOrigin(h)
	f, ok := h.(http.HandlerFunc)
	if !ok {
		f = func(w http.ResponseWriter, r *http.Request) {
			h.ServeHTTP(w, r)
		}
	}
	mux.Handle("OPTIONS", "/roles", f)
	mux.Handle("OPTIONS", "/user/media", f)
	mux.Handle("OPTIONS", "/user/{userId}/media", f)
	mux.Handle("OPTIONS", "/login", f)
	mux.Handle("OPTIONS", "/user/recovery/lookup", f)
	mux.Handle("OPTIONS", "/user/recovery", f)
	mux.Handle("OPTIONS", "/user/resume", f)
	mux.Handle("OPTIONS", "/logout", f)
	mux.Handle("OPTIONS", "/refresh", f)
	mux.Handle("OPTIONS", "/users/{userId}/validate-email", f)
	mux.Handle("OPTIONS", "/validate", f)
	mux.Handle("OPTIONS", "/users", f)
	mux.Handle("OPTIONS", "/users/{userId}", f)
	mux.Handle("OPTIONS", "/users/{userId}/password", f)
	mux.Handle("OPTIONS", "/users/{userId}/accept-tnc", f)
	mux.Handle("OPTIONS", "/user", f)
	mux.Handle("OPTIONS", "/users/project/{projectId}", f)
	mux.Handle("OPTIONS", "/user/transmission-token", f)
	mux.Handle("OPTIONS", "/projects/roles", f)
	mux.Handle("OPTIONS", "/admin/user/tnc", f)
	mux.Handle("OPTIONS", "/admin/user", f)
	mux.Handle("OPTIONS", "/admin/users/search", f)
}

// NewCORSHandler creates a HTTP handler which returns a simple 200 response.
func NewCORSHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
	})
}

// handleUserOrigin applies the CORS response headers corresponding to the
// origin for the service user.
func handleUserOrigin(h http.Handler) http.Handler {
	spec0 := regexp.MustCompile("(.+[.])?127.0.0.1:\\d+")
	spec1 := regexp.MustCompile("(.+[.])?192.168.\\d+.\\d+:\\d+")
	spec2 := regexp.MustCompile("(.+[.])?fieldkit.org:\\d+")
	spec3 := regexp.MustCompile("(.+[.])?local.fkdev.org:\\d+")
	spec4 := regexp.MustCompile("(.+[.])?localhost:\\d+")
	origHndlr := h.(http.HandlerFunc)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		origin := r.Header.Get("Origin")
		if origin == "" {
			// Not a CORS request
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec0) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec1) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec2) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec3) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOriginRegexp(origin, spec4) {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https://*.fieldkit.org") {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https://*.fieldkit.org:8080") {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https://*.fkdev.org") {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https://fieldkit.org") {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https://fieldkit.org:8080") {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		if cors.MatchOrigin(origin, "https://fkdev.org") {
			w.Header().Set("Access-Control-Allow-Origin", origin)
			w.Header().Set("Vary", "Origin")
			w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
			w.Header().Set("Access-Control-Allow-Credentials", "false")
			if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
				// We are handling a preflight request
				w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
				w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
			}
			origHndlr(w, r)
			return
		}
		origHndlr(w, r)
		return
	})
}
