diff --git a/SETUP.md b/SETUP.md new file mode 100644 index 0000000..a9d2b87 --- /dev/null +++ b/SETUP.md @@ -0,0 +1,100 @@ +# Spring Boot Banner Generator — Setup & Usage + +## Running the App + +```bash +mvn spring-boot:run +``` + +The app starts on `http://localhost:8080`. + +--- + +## Web UI + +Open your browser and go to: + +``` +http://localhost:8080/banner +``` + +Upload an image, choose light or dark mode, and the colored HTML banner is rendered in the page. + +--- + +## REST API + +### Generate a Banner + +**Endpoint:** `POST /api/banner` +**Content-Type:** `multipart/form-data` + +| Field | Type | Required | Description | +|---------|---------|----------|------------------------------------| +| `image` | file | yes | Image to convert (PNG, JPG, etc.) | +| `dark` | boolean | no | Use dark color scheme (default: false) | + +**Curl example:** + +```bash +curl -X POST http://localhost:8080/api/banner \ + -F "image=@./logo.png" \ + -F "dark=false" +``` + +**Response (JSON):** + +```json +{ + "ansi": "${AnsiColor.RED}@@@@...", + "html": "@..." +} +``` + +--- + +## Which Output to Use + +### `ansi` — Colored terminal banner for Spring Boot apps + +The `ansi` field contains Spring Boot's `${AnsiColor.X}` syntax. Paste it into `src/main/resources/banner.txt` in **any Spring Boot project** and it will print in color when the app starts. + +**Extract and save directly:** + +```bash +curl -s -X POST http://localhost:8080/api/banner \ + -F "image=@./logo.png" \ + -F "dark=false" \ + | python -c "import sys,json; print(json.load(sys.stdin)['ansi'])" \ + > /path/to/your-spring-app/src/main/resources/banner.txt +``` + +Then start your Spring Boot app — the banner prints in color in the terminal. + +### `html` — Colored banner for web pages + +The `html` field contains inline `` tags. Drop it inside a `
` tag in any HTML page:
+
+```html
+
+  
+
+``` + +--- + +## Swagger UI + +Interactive API docs are available at: + +``` +http://localhost:8080/swagger-ui.html +``` + +Use it to explore and test the `/api/banner` endpoint directly from the browser — upload a file, toggle dark mode, and see the live JSON response. + +The raw OpenAPI spec (JSON) is at: + +``` +http://localhost:8080/v2/api-docs +``` diff --git a/pom.xml b/pom.xml index 228d8db..e33e861 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ UTF-8 1.8 + 1.18.30 @@ -37,6 +38,7 @@ org.projectlombok lombok + 1.18.30 org.springframework.boot @@ -58,6 +60,21 @@ 1.0.0.RELEASE test + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + + + com.google.guava + guava + 20.0 + @@ -77,6 +94,16 @@ org.springframework.boot spring-boot-maven-plugin + + --add-opens=java.base/java.lang=ALL-UNNAMED + + + + org.apache.maven.plugins + maven-surefire-plugin + + --add-opens=java.base/java.lang=ALL-UNNAMED + diff --git a/src/main/java/be/ordina/cloudfoundry/config/SwaggerConfig.java b/src/main/java/be/ordina/cloudfoundry/config/SwaggerConfig.java new file mode 100644 index 0000000..80447aa --- /dev/null +++ b/src/main/java/be/ordina/cloudfoundry/config/SwaggerConfig.java @@ -0,0 +1,23 @@ +package be.ordina.cloudfoundry.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfig { + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.basePackage("be.ordina.cloudfoundry.controller")) + .paths(PathSelectors.ant("/api/**")) + .build(); + } +} diff --git a/src/main/java/be/ordina/cloudfoundry/controller/BannerRestController.java b/src/main/java/be/ordina/cloudfoundry/controller/BannerRestController.java new file mode 100644 index 0000000..296ce84 --- /dev/null +++ b/src/main/java/be/ordina/cloudfoundry/controller/BannerRestController.java @@ -0,0 +1,46 @@ +package be.ordina.cloudfoundry.controller; + +import be.ordina.cloudfoundry.banner.Banner; +import be.ordina.cloudfoundry.banner.BannerGenerator; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.util.LinkedHashMap; +import java.util.Map; + +@Slf4j +@RestController +@RequestMapping("/api/banner") +@Api(value = "Banner Generator", description = "Generate Spring Boot ASCII banners from images") +public class BannerRestController { + + @Autowired + private BannerGenerator bannerGenerator; + + @ApiOperation(value = "Generate a banner from an image", response = Map.class) + @RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> generateBanner( + @ApiParam(value = "Image file to convert to ASCII banner", required = true) + @RequestParam("image") MultipartFile image, + @ApiParam(value = "Use dark color scheme", defaultValue = "false") + @RequestParam(value = "dark", defaultValue = "false") boolean dark) { + + log.info("REST request for image [{}]", image.getOriginalFilename()); + Banner banner = bannerGenerator.generateBanner(image, dark); + + Map response = new LinkedHashMap<>(); + response.put("ansi", banner.getAnsi()); + response.put("html", banner.getHtml()); + return ResponseEntity.ok(response); + } +}