├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── Procfile ├── README.md ├── app.json ├── build.gradle ├── docs ├── IDEA_SETTINGS.md ├── architecture.jpg ├── gwt-project-structure-gwt.jpg ├── gwt-project-structure-web.jpg ├── gwt-run-config.jpg └── spring-run-config.jpg ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main ├── java │ └── com │ │ └── codecrafters │ │ ├── SpringBootGwt.gwt.xml │ │ ├── client │ │ ├── SpringBootGwt.java │ │ ├── TodoItem.java │ │ ├── TodoItemLabel.java │ │ ├── TodoItemLabel.ui.xml │ │ ├── TodoItemService.java │ │ ├── TodoPanel.java │ │ └── TodoPanel.ui.xml │ │ └── server │ │ ├── ScheduledDatabaseResetTask.java │ │ ├── SpringBootGwtApplication.java │ │ ├── SpringBootGwtProperties.java │ │ ├── TodoItem.java │ │ ├── TodoItemRepository.java │ │ └── TodoItemRestController.java └── resources │ ├── META-INF │ └── additional-spring-configuration-metadata.json │ ├── application-test.properties │ ├── application.properties │ └── static │ └── index.html └── test └── groovy └── com └── codecrafters └── server ├── ScheduledDatabaseResetTaskTest.groovy ├── TodoItemRepositoryIntegrationTest.groovy └── TodoItemRestControllerIntegrationTest.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | ### Eclipse template 2 | *.pydevproject 3 | .metadata 4 | .gradle 5 | bin/ 6 | tmp/ 7 | *.tmp 8 | *.bak 9 | *.swp 10 | *~.nib 11 | local.properties 12 | .settings/ 13 | .loadpath 14 | 15 | # Eclipse Core 16 | .project 17 | 18 | # External tool builders 19 | .externalToolBuilders/ 20 | 21 | # Locally stored "Eclipse launch configurations" 22 | *.launch 23 | 24 | # CDT-specific 25 | .cproject 26 | 27 | # JDT-specific (Eclipse Java Development Tools) 28 | .classpath 29 | 30 | # Java annotation processor (APT) 31 | .factorypath 32 | 33 | # PDT-specific 34 | .buildpath 35 | 36 | # sbteclipse plugin 37 | .target 38 | 39 | # TeXlipse plugin 40 | .texlipse 41 | 42 | ### Java template 43 | *.class 44 | 45 | # Mobile Tools for Java (J2ME) 46 | .mtj.tmp/ 47 | 48 | # Package Files # 49 | *.jar 50 | *.war 51 | *.ear 52 | 53 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 54 | hs_err_pid* 55 | 56 | ### GWT template 57 | # gwt caches and compiled units # 58 | war/gwt_bree/ 59 | gwt-unitCache/ 60 | 61 | # boilerplate generated classes # 62 | .apt_generated/ 63 | 64 | # more caches and things from deploy # 65 | war/WEB-INF/deploy/ 66 | war/WEB-INF/classes/ 67 | 68 | #compilation logs 69 | .gwt/ 70 | 71 | 72 | #gwt junit compilation files 73 | www-test/ 74 | 75 | #old GWT (1.5) created this dir 76 | .gwt-tmp/ 77 | 78 | ### Gradle template 79 | build/ 80 | 81 | # Ignore Gradle GUI config 82 | gradle-app.setting 83 | 84 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 85 | 86 | ### JetBrains template 87 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 88 | 89 | *.iml 90 | 91 | ## Directory-based project format: 92 | .idea/* 93 | # if you remove the above rule, at least ignore the following: 94 | 95 | # User-specific stuff: 96 | # .idea/workspace.xml 97 | # .idea/tasks.xml 98 | # .idea/dictionaries 99 | 100 | # Sensitive or high-churn files: 101 | # .idea/dataSources.ids 102 | # .idea/dataSources.xml 103 | # .idea/sqlDataSources.xml 104 | # .idea/dynamic.xml 105 | # .idea/uiDesigner.xml 106 | 107 | # Gradle: 108 | # .idea/gradle.xml 109 | # .idea/libraries 110 | 111 | # Mongo Explorer plugin: 112 | # .idea/mongoSettings.xml 113 | 114 | ## File-based project format: 115 | *.ipr 116 | *.iws 117 | 118 | ## Plugin-specific files: 119 | 120 | # IntelliJ 121 | /out/ 122 | 123 | # mpeltonen/sbt-idea plugin 124 | .idea_modules/ 125 | 126 | # JIRA plugin 127 | atlassian-ide-plugin.xml 128 | 129 | # Crashlytics plugin (for Android Studio and IntelliJ) 130 | com_crashlytics_export_strings.xml 131 | crashlytics.properties 132 | crashlytics-build.properties 133 | 134 | ### Custom 135 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 136 | !gradle-wrapper.jar 137 | 138 | # Add run configurations for intellij 139 | !.idea/runConfigurations/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team (via Github Issues). The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Fabian Dietenberger 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: java -Dserver.port=$PORT $JAVA_OPTS -Xms64m -Xmx384m -Xss512k -XX:+UseConcMarkSweepGC -jar build/libs/*.jar 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot GWT 2 | 3 | [![Build Status](https://img.shields.io/travis/feedm3/spring-boot-gwt.svg?style=flat-square)](https://travis-ci.org/feedm3/spring-boot-gwt) 4 | [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org) 5 | 6 | [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/feedm3/spring-boot-gwt/blob/master) 7 | 8 | This is a demo project to show Spring Boot in conjunction with GWT. It uses the latest dependencies 9 | (Spring Boot 2.0.1 and GWT 2.8.2) and Java 8. The deployed app can be found [here](https://spring-boot-gwt.herokuapp.com/). 10 | 11 | ## Run 12 | 13 | To run this project you have to start Spring Boot and GWT separate. If you use IntelliJ, see the 14 | [IDEA Settings readme](docs/IDEA_SETTINGS.md) for the correct configuration. 15 | 16 | Spring Boot can also be started with gradle. 17 | 18 | ``` 19 | gradlew bootRun 20 | ``` 21 | 22 | ## Test 23 | 24 | Currently only the server side code is tested. To run the tests use the following command 25 | 26 | ``` 27 | gradlew test 28 | ``` 29 | 30 | We use [Spock](https://github.com/spockframework/spock) as testing framework because of the great 31 | readability, syntax and built in features. 32 | 33 | #### Outdated dependencies 34 | 35 | To check for outdated dependencies 36 | ``` 37 | gradlew dependencyUpdates -Drevision=release 38 | ``` 39 | 40 | ## Build 41 | 42 | The project con be build to a single jar file with an embedded tomcat: 43 | 44 | ``` 45 | gradlew clean build 46 | ``` 47 | 48 | After gradle build the project the finished jar file is in `build/libs/spring-boot-gwt-1.0.0.jar` 49 | and can simply be started with 50 | 51 | ``` 52 | java -jar build/libs/spring-boot-gwt-1.0.0.jar 53 | ``` 54 | 55 | ### Heroku 56 | 57 | To deploy this app to heroku use the __Deploy to Heroku__ Button on the top. 58 | 59 | Heroku uses the gradle `stage` task to build the project. Because Spring Boot puts everything we 60 | need into the jar file we only have to tell heroku to execute this jar file. 61 | 62 | ## Technical Details 63 | 64 | ![Architecture](docs/architecture.jpg) 65 | 66 | The client side and server side are strictly separated. The GWT files are in the `client` package 67 | (except the `.gwt.xml`) and the server side code is in the `server` package. All static client code 68 | like the `index.html` and css files are inside the [`static`](src/main/resources/static) folder. Gradle 69 | will also put the compiled sources in this folder. 70 | 71 | The communication is made via JSON for which reason we have make 2 implementations of the object we 72 | send (POJO in the frontend and POJO with javax annotations in the backend). 73 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Spring Boot with GWT demo app", 3 | "description": "A barebone Spring Boot app using GWT as frontend", 4 | "repository": "https://github.com/feedm3/spring-boot-gwt", 5 | "keywords": [ 6 | "spring boot", 7 | "spring", 8 | "gwt" 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'org.wisepersist:gwt-gradle-plugin:1.0.6' 7 | } 8 | } 9 | 10 | plugins { 11 | id 'java' 12 | id 'eclipse' 13 | id 'groovy' 14 | id 'idea' 15 | id 'org.springframework.boot' version '2.0.1.RELEASE' 16 | id 'com.github.ben-manes.versions' version '0.17.0' 17 | } 18 | 19 | apply plugin: 'gwt' 20 | apply plugin: 'io.spring.dependency-management' 21 | 22 | sourceCompatibility = 1.8 23 | targetCompatibility = 1.8 24 | def GWT_VERSION = '2.8.2' 25 | ext['spock.version'] = '1.1' 26 | 27 | repositories { 28 | jcenter() 29 | } 30 | 31 | sourceSets { 32 | main.java.srcDir "src/main/java" 33 | main.resources.srcDir "src/main/resources" 34 | test.java.srcDir "src/test/java" 35 | test.groovy.srcDir "src/test/groovy" 36 | test.resources.srcDir "src/test/resources" 37 | } 38 | 39 | dependencies { 40 | // although the gwt plugin should already download the gwt dependencies, we have to add 41 | // them manually as some jetty dependencies are missing otherwise 42 | compileOnly("com.google.gwt:gwt-user:${GWT_VERSION}") 43 | compileOnly("com.google.gwt:gwt-dev:${GWT_VERSION}") 44 | compileOnly('org.fusesource.restygwt:restygwt:2.2.3') 45 | 46 | compile('javax.ws.rs:javax.ws.rs-api:2.1') 47 | compile('org.springframework.boot:spring-boot-starter-data-jpa') 48 | compile('org.springframework.boot:spring-boot-starter-jetty') 49 | compile('org.springframework.boot:spring-boot-starter-web') { 50 | exclude module: 'spring-boot-starter-tomcat' 51 | } 52 | 53 | runtime('com.h2database:h2') 54 | 55 | testCompile('org.springframework.boot:spring-boot-starter-test') 56 | testCompile('org.codehaus.groovy:groovy-all:2.4.15') 57 | testCompile('org.spockframework:spock-core:1.1-groovy-2.4') 58 | testCompile('org.spockframework:spock-spring:1.1-groovy-2.4') 59 | 60 | annotationProcessor('org.springframework.boot:spring-boot-configuration-processor') 61 | } 62 | 63 | task wrapper(type: Wrapper) { 64 | gradleVersion = '4.6' 65 | } 66 | 67 | gwt { 68 | gwtVersion = GWT_VERSION 69 | modules 'com.codecrafters.SpringBootGwt' 70 | maxHeapSize = "1024M" 71 | } 72 | 73 | compileJava.dependsOn(processResources) 74 | 75 | bootJar.dependsOn compileGwt 76 | bootJar { 77 | baseName = 'spring-boot-gwt' 78 | version = '1.0.0' 79 | 80 | into('BOOT-INF/classes/static') { 81 | from compileGwt.outputs 82 | } 83 | } 84 | 85 | eclipse { 86 | classpath { 87 | containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') 88 | containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /docs/IDEA_SETTINGS.md: -------------------------------------------------------------------------------- 1 | # IDEA Settings 2 | 3 | To run both the frontend and backend in IntelliJ, make sure the following settings are set: 4 | 5 | #### Project Structure... 6 | 7 | ![GWT project structure web](gwt-project-structure-web.jpg) 8 | ![GWT project structure gwt](gwt-project-structure-gwt.jpg) 9 | 10 | #### Run configs 11 | 12 | ![GWT run config](gwt-run-config.jpg) 13 | ![Spring run config](spring-run-config.jpg) -------------------------------------------------------------------------------- /docs/architecture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feedm3/spring-boot-gwt/449a953a5e2a9fde1799eddef2b14bd65334d6a4/docs/architecture.jpg -------------------------------------------------------------------------------- /docs/gwt-project-structure-gwt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feedm3/spring-boot-gwt/449a953a5e2a9fde1799eddef2b14bd65334d6a4/docs/gwt-project-structure-gwt.jpg -------------------------------------------------------------------------------- /docs/gwt-project-structure-web.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feedm3/spring-boot-gwt/449a953a5e2a9fde1799eddef2b14bd65334d6a4/docs/gwt-project-structure-web.jpg -------------------------------------------------------------------------------- /docs/gwt-run-config.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feedm3/spring-boot-gwt/449a953a5e2a9fde1799eddef2b14bd65334d6a4/docs/gwt-run-config.jpg -------------------------------------------------------------------------------- /docs/spring-run-config.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feedm3/spring-boot-gwt/449a953a5e2a9fde1799eddef2b14bd65334d6a4/docs/spring-run-config.jpg -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feedm3/spring-boot-gwt/449a953a5e2a9fde1799eddef2b14bd65334d6a4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/SpringBootGwt.gwt.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/SpringBootGwt.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.client; 2 | 3 | import com.google.gwt.core.client.EntryPoint; 4 | import com.google.gwt.core.client.GWT; 5 | import com.google.gwt.user.client.ui.RootPanel; 6 | import org.fusesource.restygwt.client.Defaults; 7 | 8 | /** 9 | * This class is used as entry point for our GWT application. It configures the app and adds the UI. 10 | * 11 | * @author Fabian Dietenberger 12 | */ 13 | public class SpringBootGwt implements EntryPoint { 14 | 15 | public void onModuleLoad() { 16 | useCorrectRequestBaseUrl(); 17 | RootPanel.get().add(new TodoPanel()); 18 | } 19 | 20 | private void useCorrectRequestBaseUrl() { 21 | if (isDevelopmentMode()) { 22 | // our spring boot server is running at port 80. If we don't change the url 23 | // resty gwt would use the gwt servlet port 24 | Defaults.setServiceRoot("http://localhost:8080"); 25 | } else { 26 | // in production our compiled javascript code gets copied into 27 | // a 'springbootgwt' folder into the static resources. So if we 28 | // dont change the default url resty gwt would put the folders name 29 | // to the base url 30 | Defaults.setServiceRoot(GWT.getHostPageBaseURL()); 31 | } 32 | } 33 | 34 | /** 35 | * Detect if the app is in development mode. 36 | * 37 | * @return true if in development mode 38 | */ 39 | private static native boolean isDevelopmentMode()/*-{ 40 | return typeof $wnd.__gwt_sdm !== 'undefined'; 41 | }-*/; 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/TodoItem.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.client; 2 | 3 | /** 4 | * This class is used to represent a todoItem. 5 | * 6 | * @author Fabian Dietenberger 7 | */ 8 | class TodoItem { 9 | 10 | private long id; 11 | private String text; 12 | 13 | public TodoItem() { 14 | } 15 | 16 | public TodoItem(final String text) { 17 | this.text = text; 18 | } 19 | 20 | public long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(final long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getText() { 29 | return text; 30 | } 31 | 32 | public void setText(final String text) { 33 | this.text = text; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "TodoItem{" + 39 | "id=" + id + 40 | ", text='" + text + '\'' + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/TodoItemLabel.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.client; 2 | 3 | import com.google.gwt.core.client.GWT; 4 | import com.google.gwt.event.dom.client.ClickEvent; 5 | import com.google.gwt.uibinder.client.UiBinder; 6 | import com.google.gwt.uibinder.client.UiField; 7 | import com.google.gwt.user.client.ui.Composite; 8 | import com.google.gwt.user.client.ui.Label; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | /** 14 | * This class is used to represent a {@link TodoItem} in a panel. 15 | * 16 | * @author Fabian Dietenberger 17 | */ 18 | class TodoItemLabel extends Composite { 19 | 20 | interface TodoItemLabelUiBinder extends UiBinder {} 21 | private static TodoItemLabelUiBinder ourUiBinder = GWT.create(TodoItemLabelUiBinder.class); 22 | 23 | public interface TodoItemLabelClickHandler { 24 | void onClick(final TodoItem todoItem); 25 | } 26 | 27 | private final List clickHandlers; 28 | 29 | @UiField 30 | Label label; 31 | 32 | public TodoItemLabel(final TodoItem todoItem) { 33 | initWidget(ourUiBinder.createAndBindUi(this)); 34 | label.setText(todoItem.getText()); 35 | clickHandlers = new ArrayList<>(); 36 | 37 | addDomHandler(event -> { 38 | for (final TodoItemLabelClickHandler clickHandler : clickHandlers) { 39 | clickHandler.onClick(todoItem); 40 | } 41 | }, ClickEvent.getType()); 42 | } 43 | 44 | public void addClickHandler(final TodoItemLabelClickHandler clickHandler) { 45 | clickHandlers.add(clickHandler); 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/TodoItemLabel.ui.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | .todo-item-label { 5 | width: 100%; 6 | } 7 | 8 | .todo-item-label:hover { 9 | text-decoration: line-through; 10 | color: #999999; 11 | cursor: pointer; 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/TodoItemService.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.client; 2 | 3 | import org.fusesource.restygwt.client.MethodCallback; 4 | import org.fusesource.restygwt.client.RestService; 5 | 6 | import javax.ws.rs.*; 7 | import java.util.List; 8 | 9 | /** 10 | * This class is used as rest service for the {@link TodoItem}. 11 | * 12 | * @author Fabian Dietenberger 13 | */ 14 | @Path("todos") 15 | interface TodoItemService extends RestService { 16 | 17 | /** 18 | * Get all todoItems from the server. 19 | * 20 | * @param text optional text to only get the todoItems which contain the given text 21 | * @param callback callback 22 | */ 23 | @GET 24 | @Path("?text={text}") 25 | void getTodos(@PathParam("text") final String text, MethodCallback> callback); 26 | 27 | /** 28 | * Add a todoItem to the server. 29 | * 30 | * @param todoItem the todoItem to add 31 | * @param callback callback 32 | */ 33 | @PUT 34 | void addTodo(final TodoItem todoItem, final MethodCallback callback); 35 | 36 | /** 37 | * Delete a todoItem from the server. 38 | * 39 | * @param todoItem the todoItem to delete 40 | * @param callback callback 41 | */ 42 | @DELETE 43 | void deleteTodo(final TodoItem todoItem, final MethodCallback callback); 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/TodoPanel.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.client; 2 | 3 | import com.google.gwt.core.client.GWT; 4 | import com.google.gwt.event.dom.client.KeyCodes; 5 | import com.google.gwt.uibinder.client.UiBinder; 6 | import com.google.gwt.uibinder.client.UiField; 7 | import com.google.gwt.user.client.ui.*; 8 | import org.fusesource.restygwt.client.Method; 9 | import org.fusesource.restygwt.client.MethodCallback; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * This class is used as root panel and contains the whole page. 15 | * 16 | * @author Fabian Dietenberger 17 | */ 18 | class TodoPanel extends Composite { 19 | 20 | interface TestViewUiBinder extends UiBinder {} 21 | private static TestViewUiBinder ourUiBinder = GWT.create(TestViewUiBinder.class); 22 | 23 | private static final TodoItemService todoItemService = GWT.create(TodoItemService.class); 24 | 25 | @UiField 26 | FlowPanel todoItemsList; 27 | 28 | @UiField 29 | TextBox todoItemTextBox; 30 | 31 | @UiField 32 | Button addTodoItemButton; 33 | 34 | public TodoPanel() { 35 | initWidget(ourUiBinder.createAndBindUi(this)); 36 | refreshTodoItems(); 37 | 38 | addTodoItemButton.addClickHandler(event -> { 39 | final String todoItemText = todoItemTextBox.getText(); 40 | if (!todoItemText.isEmpty()) { 41 | addTodoItem(todoItemText); 42 | } 43 | }); 44 | 45 | todoItemTextBox.getElement().setAttribute("placeholder", "Add a todo item"); 46 | todoItemTextBox.addKeyUpHandler(event -> { 47 | if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { 48 | final String todoItemText = todoItemTextBox.getText(); 49 | if (!todoItemText.isEmpty()) { 50 | addTodoItem(todoItemText); 51 | } 52 | } 53 | }); 54 | } 55 | 56 | /** 57 | * Clear the todoItemsPanel and add all todoItems from the server. 58 | */ 59 | private void refreshTodoItems() { 60 | todoItemService.getTodos("", new MethodCallback>() { 61 | @Override 62 | public void onFailure(final Method method, final Throwable exception) { 63 | 64 | } 65 | 66 | @Override 67 | public void onSuccess(final Method method, final List response) { 68 | todoItemsList.clear(); 69 | for (final TodoItem todoItem : response) { 70 | final TodoItemLabel todoItemLabel = new TodoItemLabel(todoItem); 71 | todoItemLabel.addClickHandler(todoItemToRemove -> removeTodoItem(todoItemToRemove)); 72 | todoItemsList.add(todoItemLabel); 73 | } 74 | } 75 | }); 76 | } 77 | 78 | /** 79 | * Send a new todoItem to the server. On success refresh the todoItemsPanel. 80 | * 81 | * @param text the text of the todoItem 82 | */ 83 | private void addTodoItem(final String text) { 84 | todoItemService.addTodo(new TodoItem(text), new MethodCallback() { 85 | @Override 86 | public void onFailure(final Method method, final Throwable exception) { 87 | 88 | } 89 | 90 | @Override 91 | public void onSuccess(final Method method, final Void response) { 92 | todoItemTextBox.setText(""); 93 | refreshTodoItems(); 94 | } 95 | }); 96 | } 97 | 98 | /** 99 | * Remove a todoItem from the server. On success refresh the todoItemsPanel. 100 | * 101 | * @param todoItem the todoItem to delete 102 | */ 103 | public void removeTodoItem(final TodoItem todoItem) { 104 | todoItemService.deleteTodo(todoItem, new MethodCallback() { 105 | @Override 106 | public void onFailure(final Method method, final Throwable exception) { 107 | 108 | } 109 | 110 | @Override 111 | public void onSuccess(final Method method, final Void response) { 112 | refreshTodoItems(); 113 | } 114 | }); 115 | } 116 | } -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/client/TodoPanel.ui.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | body, html { 5 | font-family: 'Roboto', sans-serif; 6 | font-size: 16px; 7 | line-height: 24px; 8 | width: 100%; 9 | height: 100%; 10 | margin: 0; 11 | background-color: #E9EAED; 12 | } 13 | 14 | ul { 15 | padding: 0; 16 | list-style-type: none; 17 | } 18 | 19 | .center { 20 | display: flex; 21 | align-items: center; 22 | justify-content: center; 23 | flex-direction: column; 24 | height: 100%; 25 | width: 100%; 26 | } 27 | 28 | .todo-panel { 29 | background-color: #FFFFFF; 30 | width: 480px; 31 | padding: 20px; 32 | box-shadow: 0 0 10px 1px #C9C9C9; 33 | border-radius: .25rem 34 | } 35 | 36 | .header { 37 | text-align: center; 38 | margin: 0; 39 | padding-bottom: 20px; 40 | } 41 | 42 | .input-group { 43 | display: flex; 44 | width: 100%; 45 | padding-top: 20px; 46 | flex-direction: row; 47 | } 48 | 49 | .input-group-text { 50 | width: 100%; 51 | border: 1px solid #CCC; 52 | padding: .375rem .75rem; 53 | border-radius: .25rem 0 0 .25rem; 54 | border-right: 0; 55 | } 56 | 57 | .input-group-button { 58 | padding: 0.375rem 0.75rem; 59 | color: #FFF; 60 | background-color: #0275d8; 61 | border: 1px solid #0275d8; 62 | border-radius: 0 .25rem .25rem 0; 63 | cursor: pointer; 64 | } 65 | 66 | .input-group-button:hover { 67 | background-color: #025aa5; 68 | border-color: #01549b; 69 | } 70 | 71 | .input-group-button:active { 72 | background-color: #014682; 73 | border-color: #01315a; 74 | } 75 | 76 | .footer { 77 | color: gray; 78 | text-align: center; 79 | cursor: pointer; 80 | margin-top: 10px; 81 | text-decoration: none; 82 | font-size: 12px; 83 | } 84 | 85 | 86 | 87 |
88 |

