├── .editorconfig ├── .env ├── .gitignore ├── deploy.sh ├── docker-compose.production.yml ├── docker-compose.yml ├── docs ├── architecture-diagram.png └── screenshot.png ├── ecs-params.yml ├── qr-code-service ├── .dockerignore ├── .gitignore ├── Dockerfile ├── protos │ └── qr_code_service.proto ├── requirements.txt └── server.py ├── readme.md └── web ├── .dockerignore ├── .gitignore ├── Dockerfile ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── settings.gradle └── src ├── main ├── kotlin │ └── com │ │ └── piotrjanczyk │ │ └── qrcodegenerator │ │ └── web │ │ ├── Config.kt │ │ ├── ImageController.kt │ │ ├── IndexController.kt │ │ ├── QrCodeGeneratorApplication.kt │ │ ├── QrCodeServiceClient.kt │ │ └── dto.kt ├── proto │ └── qr_code_service.proto └── resources │ ├── application.properties │ └── templates │ └── index.html └── test └── kotlin └── com └── piotrjanczyk └── qrcodegenerator └── web └── QrCodeGeneratorApplicationTests.kt /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | COMPOSE_PROJECT_NAME=qr-code-generator 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | cd "$(dirname "$0")" 4 | 5 | echo "-> Building Docker images..." 6 | docker-compose build 7 | 8 | echo "-> Pushing images to Docker registry..." 9 | docker push pjanczyk/qr-code-generator_web 10 | docker push pjanczyk/qr-code-generator_qr-code-service 11 | 12 | echo "-> Recreating ECS containers..." 13 | ecs-cli compose --file docker-compose.production.yml service stop 14 | sleep 10 15 | ecs-cli compose --file docker-compose.production.yml service start --force-deployment 16 | 17 | echo "-> Done" 18 | -------------------------------------------------------------------------------- /docker-compose.production.yml: -------------------------------------------------------------------------------- 1 | version: "3.0" 2 | 3 | services: 4 | qr-code-service: 5 | image: pjanczyk/qr-code-generator_web 6 | ports: 7 | - "5000:5000" 8 | 9 | web: 10 | image: pjanczyk/qr-code-generator_qr-code-service 11 | ports: 12 | - "80:80" 13 | environment: 14 | QR_CODE_GENERATOR_BASE_URL: http://qrcode.piotrjanczyk.com 15 | QR_CODE_GENERATOR_QR_CODE_SERVICE_ADDRESS: qrcode.piotrjanczyk.com:5000 16 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.0" 2 | 3 | services: 4 | qr-code-service: 5 | build: qr-code-service 6 | image: pjanczyk/qr-code-generator_web 7 | 8 | web: 9 | build: web 10 | image: pjanczyk/qr-code-generator_qr-code-service 11 | ports: 12 | - "80:80" 13 | environment: 14 | QR_CODE_GENERATOR_BASE_URL: http://localhost 15 | QR_CODE_GENERATOR_QR_CODE_SERVICE_ADDRESS: qr-code-service:5000 16 | -------------------------------------------------------------------------------- /docs/architecture-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pjanczyk/qr-code-generator/e2518e9dc255ba1c123ee5b224a1a6dbbb731460/docs/architecture-diagram.png -------------------------------------------------------------------------------- /docs/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pjanczyk/qr-code-generator/e2518e9dc255ba1c123ee5b224a1a6dbbb731460/docs/screenshot.png -------------------------------------------------------------------------------- /ecs-params.yml: -------------------------------------------------------------------------------- 1 | version: 1 2 | task_definition: 3 | services: 4 | qr-code-service: 5 | cpu_shares: 50 6 | mem_limit: 250000000 # 250 MB 7 | web: 8 | cpu_shares: 50 9 | mem_limit: 500000000 # 500 MB 10 | -------------------------------------------------------------------------------- /qr-code-service/.dockerignore: -------------------------------------------------------------------------------- 1 | /qr_code_service_pb2.py 2 | /qr_code_service_pb2_grpc.py 3 | *.iml 4 | -------------------------------------------------------------------------------- /qr-code-service/.gitignore: -------------------------------------------------------------------------------- 1 | /qr_code_service_pb2.py 2 | /qr_code_service_pb2_grpc.py 3 | *.iml -------------------------------------------------------------------------------- /qr-code-service/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.8.3 2 | 3 | WORKDIR /app 4 | 5 | COPY requirements.txt ./ 6 | RUN pip install --no-cache-dir -r requirements.txt 7 | 8 | COPY . . 9 | 10 | RUN python -m grpc_tools.protoc --proto_path=protos --python_out=. --grpc_python_out=. protos/qr_code_service.proto 11 | 12 | EXPOSE 5000 13 | 14 | CMD [ "python", "-u", "server.py" ] 15 | -------------------------------------------------------------------------------- /qr-code-service/protos/qr_code_service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | option java_package = "com.piotrjanczyk.qrcodegenerator.proto"; 4 | option java_multiple_files = true; 5 | 6 | package qrcodegenerator; 7 | 8 | service QrCodeService { 9 | rpc CreateQrCode (CreateQrCodeRequest) returns (QrCodeResponse) { 10 | } 11 | } 12 | 13 | message CreateQrCodeRequest { 14 | /** Data to be encoded in the QR code. */ 15 | string data = 1; 16 | 17 | /** 18 | * Integer from 1 to 40 that control the size of the QR Code. 19 | * If not provided, it will be determined from the size of `data`. 20 | */ 21 | int32 version = 2; 22 | 23 | ErrorCorrection error_correction = 3; 24 | 25 | /** Size of each "box" of the QR code, in pixels. */ 26 | int32 box_size = 4; 27 | 28 | /** Empty space around the QR code, in pixels. */ 29 | int32 border = 5; 30 | 31 | /** Foreground color, in #RRGGBB format. */ 32 | string fill_color = 6; 33 | 34 | /** Background color, in #RRGGBB format. */ 35 | string back_color = 7; 36 | 37 | ImageFormat image_format = 8; 38 | } 39 | 40 | enum ErrorCorrection { 41 | /** 7% redundancy */ 42 | LOW = 0; 43 | /** 15% redundancy */ 44 | MEDIUM = 1; 45 | /** 25% redundancy */ 46 | QUARTILE = 2; 47 | /** 30% redundancy */ 48 | HIGH = 3; 49 | } 50 | 51 | enum ImageFormat { 52 | PNG = 0; 53 | SVG = 1; 54 | } 55 | 56 | message QrCodeResponse { 57 | /** Content of PNG or SVG file */ 58 | bytes file = 1; 59 | } 60 | -------------------------------------------------------------------------------- /qr-code-service/requirements.txt: -------------------------------------------------------------------------------- 1 | grpcio ~= 1.23 2 | grpcio-tools 3 | qrcode 4 | pillow 5 | -------------------------------------------------------------------------------- /qr-code-service/server.py: -------------------------------------------------------------------------------- 1 | import io 2 | from concurrent.futures import ThreadPoolExecutor 3 | 4 | import grpc 5 | import qrcode 6 | import qrcode.image.pil 7 | import qrcode.image.svg 8 | 9 | from qr_code_service_pb2 import ErrorCorrection, ImageFormat, QrCodeResponse 10 | from qr_code_service_pb2_grpc import QrCodeServiceServicer, add_QrCodeServiceServicer_to_server 11 | 12 | 13 | class SvgImageFactory(qrcode.image.svg.SvgPathImage): 14 | def __init__(self, *args, fill_color, back_color, **kwargs): 15 | self.background = back_color 16 | self.QR_PATH_STYLE = f'fill:{fill_color};fill-opacity:1;fill-rule:nonzero;stroke:none' 17 | super().__init__(*args, **kwargs) 18 | 19 | def _svg(self, **kwargs): 20 | svg = super()._svg(**kwargs) 21 | return svg 22 | 23 | 24 | class QrCodeService(QrCodeServiceServicer): 25 | def CreateQrCode(self, request, context): 26 | version = request.version if 1 <= request.version <= 40 else None 27 | 28 | error_correction = { 29 | ErrorCorrection.LOW: qrcode.constants.ERROR_CORRECT_L, 30 | ErrorCorrection.MEDIUM: qrcode.constants.ERROR_CORRECT_M, 31 | ErrorCorrection.QUARTILE: qrcode.constants.ERROR_CORRECT_Q, 32 | ErrorCorrection.HIGH: qrcode.constants.ERROR_CORRECT_H, 33 | }[request.error_correction] 34 | 35 | image_factory = { 36 | ImageFormat.PNG: qrcode.image.pil.PilImage, 37 | ImageFormat.SVG: SvgImageFactory 38 | }[request.image_format] 39 | 40 | qr = qrcode.QRCode(version=version, 41 | error_correction=error_correction, 42 | box_size=request.box_size, 43 | border=request.border, 44 | image_factory=image_factory) 45 | qr.add_data(request.data) 46 | image = qr.make_image(fill_color=request.fill_color, 47 | back_color=request.back_color) 48 | 49 | bytes_io = io.BytesIO() 50 | image.save(bytes_io) 51 | byte_array = bytes_io.getvalue() 52 | 53 | return QrCodeResponse(file=byte_array) 54 | 55 | 56 | def serve(): 57 | print("Starting server...") 58 | server = grpc.server(ThreadPoolExecutor(max_workers=10)) 59 | add_QrCodeServiceServicer_to_server(QrCodeService(), server) 60 | server.add_insecure_port('[::]:5000') 61 | server.start() 62 | print("Listening on port 5000") 63 | server.wait_for_termination() 64 | 65 | 66 | if __name__ == '__main__': 67 | serve() 68 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # QR Code Generator 2 | 3 | ### Demo 4 | 5 | [qrcode.piotrjanczyk.com](http://qrcode.piotrjanczyk.com/) 6 | 7 | 8 | 9 | ### Architecture 10 | 11 | 12 | 13 | ### Implementation details 14 | 15 | _**qr-code-service**_ is a microservice implemented in Python which uses [qrcode](https://github.com/lincolnloop/python-qrcode) library. 16 | It provides a gRPC interface for generating QR codes. 17 | 18 | _**web**_ is a web application written in Spring and Kotlin. It uses Thymeleaf for server-side rendering. 19 | 20 | Both services are dockerized. 21 | Docker Compose configuration is used for deployment on AWS Elastic Container Service. 22 | 23 | ### UI Design 24 | 25 | [Figma project](https://www.figma.com/file/m0zkjHTBtYOHYB327GsUou/QR_Code_Generator?node-id=0%3A1) 26 | 27 | ### Local development 28 | 29 | All services can be run with Docker Compose: 30 | 31 | ```bash 32 | docker-compose up --build 33 | ``` 34 | 35 | ### Deployment 36 | 37 | See [`deploy.sh`](deploy.sh) script. 38 | 39 | It builds and deploys to [qrcode.piotrjanczyk.com](http://qrcode.piotrjanczyk.com/): 40 | * Builds Docker images 41 | * Pushes them to Docker Hub 42 | * Deploys them to AWS ECS 43 | -------------------------------------------------------------------------------- /web/.dockerignore: -------------------------------------------------------------------------------- 1 | /.gradle 2 | /build 3 | -------------------------------------------------------------------------------- /web/.gitignore: -------------------------------------------------------------------------------- 1 | /.gradle 2 | /build -------------------------------------------------------------------------------- /web/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:11-jdk AS builder 2 | 3 | WORKDIR /app 4 | 5 | ENV GRADLE_OPTS -Dorg.gradle.daemon=false 6 | COPY gradle ./gradle 7 | COPY gradlew build.gradle settings.gradle ./ 8 | RUN ./gradlew build -x check || true 9 | RUN ./gradlew generateProto || true 10 | COPY . ./ 11 | RUN ./gradlew build -x check 12 | 13 | FROM openjdk:11-jre 14 | COPY --from=builder /app/build/libs/web-1.0.0.jar /app/web.jar 15 | EXPOSE 8000 16 | CMD java -server -jar /app/web.jar 17 | -------------------------------------------------------------------------------- /web/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "idea" 3 | id "org.springframework.boot" version "2.3.0.RELEASE" 4 | id "io.spring.dependency-management" version "1.0.9.RELEASE" 5 | id "org.jetbrains.kotlin.jvm" version "1.3.72" 6 | id "org.jetbrains.kotlin.plugin.spring" version "1.3.72" 7 | id "org.jetbrains.kotlin.kapt" version "1.3.72" 8 | id "com.google.protobuf" version "0.8.12" 9 | } 10 | 11 | group = "com.piotrjanczyk.qrcodegenerator.web" 12 | version = "1.0.0" 13 | java.sourceCompatibility = JavaVersion.VERSION_11 14 | 15 | repositories { 16 | mavenCentral() 17 | } 18 | 19 | dependencies { 20 | implementation "org.springframework.boot:spring-boot-starter-thymeleaf" 21 | implementation "org.springframework.boot:spring-boot-starter-webflux" 22 | implementation "com.fasterxml.jackson.module:jackson-module-kotlin" 23 | implementation "io.projectreactor.kotlin:reactor-kotlin-extensions" 24 | implementation "org.jetbrains.kotlin:kotlin-reflect" 25 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 26 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-reactor" 27 | 28 | implementation "com.google.protobuf:protobuf-gradle-plugin:0.8.11" 29 | implementation "com.google.protobuf:protobuf-java:3.11.1" 30 | implementation "com.google.protobuf:protobuf-java-util:3.11.1" 31 | implementation "io.grpc:grpc-netty-shaded:1.26.0" 32 | implementation "io.grpc:grpc-protobuf:1.26.0" 33 | implementation "io.grpc:grpc-stub:1.26.0" 34 | implementation "io.grpc:grpc-kotlin-stub:0.1.1" 35 | 36 | kapt "org.springframework.boot:spring-boot-configuration-processor" 37 | 38 | testImplementation("org.springframework.boot:spring-boot-starter-test") { 39 | exclude group: "org.junit.vintage", module: "junit-vintage-engine" 40 | } 41 | testImplementation "io.projectreactor:reactor-test" 42 | } 43 | 44 | test { 45 | useJUnitPlatform() 46 | } 47 | 48 | compileKotlin { 49 | kotlinOptions { 50 | freeCompilerArgs = ["-Xjsr305=strict"] 51 | jvmTarget = "1.8" 52 | } 53 | } 54 | 55 | protobuf { 56 | protoc { artifact = "com.google.protobuf:protoc:3.6.1" } 57 | plugins { 58 | grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.27.0" } 59 | grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:0.1.1" } 60 | } 61 | generateProtoTasks { 62 | all().each { task -> 63 | task.plugins { 64 | grpc {} 65 | grpckt {} 66 | } 67 | } 68 | } 69 | } 70 | 71 | idea { 72 | module { 73 | def kaptMain = file("${project.buildDir}/generated/source/kapt/main") 74 | sourceDirs += kaptMain 75 | generatedSourceDirs += kaptMain 76 | 77 | outputDir file("${project.buildDir}/classes/main") 78 | testOutputDir file("${project.buildDir}/classes/test") 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /web/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pjanczyk/qr-code-generator/e2518e9dc255ba1c123ee5b224a1a6dbbb731460/web/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /web/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /web/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin or MSYS, switch paths to Windows format before running java 129 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=`expr $i + 1` 158 | done 159 | case $i in 160 | 0) set -- ;; 161 | 1) set -- "$args0" ;; 162 | 2) set -- "$args0" "$args1" ;; 163 | 3) set -- "$args0" "$args1" "$args2" ;; 164 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=`save "$@"` 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | exec "$JAVACMD" "$@" 184 | -------------------------------------------------------------------------------- /web/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "web" 2 | -------------------------------------------------------------------------------- /web/src/main/kotlin/com/piotrjanczyk/qrcodegenerator/web/Config.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties 4 | import org.springframework.context.annotation.Configuration 5 | import kotlin.properties.Delegates.notNull 6 | 7 | @Configuration 8 | @ConfigurationProperties(prefix = "qr-code-generator") 9 | class Config { 10 | var baseUrl by notNull() 11 | var qrCodeServiceAddress by notNull() 12 | } 13 | -------------------------------------------------------------------------------- /web/src/main/kotlin/com/piotrjanczyk/qrcodegenerator/web/ImageController.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | import com.piotrjanczyk.qrcodegenerator.web.ImageFormat.PNG 4 | import com.piotrjanczyk.qrcodegenerator.web.ImageFormat.SVG 5 | import org.springframework.http.HttpHeaders 6 | import org.springframework.http.HttpStatus 7 | import org.springframework.http.MediaType 8 | import org.springframework.http.ResponseEntity 9 | import org.springframework.web.bind.annotation.GetMapping 10 | import org.springframework.web.bind.annotation.RequestParam 11 | import org.springframework.web.bind.annotation.RestController 12 | 13 | @RestController 14 | class ImageController( 15 | private val qrCodeServiceClient: QrCodeServiceClient 16 | ) { 17 | @GetMapping("/qr") 18 | suspend fun getImage( 19 | @RequestParam("data") data: String, 20 | @RequestParam("format", required = false) imageFormat: ImageFormat?, 21 | @RequestParam("ec", required = false) errorCorrection: ErrorCorrection?, 22 | @RequestParam("version", required = false) version: Int?, 23 | @RequestParam("fg", required = false) foregroundColor: String?, 24 | @RequestParam("bg", required = false) backgroundColor: String?, 25 | @RequestParam("box", required = false) boxSize: Int?, 26 | @RequestParam("border", required = false) border: Int? 27 | ): ResponseEntity { 28 | val definition = QrCodeDefinition.build( 29 | data = data, 30 | imageFormat = imageFormat, 31 | version = version, 32 | errorCorrection = errorCorrection, 33 | foregroundColor = foregroundColor, 34 | backgroundColor = backgroundColor, 35 | boxSize = boxSize, 36 | border = border 37 | ) 38 | val bytes = qrCodeServiceClient.createQrCode(definition) 39 | val headers = HttpHeaders().apply { 40 | contentType = when (definition.imageFormat) { 41 | PNG -> MediaType.IMAGE_PNG 42 | SVG -> MediaType("image", "svg+xml") 43 | } 44 | } 45 | return ResponseEntity(bytes, headers, HttpStatus.OK) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /web/src/main/kotlin/com/piotrjanczyk/qrcodegenerator/web/IndexController.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | import org.springframework.stereotype.Controller 4 | import org.springframework.ui.Model 5 | import org.springframework.web.bind.annotation.GetMapping 6 | import org.springframework.web.bind.annotation.RequestParam 7 | import org.springframework.web.util.UriUtils 8 | 9 | @Controller 10 | class IndexController( 11 | private val config: Config 12 | ) { 13 | @GetMapping("/") 14 | fun home( 15 | @RequestParam(required = false) data: String?, 16 | @RequestParam(required = false) imageFormat: ImageFormat?, 17 | @RequestParam(required = false) errorCorrection: ErrorCorrection?, 18 | @RequestParam(required = false) version: Int?, 19 | @RequestParam(required = false) foregroundColor: String?, 20 | @RequestParam(required = false) backgroundColor: String?, 21 | @RequestParam(required = false) boxSize: Int?, 22 | @RequestParam(required = false) border: Int?, 23 | model: Model 24 | ): String { 25 | val definition = QrCodeDefinition.build( 26 | data = data ?: "", 27 | imageFormat = imageFormat, 28 | errorCorrection = errorCorrection, 29 | version = version, 30 | foregroundColor = foregroundColor, 31 | backgroundColor = backgroundColor, 32 | boxSize = boxSize, 33 | border = border 34 | ) 35 | model.addAttribute("definition", definition) 36 | if (definition.data.isNotBlank()) { 37 | model.addAttribute("url", buildImageUrl(definition)) 38 | } 39 | return "index" 40 | } 41 | 42 | fun buildImageUrl(definition: QrCodeDefinition): String { 43 | var url = "${config.baseUrl}/qr" 44 | url += "?data=${UriUtils.encodeQueryParam(definition.data, Charsets.UTF_8)}" 45 | url += "&format=${definition.imageFormat}" 46 | if (definition.hasCustomErrorCorrection) { 47 | url += "&ec=${definition.errorCorrection}" 48 | } 49 | if (definition.hasCustomVersion) { 50 | url += "&version=${definition.version}" 51 | } 52 | if (definition.hasCustomForegroundColor) { 53 | url += "&fg=${UriUtils.encodeQueryParam(definition.foregroundColor, Charsets.UTF_8)}" 54 | } 55 | if (definition.hasCustomBackgroundColor) { 56 | url += "&bg=${UriUtils.encodeQueryParam(definition.backgroundColor, Charsets.UTF_8)}" 57 | } 58 | if (definition.hasCustomBoxSize) { 59 | url += "&box=${definition.boxSize}" 60 | } 61 | if (definition.hasCustomBorder) { 62 | url += "&border=${definition.border}" 63 | } 64 | return url 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /web/src/main/kotlin/com/piotrjanczyk/qrcodegenerator/web/QrCodeGeneratorApplication.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication 4 | import org.springframework.boot.runApplication 5 | 6 | @SpringBootApplication 7 | class QrCodeGeneratorApplication 8 | 9 | fun main(args: Array) { 10 | runApplication(*args) 11 | } 12 | -------------------------------------------------------------------------------- /web/src/main/kotlin/com/piotrjanczyk/qrcodegenerator/web/QrCodeServiceClient.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | import com.piotrjanczyk.qrcodegenerator.proto.QrCodeServiceGrpcKt.QrCodeServiceCoroutineStub 4 | import com.piotrjanczyk.qrcodegenerator.web.ErrorCorrection.* 5 | import com.piotrjanczyk.qrcodegenerator.web.ImageFormat.PNG 6 | import com.piotrjanczyk.qrcodegenerator.web.ImageFormat.SVG 7 | import io.grpc.ManagedChannelBuilder 8 | import kotlinx.coroutines.Dispatchers 9 | import kotlinx.coroutines.asExecutor 10 | import org.springframework.stereotype.Component 11 | import java.io.Closeable 12 | import java.util.concurrent.TimeUnit 13 | import com.piotrjanczyk.qrcodegenerator.proto.CreateQrCodeRequest as PbCreateQrCodeRequest 14 | import com.piotrjanczyk.qrcodegenerator.proto.ErrorCorrection as PbErrorCorrection 15 | import com.piotrjanczyk.qrcodegenerator.proto.ImageFormat as PbImageFormat 16 | 17 | @Component 18 | class QrCodeServiceClient( 19 | config: Config 20 | ) : Closeable { 21 | private val channel = ManagedChannelBuilder 22 | .forTarget(config.qrCodeServiceAddress) 23 | .usePlaintext() 24 | .executor(Dispatchers.Default.asExecutor()) 25 | .build() 26 | 27 | private val stub = QrCodeServiceCoroutineStub(channel) 28 | 29 | suspend fun createQrCode(definition: QrCodeDefinition): ByteArray { 30 | val request = PbCreateQrCodeRequest.newBuilder() 31 | .setData(definition.data) 32 | .setVersion(definition.version ?: 0) 33 | .setErrorCorrection(when (definition.errorCorrection) { 34 | LOW -> PbErrorCorrection.LOW 35 | MEDIUM -> PbErrorCorrection.MEDIUM 36 | QUARTILE -> PbErrorCorrection.QUARTILE 37 | HIGH -> PbErrorCorrection.HIGH 38 | }) 39 | .setFillColor(definition.foregroundColor) 40 | .setBackColor(definition.backgroundColor) 41 | .setBoxSize(definition.boxSizeOrDefault) 42 | .setBorder(definition.borderOrDefault) 43 | .setImageFormat(when (definition.imageFormat) { 44 | PNG -> PbImageFormat.PNG 45 | SVG -> PbImageFormat.SVG 46 | }) 47 | .build() 48 | val response = stub.createQrCode(request) 49 | return response.file.toByteArray() 50 | } 51 | 52 | override fun close() { 53 | channel.shutdown().awaitTermination(5, TimeUnit.SECONDS) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /web/src/main/kotlin/com/piotrjanczyk/qrcodegenerator/web/dto.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | data class QrCodeDefinition( 4 | val data: String, 5 | val imageFormat: ImageFormat = ImageFormat.PNG, 6 | val version: Int? = null, 7 | val errorCorrection: ErrorCorrection = DEFAULT_ERROR_CORRECTION, 8 | val foregroundColor: String = DEFAULT_FOREGROUND_COLOR, 9 | val backgroundColor: String = DEFAULT_BACKGROUND_COLOR, 10 | val boxSize: Int? = null, 11 | val border: Int? = null 12 | ) { 13 | val hasCustomVersion: Boolean get() = version != null 14 | val hasCustomErrorCorrection: Boolean get() = errorCorrection != DEFAULT_ERROR_CORRECTION 15 | val hasCustomForegroundColor: Boolean get() = foregroundColor != DEFAULT_FOREGROUND_COLOR 16 | val hasCustomBackgroundColor: Boolean get() = backgroundColor != DEFAULT_BACKGROUND_COLOR 17 | val hasCustomBoxSize: Boolean get() = boxSize != null 18 | val hasCustomBorder: Boolean get() = border != null 19 | 20 | val boxSizeOrDefault: Int get() = boxSize ?: DEFAULT_BOX_SIZE 21 | val borderOrDefault: Int get() = border ?: DEFAULT_BORDER 22 | 23 | companion object { 24 | val DEFAULT_ERROR_CORRECTION = ErrorCorrection.MEDIUM 25 | const val DEFAULT_FOREGROUND_COLOR = "#000000" 26 | const val DEFAULT_BACKGROUND_COLOR = "#ffffff" 27 | const val DEFAULT_BOX_SIZE = 10 28 | const val DEFAULT_BORDER = 4 29 | 30 | fun build( 31 | data: String, 32 | imageFormat: ImageFormat? = null, 33 | version: Int? = null, 34 | errorCorrection: ErrorCorrection? = null, 35 | foregroundColor: String?, 36 | backgroundColor: String? = null, 37 | boxSize: Int? = null, 38 | border: Int? = null 39 | ): QrCodeDefinition = 40 | QrCodeDefinition( 41 | data = data, 42 | imageFormat = imageFormat ?: ImageFormat.PNG, 43 | version = version, 44 | errorCorrection = errorCorrection ?: DEFAULT_ERROR_CORRECTION, 45 | foregroundColor = foregroundColor ?: DEFAULT_FOREGROUND_COLOR, 46 | backgroundColor = backgroundColor ?: DEFAULT_BACKGROUND_COLOR, 47 | boxSize = boxSize, 48 | border = border 49 | ) 50 | } 51 | } 52 | 53 | enum class ImageFormat { 54 | PNG, 55 | SVG 56 | } 57 | 58 | enum class ErrorCorrection { 59 | LOW, 60 | MEDIUM, 61 | QUARTILE, 62 | HIGH 63 | } 64 | -------------------------------------------------------------------------------- /web/src/main/proto/qr_code_service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | option java_package = "com.piotrjanczyk.qrcodegenerator.proto"; 4 | option java_multiple_files = true; 5 | 6 | package qrcodegenerator; 7 | 8 | service QrCodeService { 9 | rpc CreateQrCode (CreateQrCodeRequest) returns (QrCodeResponse) { 10 | } 11 | } 12 | 13 | message CreateQrCodeRequest { 14 | /** Data to be encoded in the QR code. */ 15 | string data = 1; 16 | 17 | /** 18 | * Integer from 1 to 40 that control the size of the QR Code. 19 | * If not provided, it will be determined from the size of `data`. 20 | */ 21 | int32 version = 2; 22 | 23 | ErrorCorrection error_correction = 3; 24 | 25 | /** Size of each "box" of the QR code, in pixels. */ 26 | int32 box_size = 4; 27 | 28 | /** Empty space around the QR code, in pixels. */ 29 | int32 border = 5; 30 | 31 | /** Foreground color, in #RRGGBB format. */ 32 | string fill_color = 6; 33 | 34 | /** Background color, in #RRGGBB format. */ 35 | string back_color = 7; 36 | 37 | ImageFormat image_format = 8; 38 | } 39 | 40 | enum ErrorCorrection { 41 | /** 7% redundancy */ 42 | LOW = 0; 43 | /** 15% redundancy */ 44 | MEDIUM = 1; 45 | /** 25% redundancy */ 46 | QUARTILE = 2; 47 | /** 30% redundancy */ 48 | HIGH = 3; 49 | } 50 | 51 | enum ImageFormat { 52 | PNG = 0; 53 | SVG = 1; 54 | } 55 | 56 | message QrCodeResponse { 57 | /** Content of PNG or SVG file */ 58 | bytes file = 1; 59 | } 60 | -------------------------------------------------------------------------------- /web/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=80 2 | qr-code-generator.base-url=http://localhost 3 | qr-code-generator.qr-code-service-address=localhost:5000 4 | -------------------------------------------------------------------------------- /web/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | QR Code Generator 6 | 7 | 8 | 9 | 10 | 11 | 258 | 259 | 260 | 261 |
262 |
263 |

