001package io.jstach.opt.spring.webflux.example;
002
003import org.springframework.beans.factory.annotation.Autowired;
004import org.springframework.context.ApplicationContext;
005import org.springframework.context.annotation.Bean;
006import org.springframework.context.annotation.Configuration;
007import org.springframework.http.codec.ServerCodecConfigurer;
008import org.springframework.web.reactive.config.WebFluxConfigurer;
009
010import io.jstach.jstachio.JStachio;
011import io.jstach.opt.spring.web.JStachioHttpMessageConverter;
012import io.jstach.opt.spring.webflux.JStachioEncoder;
013import io.jstach.opt.spring.webflux.JStachioModelViewConfigurer;
014import io.jstach.opt.spring.webflux.ViewSetupBeanPostProcessor;
015
016/**
017 * Configures MVC using {@link JStachioHttpMessageConverter} to allow returning models
018 * which will be rendered using JStachio runtime.
019 *
020 * @author agentgt
021 * @see JStachioHttpMessageConverter
022 */
023@Configuration
024public class WebConfig implements WebFluxConfigurer {
025
026        private final JStachio jstachio;
027
028        /**
029         * Configures based on the jstachio found by spring
030         * @param jstachio the found jstachio
031         */
032        @Autowired
033        public WebConfig(JStachio jstachio) {
034                this.jstachio = jstachio;
035        }
036
037        @Override
038        public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
039                configurer.customCodecs().register(new JStachioEncoder(jstachio));
040        }
041
042        /**
043         * Factory to create a bean post processor to register
044         * {@link JStachioModelViewConfigurer}s.
045         * @param context supplied by spring
046         * @return a post processor that will configure JStachio Model Views before being
047         * rendered.
048         */
049        @Bean
050        public ViewSetupBeanPostProcessor viewSetupHandlerInterceptor(ApplicationContext context) {
051                return new ViewSetupBeanPostProcessor(context);
052        }
053
054}