This commit is contained in:
TheEnd 2022-07-31 20:21:14 +08:00
parent 00e9949550
commit c767deb2ad
12 changed files with 216 additions and 72 deletions

View File

@ -18,7 +18,8 @@
<module>zy-shorturl-core</module> <module>zy-shorturl-core</module>
<module>zy-shorturl-spring-boot-starter</module> <module>zy-shorturl-spring-boot-starter</module>
<module>zy-shorturl-store</module> <module>zy-shorturl-store</module>
<module>zu-shorturl-web</module> <module>zy-shorturl-server</module>
<module>zu-shorturl-web-spring-boot-starter</module>
</modules> </modules>
<properties> <properties>

View File

@ -9,7 +9,7 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>zu-shorturl-web</artifactId> <artifactId>zu-shorturl-web-spring-boot-starter</artifactId>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
@ -17,38 +17,29 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- guava --> <!-- spring-boot -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- zy-shorturl -->
<dependency>
<groupId>cn.zzzykj</groupId>
<artifactId>zy-short-url-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- SpringBoot -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>
</dependency> </dependency>
<!-- short-url-spring-boot-starter -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>cn.zzzykj</groupId>
<artifactId>spring-boot-autoconfigure</artifactId> <artifactId>zy-short-url-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<!-- lombok --> <!-- lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional>
<scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,18 @@
package cn.shorturl.web.boot.starter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author ZhuoQinghui
* @version 1.0.0
* Create By 2022/7/31 19:41
*/
@Slf4j
@Configuration
@ComponentScan("cn.shorturl.web.boot.starter.controller")
@EnableConfigurationProperties
public class ShortUrlWebAutoConfiguration {
}

View File

@ -0,0 +1,39 @@
package cn.shorturl.web.boot.starter.controller;
import cn.shorturl.core.ShortUrl;
import cn.shorturl.store.ShortUrlStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
/**
* @author ZhuoQinghui
* @version 1.0.0
* Create By 2022/7/31 16:41
*/
@Slf4j
@RestController
@ConditionalOnProperty(prefix = "shorturl.api", name = "enable")
@RequestMapping("${shorturl.api.url-suffix}")
public class ShortUrlController {
@Resource
private ShortUrlStore store;
@GetMapping("${shorturl.api.add}")
public ShortUrl add(@RequestParam String url, @RequestParam(required = false) Date expire) {
return store.add(url, expire);
}
@GetMapping("${shorturl.api.get}")
public ShortUrl get(@RequestParam String hash) {
return store.get(hash);
}
}

View File

@ -0,0 +1,41 @@
package cn.shorturl.web.boot.starter.controller;
import cn.shorturl.core.ShortUrl;
import cn.shorturl.store.ShortUrlStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author ZhuoQinghui
* @version 1.0.0
* Create By 2022/7/31 18:57
*/
@Controller
@ConditionalOnProperty(prefix = "shorturl.redirect", name = "enable")
@RequestMapping("${shorturl.redirect.url-suffix}")
public class ToController {
@Resource
private ShortUrlStore store;
@RequestMapping("/{hash}")
public void to(@PathVariable String hash, HttpServletResponse response) {
ShortUrl url = store.get(hash);
if (url == null) {
throw new IllegalArgumentException("hash not found");
}
try {
response.sendRedirect(url.getUrl());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.shorturl.web.boot.starter.ShortUrlWebAutoConfiguration

View File

@ -1,40 +0,0 @@
package cn.shorturl.web.controller;
import cn.shorturl.core.ShortUrlConfig;
import cn.shorturl.store.ShortUrlStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* @author ZhuoQinghui
* @version 1.0.0
* Create By 2022/7/31 16:41
*/
@Slf4j
@RestController
@RequestMapping("/su")
public class ShortUrlController {
@Resource
private ShortUrlStore store;
@PostConstruct
public void init() {
log.info("store: {}", store);
String url = "https://www.baidu.com";
String hash = store.hash(url);
log.info("hash: {}", hash);
store.add(url);
}
@GetMapping("/gen")
public String gen() {
return "";
}
}

View File

@ -1,7 +0,0 @@
server:
port: 8090
shorturl:
store:
retryMax: 6
store-type: redis

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zy-short-url</artifactId>
<groupId>cn.zzzykj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zy-shorturl-server</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<!-- zy-shorturl -->
<dependency>
<groupId>cn.zzzykj</groupId>
<artifactId>zu-shorturl-web-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,8 +1,9 @@
package cn.shorturl.web; package cn.shorturl.server;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/** /**
* @author ZhuoQinghui * @author ZhuoQinghui
* @version 1.0.0 * @version 1.0.0

View File

@ -0,0 +1,24 @@
server:
port: 8090
spring:
redis:
host: '192.168.1.202'
port: 6379
timeout: 60000
database: 12
password: '123456'
shorturl:
redirect:
enable: true
url-suffix: '/to'
api:
enable: true
url-suffix: '/api'
add: '/add'
get: '/get'
store:
retryMax: 6
store-type: redis

View File

@ -13,12 +13,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.PostConstruct;
/** /**
* @author ZhuoQinghui * @author ZhuoQinghui