package com.sw.appcloudv1.controller;

import com.sw.appcloudv1.service.ShortUrlService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by shiwn on 2022/2/24 10:33
 */
@RestController
public class ShortUrlController {
    @Resource
    private ShortUrlService shortUrlService;
    @Resource
    private HttpServletResponse httpServletResponse;

    @GetMapping(value = "/deCode/{shortUrlKey}")
    public void deCode(@PathVariable String shortUrlKey) {
        String url = shortUrlService.getLongUrl(shortUrlKey);
        if (url == null) {
            return;
        }
        try {
            //重定向到原始的url
            httpServletResponse.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