QR Code Generator

264 |
265 |
266 | 267 |
268 |
269 | 271 | 272 | 273 |
274 | 275 | 276 | 277 |
278 | Download 279 |
280 | 281 |
282 | 283 |
284 |
285 |
286 |
287 |
288 | 289 |
290 |
291 | 292 |
293 | 295 | 296 | 298 | 299 |
300 |
301 | 302 |
303 | 304 |
305 | 307 | 308 | 310 | 311 | 313 | 314 | 316 | 317 |
318 |
319 | 320 |
321 | 322 |
323 | 325 |
326 |
327 | 328 |
329 | 330 |
331 | 333 |
334 |
335 | 336 |
337 | 338 |
339 | 341 |
342 |
343 | 344 |
345 | 346 |
347 | 349 |
px
350 |
351 |
352 | 353 |
354 | 355 |
356 | 358 |
359 |
360 |
361 |
362 |
363 | 364 | 367 | 368 | 369 | 370 | -------------------------------------------------------------------------------- /web/src/test/kotlin/com/piotrjanczyk/qrcodegenerator/web/QrCodeGeneratorApplicationTests.kt: -------------------------------------------------------------------------------- 1 | package com.piotrjanczyk.qrcodegenerator.web 2 | 3 | import org.junit.jupiter.api.Test 4 | import org.springframework.boot.test.context.SpringBootTest 5 | 6 | @SpringBootTest 7 | class QrCodeGeneratorApplicationTests { 8 | 9 | @Test 10 | fun contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------