|
| 1 | +package com.kfyty.benchmark.example.controller; |
| 2 | + |
| 3 | +import com.kfyty.benchmark.example.model.Fortune; |
| 4 | +import com.kfyty.benchmark.example.model.Fortunes; |
| 5 | +import com.kfyty.benchmark.example.model.World; |
| 6 | +import com.kfyty.benchmark.example.repository.DbRepository; |
| 7 | +import com.kfyty.benchmark.example.utils.Utils; |
| 8 | +import com.kfyty.loveqq.framework.web.core.annotation.GetMapping; |
| 9 | +import com.kfyty.loveqq.framework.web.core.annotation.RestController; |
| 10 | +import com.kfyty.loveqq.framework.web.core.annotation.bind.RequestParam; |
| 11 | +import com.kfyty.loveqq.framework.web.core.http.ServerResponse; |
| 12 | +import io.jstach.jstachio.JStachio; |
| 13 | + |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.Comparator; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +@RestController |
| 21 | +public class WebMvcController { |
| 22 | + private static final byte[] TEXT_BODY = "Hello, World!".getBytes(StandardCharsets.UTF_8); |
| 23 | + |
| 24 | + private final DbRepository dbRepository; |
| 25 | + |
| 26 | + public WebMvcController(DbRepository dbRepository) { |
| 27 | + this.dbRepository = dbRepository; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * GET /plaintext HTTP/1.1 |
| 32 | + */ |
| 33 | + @GetMapping(value = "/plaintext", produces = "text/plain; charset=UTF-8") |
| 34 | + public byte[] plaintext(ServerResponse response) { |
| 35 | + response.setHeader("Content-Length", "13"); |
| 36 | + return TEXT_BODY; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * GET /json HTTP/1.1 |
| 41 | + */ |
| 42 | + @GetMapping(value = "/json") |
| 43 | + public Map<String, String> json(ServerResponse response) { |
| 44 | + response.setHeader("Content-Length", "27"); |
| 45 | + return Map.of("message", "Hello, world!"); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * GET /db HTTP/1.1 |
| 50 | + */ |
| 51 | + @GetMapping("/db") |
| 52 | + public World db() { |
| 53 | + return dbRepository.getWorld(Utils.randomWorldNumber()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * GET /queries?queries=10 HTTP/1.1 |
| 58 | + */ |
| 59 | + @GetMapping("/queries") |
| 60 | + public World[] queries(@RequestParam(defaultValue = "1") Integer queries) { |
| 61 | + return Utils.randomWorldNumbers().mapToObj(dbRepository::getWorld).limit(queries).toArray(World[]::new); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * GET /updates?queries=10 HTTP/1.1 |
| 66 | + */ |
| 67 | + @GetMapping("/updates") |
| 68 | + public List<World> updates(@RequestParam(defaultValue = "1") Integer queries) { |
| 69 | + List<World> worlds = Utils.randomWorldNumbers() |
| 70 | + .mapToObj(id -> { |
| 71 | + World world = dbRepository.getWorld(id); |
| 72 | + int randomNumber; |
| 73 | + do { |
| 74 | + randomNumber = Utils.randomWorldNumber(); |
| 75 | + } while (randomNumber == world.randomNumber); |
| 76 | + world.randomNumber = randomNumber; |
| 77 | + return world; |
| 78 | + }) |
| 79 | + .limit(queries) |
| 80 | + .sorted(Comparator.comparingInt(w -> w.id)) |
| 81 | + .toList(); |
| 82 | + dbRepository.updateWorlds(worlds); |
| 83 | + return worlds; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * GET /fortunes HTTP/1.1 |
| 88 | + */ |
| 89 | + @GetMapping(value = "/fortunes", produces = "text/html; charset=UTF-8") |
| 90 | + public String fortunes() { |
| 91 | + List<Fortune> fortunes = dbRepository.fortunes(); |
| 92 | + fortunes.add(new Fortune(0, "Additional fortune added at request time.")); |
| 93 | + |
| 94 | + Collections.sort(fortunes); |
| 95 | + |
| 96 | + return JStachio.render(new Fortunes(fortunes)); |
| 97 | + } |
| 98 | +} |
0 commit comments