Todos

89 | 90 | 91 | 92 | 93 |
94 | 95 | Add 96 |
97 |
98 | Data refresh every 15 99 | minutes. © 2015 Fabian Dietenberger 100 | 101 |
102 |
-------------------------------------------------------------------------------- /src/main/java/com/codecrafters/server/ScheduledDatabaseResetTask.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.scheduling.annotation.Scheduled; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.Collections; 10 | import java.util.List; 11 | import java.util.Optional; 12 | 13 | /** 14 | * This class is used as scheduled task which resets the database every 15 minutes to it's initial state. 15 | * 16 | * @author Fabian Dietenberger 17 | */ 18 | @Component 19 | class ScheduledDatabaseResetTask { 20 | 21 | private final Logger logger = LoggerFactory.getLogger(ScheduledDatabaseResetTask.class); 22 | private final TodoItemRepository repository; 23 | private final SpringBootGwtProperties springBootGwtProperties; 24 | 25 | @Autowired 26 | public ScheduledDatabaseResetTask(final TodoItemRepository repository, final SpringBootGwtProperties springBootGwtProperties) { 27 | this.repository = repository; 28 | this.springBootGwtProperties = springBootGwtProperties; 29 | } 30 | 31 | @Scheduled(fixedRateString = "${spring-boot-gwt.scheduled-database-reset-interval-millis}") 32 | public void resetDatabase() { 33 | if (springBootGwtProperties.isScheduledDatabaseReset()) { 34 | logger.info("Reset database"); 35 | 36 | repository.deleteAll(); 37 | 38 | for (final String initialTodoItem : springBootGwtProperties.getInitialTodoItems()) { 39 | repository.save(new TodoItem(initialTodoItem)); 40 | } 41 | 42 | final List itemsInDatabase = Optional.ofNullable(repository.findAll()).orElse(Collections.emptyList()); 43 | logger.info("Saved " + itemsInDatabase.size() + " todo items to the database: " + itemsInDatabase.toString()); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/server/SpringBootGwtApplication.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.scheduling.annotation.EnableScheduling; 7 | import org.springframework.web.cors.CorsConfiguration; 8 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource; 9 | import org.springframework.web.filter.CorsFilter; 10 | 11 | @SpringBootApplication 12 | @EnableScheduling 13 | public class SpringBootGwtApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(SpringBootGwtApplication.class, args); 17 | } 18 | 19 | @Bean 20 | public CorsFilter corsFilter() { 21 | final CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues(); 22 | config.addAllowedMethod("*"); 23 | 24 | final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 25 | source.registerCorsConfiguration("/**", config); 26 | return new CorsFilter(source); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/server/SpringBootGwtProperties.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * This class is used to autowire the application properties into a bean. 10 | * 11 | * @author Fabian Dietenberger 12 | */ 13 | @Configuration 14 | @ConfigurationProperties(prefix = "spring-boot-gwt") 15 | public class SpringBootGwtProperties { 16 | 17 | private List initialTodoItems; 18 | private boolean scheduledDatabaseReset; 19 | 20 | public List getInitialTodoItems() { 21 | return initialTodoItems; 22 | } 23 | 24 | public void setInitialTodoItems(final List initialTodoItems) { 25 | this.initialTodoItems = initialTodoItems; 26 | } 27 | 28 | public boolean isScheduledDatabaseReset() { 29 | return scheduledDatabaseReset; 30 | } 31 | 32 | public void setScheduledDatabaseReset(final boolean scheduledDatabaseReset) { 33 | this.scheduledDatabaseReset = scheduledDatabaseReset; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/server/TodoItem.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | 7 | /** 8 | * This class is used as entity for a todoItem. 9 | * 10 | * @author Fabian Dietenberger 11 | */ 12 | @Entity 13 | class TodoItem { 14 | 15 | @Id 16 | @GeneratedValue 17 | private Long id; 18 | 19 | private String text; 20 | 21 | public TodoItem() { 22 | } 23 | 24 | public TodoItem(final String text) { 25 | this.text = text; 26 | } 27 | 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | public void setId(final Long id) { 33 | this.id = id; 34 | } 35 | 36 | public String getText() { 37 | return text; 38 | } 39 | 40 | public void setText(final String text) { 41 | this.text = text; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return "TodoItem{" + 47 | "id=" + id + 48 | ", text='" + text + '\'' + 49 | '}'; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/server/TodoItemRepository.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * This class is used as repository for the {@link TodoItem}. It gives us some "ready-to-use" CRUD operations against the 10 | * database. 11 | * 12 | * @author Fabian Dietenberger 13 | */ 14 | @Repository 15 | interface TodoItemRepository extends JpaRepository { 16 | 17 | List findByTextContainingIgnoreCase(final String text); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/codecrafters/server/TodoItemRestController.java: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.CacheControl; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | import java.util.Optional; 13 | 14 | /** 15 | * This class is used as rest controller for the {@link TodoItem}. 16 | * 17 | * @author Fabian Dietenberger 18 | */ 19 | @RestController() 20 | @RequestMapping("todos") 21 | class TodoItemRestController { 22 | 23 | private final TodoItemRepository repository; 24 | private final Logger logger = LoggerFactory.getLogger(TodoItemRestController.class); 25 | 26 | @Autowired 27 | public TodoItemRestController(final TodoItemRepository repository) { 28 | this.repository = repository; 29 | } 30 | 31 | @RequestMapping(method = RequestMethod.GET) 32 | public ResponseEntity> getTodoItems(@RequestParam(value = "text", required = false) String containingText) { 33 | final List items = repository.findByTextContainingIgnoreCase(Optional.ofNullable(containingText).orElse("")); 34 | return ResponseEntity.ok() 35 | .cacheControl(CacheControl.noCache()) // if we don't return this the browser could (edge does) cache the request 36 | .body(items); 37 | } 38 | 39 | @RequestMapping(method = RequestMethod.PUT) 40 | public ResponseEntity addTodoItem(@RequestBody final TodoItem item) { 41 | repository.save(item); 42 | logger.info("Item saved: " + item.toString()); 43 | return new ResponseEntity<>(HttpStatus.OK); 44 | } 45 | 46 | @RequestMapping(method = RequestMethod.DELETE) 47 | public ResponseEntity removeTodoItem(@RequestBody final TodoItem item) { 48 | repository.delete(item); 49 | logger.info("Item deleted: " + item.toString()); 50 | return new ResponseEntity<>(HttpStatus.OK); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/additional-spring-configuration-metadata.json: -------------------------------------------------------------------------------- 1 | { 2 | "properties": [ 3 | { 4 | "name": "spring-boot-gwt.scheduled-database-reset", 5 | "type": "java.lang.Boolean", 6 | "description": "Reset the database with in a given interval with given initial values." 7 | }, 8 | { 9 | "name": "spring-boot-gwt.scheduled-database-reset-interval-millis", 10 | "type": "java.lang.Long", 11 | "description": "The interval in milliseconds to reset the database." 12 | }, 13 | { 14 | "name": "spring-boot-gwt.initial-todo-items", 15 | "type": "java.util.List", 16 | "description": "The initial values to insert after the scheduled database reset." 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /src/main/resources/application-test.properties: -------------------------------------------------------------------------------- 1 | spring-boot-gwt.scheduled-database-reset=false -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.=error 2 | logging.level.com.codecrafters=info 3 | 4 | spring-boot-gwt.scheduled-database-reset=true 5 | spring-boot-gwt.scheduled-database-reset-interval-millis=1500000 6 | spring-boot-gwt.initial-todo-items[0]=Learn Spring Boot 7 | spring-boot-gwt.initial-todo-items[1]=Learn GWT 8 | spring-boot-gwt.initial-todo-items[2]=Learn Java 8 9 | -------------------------------------------------------------------------------- /src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Spring Boot GWT 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/groovy/com/codecrafters/server/ScheduledDatabaseResetTaskTest.groovy: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server 2 | 3 | import spock.lang.Specification 4 | /** 5 | * This class is used as test for the ScheduledDatabaseResetTask. 6 | * 7 | * @author Fabian Dietenberger 8 | */ 9 | class ScheduledDatabaseResetTaskTest extends Specification { 10 | 11 | 12 | def "reset the database when scheduling is enabled"() { 13 | given: 14 | def repository = Mock(TodoItemRepository) 15 | def properties = new SpringBootGwtProperties() 16 | properties.scheduledDatabaseReset = true 17 | properties.initialTodoItems = ["Item 1", "Item 2", "Item 3"] 18 | 19 | def databaseResetTask = new ScheduledDatabaseResetTask(repository, properties) 20 | 21 | when: 22 | databaseResetTask.resetDatabase() 23 | 24 | then: 25 | 1 * repository.deleteAll() 26 | 3 * repository.save(_) 27 | 1 * repository.findAll() 28 | } 29 | 30 | def "do nothing when scheduling is not enabled"() { 31 | given: 32 | def repository = Mock(TodoItemRepository) 33 | def properties = new SpringBootGwtProperties() 34 | properties.scheduledDatabaseReset = false 35 | 36 | def databaseResetTask = new ScheduledDatabaseResetTask(repository, properties) 37 | 38 | when: 39 | databaseResetTask.resetDatabase() 40 | 41 | then: 42 | 0 * repository.deleteAll() 43 | 0 * repository.save(_) 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/groovy/com/codecrafters/server/TodoItemRepositoryIntegrationTest.groovy: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server 2 | 3 | import org.springframework.beans.factory.annotation.Autowired 4 | import org.springframework.boot.test.context.SpringBootTest 5 | import org.springframework.test.context.ActiveProfiles 6 | import spock.lang.Specification 7 | 8 | 9 | /** 10 | * This class is used as integration test for the TodoItemRepository. 11 | * 12 | * @author Fabian Dietenberger 13 | */ 14 | @SpringBootTest 15 | @ActiveProfiles("test") 16 | class TodoItemRepositoryIntegrationTest extends Specification { 17 | 18 | @Autowired 19 | TodoItemRepository repository 20 | 21 | def "Save a todo item"() { 22 | given: "an inital number of items in the database" 23 | def numberOfTodoItems = repository.findAll().size() 24 | 25 | when: "when we save a new item" 26 | repository.save(new TodoItem("Test")) 27 | 28 | then: "we have one more item in the database" 29 | repository.findAll().size() == numberOfTodoItems + 1 30 | } 31 | 32 | def "Find a todo item by containing text"() { 33 | given: "a new item in the database" 34 | repository.save(new TodoItem("I should start learning Java 8")) 35 | 36 | when: "we search for the specific text of the item" 37 | List todoItems = repository.findByTextContainingIgnoreCase("learning") 38 | 39 | then: "we find it" 40 | todoItems != null 41 | todoItems.size() > 0 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/groovy/com/codecrafters/server/TodoItemRestControllerIntegrationTest.groovy: -------------------------------------------------------------------------------- 1 | package com.codecrafters.server 2 | 3 | import com.fasterxml.jackson.core.type.TypeReference 4 | import com.fasterxml.jackson.databind.ObjectMapper 5 | import org.springframework.beans.factory.annotation.Autowired 6 | import org.springframework.boot.test.context.SpringBootTest 7 | import org.springframework.boot.test.web.client.TestRestTemplate 8 | import org.springframework.boot.web.server.LocalServerPort 9 | import org.springframework.http.HttpEntity 10 | import org.springframework.http.HttpMethod 11 | import org.springframework.http.HttpStatus 12 | import org.springframework.test.context.ActiveProfiles 13 | import spock.lang.Shared 14 | import spock.lang.Specification 15 | import spock.lang.Stepwise 16 | 17 | import javax.ws.rs.core.HttpHeaders 18 | /** 19 | * This class is used as integration test for the /todos url (TodoItemRestController). 20 | * 21 | * @author Fabian Dietenberger 22 | */ 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | @Stepwise 25 | @ActiveProfiles("test") 26 | class TodoItemRestControllerIntegrationTest extends Specification { 27 | 28 | @Shared 29 | TestRestTemplate restTemplate = new TestRestTemplate() 30 | 31 | @Autowired 32 | ObjectMapper mapper 33 | 34 | @LocalServerPort 35 | private int port 36 | 37 | def "PUT /todos"() { 38 | when: "we post a new item" 39 | def request = new HttpEntity<>(new TodoItem("Sample text 12354321423")) 40 | def response = restTemplate.exchange(getTodosUrl(), HttpMethod.PUT, request, String.class) 41 | 42 | then: "we get the corresponding http status" 43 | response.getStatusCode() == HttpStatus.OK 44 | } 45 | 46 | def "GET /todos"() { 47 | when: "we get the items" 48 | def response = restTemplate.getForEntity(getTodosUrl(), String.class) 49 | 50 | then: "we get the http status OK" 51 | response.getStatusCode() == HttpStatus.OK 52 | 53 | and: "the response should not be cached" 54 | response.getHeaders().get(HttpHeaders.CACHE_CONTROL).get(0) == "no-cache" 55 | } 56 | 57 | def "GET /todos?text=123543"() { 58 | when: "we search for an item by the text" 59 | def responseExact = restTemplate.getForEntity(getTodosUrl() + "?text=123543", String.class) 60 | List items = mapper.readValue(responseExact.getBody(), new TypeReference>() {}) 61 | 62 | then: "we get the item" 63 | items.size() == 1 64 | items.get(0).getText() == "Sample text 12354321423" 65 | } 66 | 67 | def "DELETE /todos"() { 68 | given: "the item we saved in the requests before" 69 | def responseBeforeDelete = restTemplate.getForEntity(getTodosUrl() + "?text=123543", String.class) 70 | List itemsBeforeDelete = mapper.readValue(responseBeforeDelete.getBody(), new TypeReference>() {}) 71 | 72 | when: "we delete the item and request them afterwards again" 73 | HttpEntity request = new HttpEntity<>(itemsBeforeDelete.get(0)) 74 | restTemplate.exchange(getTodosUrl(), HttpMethod.DELETE, request, String.class) 75 | 76 | def responseAfterDelete = restTemplate.getForEntity(getTodosUrl() + "?text=123543", String.class) 77 | List itemsAfterDelete = mapper.readValue(responseAfterDelete.getBody(), new TypeReference>() {}) 78 | 79 | then: "the item does not exist" 80 | itemsAfterDelete.size() == 0 81 | } 82 | 83 | String getTodosUrl() { 84 | return "http://localhost:" + port + "/todos" 85 | } 86 | } 87 | --------------------------------------------------------------------------------