├── .gitignore
├── README.md
├── build.gradle
├── gradle.properties
├── gradle
├── config
│ ├── checkstyle
│ │ └── sun_checks.xml
│ ├── migration
│ │ └── V2019.02.15.07.43__Create_user_table.sql
│ └── scripts
│ │ ├── coverage.gradle
│ │ ├── idea.gradle
│ │ └── style.gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lombok.config
├── settings.gradle
├── zero-bootstrap
├── build.gradle
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── github
│ │ └── dreamhead
│ │ └── zero
│ │ └── bootstrap
│ │ └── Bootstrap.java
│ └── resources
│ └── application.properties
└── zero-identity
└── src
├── main
├── java
│ └── com
│ │ └── github
│ │ └── dreamhead
│ │ └── zero
│ │ └── identity
│ │ ├── domain
│ │ └── User.java
│ │ ├── repository
│ │ └── UserRepository.java
│ │ ├── resource
│ │ ├── UserInfo.java
│ │ ├── UserRegistration.java
│ │ └── UserResource.java
│ │ └── service
│ │ └── UserService.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── github
└── dreamhead
└── zero
└── identity
├── resource
└── UserResourceTest.java
└── service
└── UserServiceTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.ipr
2 | *.iml
3 | *.iws
4 | out
5 | build
6 | .gradle
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Geektime Zero
2 |
3 | ## 简介
4 |
5 | 这是《极客时间》专栏《10x 程序员工作法》的《30 | 一个好的项目自动化应该是什么样子的?》中的示例项目,目的是演示基本的项目自动化。
6 |
7 | ## 基本用法
8 |
9 | * 生成 IDEA 工程
10 |
11 | ```shell
12 | ./gradlew idea
13 | ```
14 |
15 | * 检查
16 |
17 | ```shell
18 | ./gradlew check
19 | ```
20 |
21 | * 数据库迁移
22 |
23 | ```shell
24 | ./gradlew flywayMigrate
25 | ```
26 |
27 | * 生成构建产物
28 |
29 | ```shell
30 | ./gradlew build
31 | ```
32 |
33 | * 运行应用
34 |
35 | ```shell
36 | ./gradlew bootRun
37 | ```
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | dependencies {
3 | classpath "mysql:mysql-connector-java:$mysqlVersion"
4 | }
5 | }
6 |
7 | plugins {
8 | id "org.flywaydb.flyway" version "${flywayVersion}"
9 | }
10 |
11 | subprojects {
12 | apply plugin: 'java'
13 |
14 | group = 'com.github.dreamhead.zero'
15 | version = new Date().format('yyyyMMdd') + '-SNAPSHOT'
16 | sourceCompatibility = JavaVersion.VERSION_1_8
17 | targetCompatibility = JavaVersion.VERSION_1_8
18 |
19 | tasks.withType(JavaCompile) {
20 | options.encoding = 'UTF-8'
21 | options.compilerArgs << "-Xlint:unchecked"
22 | options.compilerArgs << "-Xlint:deprecation"
23 | }
24 |
25 | repositories {
26 | mavenCentral()
27 | maven { url "https://repo.maven.apache.org/maven2" }
28 | }
29 |
30 | dependencies {
31 | implementation("com.google.guava:guava:$guavaVersion")
32 | implementation("org.springframework.boot:spring-boot-starter:$springBootVersion")
33 | implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
34 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
35 |
36 | compileOnly("org.projectlombok:lombok:$lombokVersion")
37 | annotationProcessor("org.projectlombok:lombok:$lombokVersion")
38 |
39 | runtimeOnly("mysql:mysql-connector-java:$mysqlVersion")
40 |
41 | testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
42 | testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
43 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
44 | testImplementation("org.assertj:assertj-core:$assertJVersion")
45 | testImplementation("org.mockito:mockito-core:$mockitoVersion")
46 | }
47 |
48 | test {
49 | useJUnitPlatform()
50 | }
51 |
52 | apply from: "$rootDir/gradle/config/scripts/style.gradle"
53 | apply from: "$rootDir/gradle/config/scripts/coverage.gradle"
54 |
55 | coverage.excludePackages = [
56 | ]
57 |
58 | coverage.excludeClasses = [
59 | ]
60 |
61 | style.excludePackages = [
62 | ]
63 |
64 | style.excludeClasses = [
65 | ]
66 | }
67 |
68 | configure(subprojects - project(':zero-bootstrap')) {
69 | project(':zero-bootstrap').dependencies {
70 | implementation(project)
71 | }
72 | }
73 |
74 | allprojects {
75 | apply plugin: 'idea'
76 | }
77 |
78 | apply from: "$rootDir/gradle/config/scripts/idea.gradle"
79 |
80 | flyway {
81 | url = 'jdbc:mysql://localhost:3306/zero_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true'
82 | user = 'zero'
83 | password = 'geektime'
84 | locations = ["filesystem:$rootDir/gradle/config/migration"]
85 | }
86 |
87 | wrapper {
88 | gradleVersion = '7.6'
89 | }
90 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | guavaVersion=31.1-jre
2 | springBootVersion=2.7.6
3 | lombokVersion=1.18.24
4 | mysqlVersion=8.0.31
5 | junitVersion=5.9.1
6 | assertJVersion=3.23.1
7 | mockitoVersion=4.9.0
8 | flywayVersion=8.5.13
9 | jacocoVersion=0.8.8
10 | checkstyleVersion=9.3
--------------------------------------------------------------------------------
/gradle/config/checkstyle/sun_checks.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
33 |
34 |
35 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
--------------------------------------------------------------------------------
/gradle/config/migration/V2019.02.15.07.43__Create_user_table.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE zero_users(
2 | id bigint(20) not null AUTO_INCREMENT,
3 | name varchar(100) not null unique,
4 | password varchar(100) not null,
5 | primary key(id)
6 | );
--------------------------------------------------------------------------------
/gradle/config/scripts/coverage.gradle:
--------------------------------------------------------------------------------
1 | class CoverageExtension {
2 | boolean enabled = true
3 | List excludePackages;
4 | List excludeClasses;
5 | }
6 |
7 | project.extensions.create('coverage', CoverageExtension)
8 |
9 | apply plugin: 'jacoco'
10 |
11 | jacoco.toolVersion = jacocoVersion
12 |
13 | task coverageCheck(dependsOn: test) {
14 | doLast {
15 | if (!coverage.enabled) {
16 | return
17 | }
18 |
19 | if (!file("$buildDir/classes/java/test").exists()) {
20 | return
21 | }
22 |
23 | ant.taskdef(name: 'jacocoReport',
24 | classname: 'org.jacoco.ant.ReportTask',
25 | classpath: configurations.jacocoAnt.asPath)
26 |
27 | ant.jacocoReport {
28 | executiondata {
29 | fileset(dir: "$buildDir/jacoco") {
30 | include name: 'test.exec'
31 | }
32 | }
33 |
34 | structure(name: project.name) {
35 | classfiles {
36 | fileset(dir: "$buildDir/classes/java/main") {
37 | coverage.excludePackages.each() {
38 | exclude name: "${it.replaceAll('\\.', '/') + '/*'}"
39 | }
40 | coverage.excludeClasses.each() {
41 | exclude name: "${it.replaceAll('\\.', '/') + '.class'}"
42 | }
43 | }
44 | }
45 | sourcefiles {
46 | fileset dir: "src/main/java"
47 | }
48 | }
49 |
50 | html(destdir: "$buildDir/reports/jacoco")
51 |
52 | check(failOnViolation: true) {
53 | rule(element: 'PACKAGE') {
54 | limit(counter: "LINE", value: "COVEREDRATIO", minimum: "1.0")
55 | limit(counter: "CLASS", value: "COVEREDRATIO", minimum: "1.0")
56 |
57 | }
58 | }
59 | }
60 | }
61 | }
62 |
63 | check.dependsOn "coverageCheck"
--------------------------------------------------------------------------------
/gradle/config/scripts/idea.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'idea'
2 |
3 | idea {
4 | project {
5 | jdkName = JavaVersion.VERSION_11
6 | languageLevel = JavaVersion.VERSION_11
7 |
8 | vcs = "Git"
9 | }
10 |
11 | workspace.iws.withXml { provider ->
12 | def junitDefaults = provider.node.component.find { it.@name == 'RunManager' }.configuration.find {
13 | it.@type == 'JUnit'
14 | }
15 | junitDefaults.option.find { it.@name == 'WORKING_DIRECTORY' }.@value = '$MODULE_DIR$'
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/gradle/config/scripts/style.gradle:
--------------------------------------------------------------------------------
1 | class StyleExtension {
2 | List excludePackages;
3 | List excludeClasses;
4 | }
5 |
6 | project.extensions.create('style', StyleExtension)
7 |
8 | apply plugin: 'checkstyle'
9 |
10 | checkstyle {
11 | configFile = file("$rootDir/gradle/config/checkstyle/sun_checks.xml")
12 | toolVersion = checkstyleVersion
13 | }
14 |
15 | checkstyleTest.enabled = false
16 |
17 | checkstyleMain.doFirst {
18 | style.excludePackages.each() {
19 | checkstyleMain.exclude "${'**/' + it.replaceAll('\\.', '/') + '/*'}"
20 | }
21 |
22 | style.excludeClasses.each() {
23 | checkstyleMain.exclude "${'**/' + it.replaceAll('\\.', '/') + '.java'}"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dreamhead/geektime-zero/23458c80507f586a0231d07f2fb6f9f6ffa303fd/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-7.6-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | #
4 | # Copyright © 2015-2021 the original 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 POSIX generated by Gradle.
22 | #
23 | # Important for running:
24 | #
25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26 | # noncompliant, but you have some other compliant shell such as ksh or
27 | # bash, then to run this script, type that shell name before the whole
28 | # command line, like:
29 | #
30 | # ksh Gradle
31 | #
32 | # Busybox and similar reduced shells will NOT work, because this script
33 | # requires all of these POSIX shell features:
34 | # * functions;
35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37 | # * compound commands having a testable exit status, especially «case»;
38 | # * various built-in commands including «command», «set», and «ulimit».
39 | #
40 | # Important for patching:
41 | #
42 | # (2) This script targets any POSIX shell, so it avoids extensions provided
43 | # by Bash, Ksh, etc; in particular arrays are avoided.
44 | #
45 | # The "traditional" practice of packing multiple parameters into a
46 | # space-separated string is a well documented source of bugs and security
47 | # problems, so this is (mostly) avoided, by progressively accumulating
48 | # options in "$@", and eventually passing that to Java.
49 | #
50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52 | # see the in-line comments for details.
53 | #
54 | # There are tweaks for specific operating systems such as AIX, CygWin,
55 | # Darwin, MinGW, and NonStop.
56 | #
57 | # (3) This script is generated from the Groovy template
58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59 | # within the Gradle project.
60 | #
61 | # You can find Gradle at https://github.com/gradle/gradle/.
62 | #
63 | ##############################################################################
64 |
65 | # Attempt to set APP_HOME
66 |
67 | # Resolve links: $0 may be a link
68 | app_path=$0
69 |
70 | # Need this for daisy-chained symlinks.
71 | while
72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
73 | [ -h "$app_path" ]
74 | do
75 | ls=$( ls -ld "$app_path" )
76 | link=${ls#*' -> '}
77 | case $link in #(
78 | /*) app_path=$link ;; #(
79 | *) app_path=$APP_HOME$link ;;
80 | esac
81 | done
82 |
83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84 |
85 | APP_NAME="Gradle"
86 | APP_BASE_NAME=${0##*/}
87 |
88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
90 |
91 | # Use the maximum available, or set MAX_FD != -1 to use that value.
92 | MAX_FD=maximum
93 |
94 | warn () {
95 | echo "$*"
96 | } >&2
97 |
98 | die () {
99 | echo
100 | echo "$*"
101 | echo
102 | exit 1
103 | } >&2
104 |
105 | # OS specific support (must be 'true' or 'false').
106 | cygwin=false
107 | msys=false
108 | darwin=false
109 | nonstop=false
110 | case "$( uname )" in #(
111 | CYGWIN* ) cygwin=true ;; #(
112 | Darwin* ) darwin=true ;; #(
113 | MSYS* | MINGW* ) msys=true ;; #(
114 | NONSTOP* ) nonstop=true ;;
115 | esac
116 |
117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
118 |
119 |
120 | # Determine the Java command to use to start the JVM.
121 | if [ -n "$JAVA_HOME" ] ; then
122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
123 | # IBM's JDK on AIX uses strange locations for the executables
124 | JAVACMD=$JAVA_HOME/jre/sh/java
125 | else
126 | JAVACMD=$JAVA_HOME/bin/java
127 | fi
128 | if [ ! -x "$JAVACMD" ] ; then
129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
130 |
131 | Please set the JAVA_HOME variable in your environment to match the
132 | location of your Java installation."
133 | fi
134 | else
135 | JAVACMD=java
136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137 |
138 | Please set the JAVA_HOME variable in your environment to match the
139 | location of your Java installation."
140 | fi
141 |
142 | # Increase the maximum file descriptors if we can.
143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144 | case $MAX_FD in #(
145 | max*)
146 | MAX_FD=$( ulimit -H -n ) ||
147 | warn "Could not query maximum file descriptor limit"
148 | esac
149 | case $MAX_FD in #(
150 | '' | soft) :;; #(
151 | *)
152 | ulimit -n "$MAX_FD" ||
153 | warn "Could not set maximum file descriptor limit to $MAX_FD"
154 | esac
155 | fi
156 |
157 | # Collect all arguments for the java command, stacking in reverse order:
158 | # * args from the command line
159 | # * the main class name
160 | # * -classpath
161 | # * -D...appname settings
162 | # * --module-path (only if needed)
163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
164 |
165 | # For Cygwin or MSYS, switch paths to Windows format before running java
166 | if "$cygwin" || "$msys" ; then
167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
169 |
170 | JAVACMD=$( cygpath --unix "$JAVACMD" )
171 |
172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
173 | for arg do
174 | if
175 | case $arg in #(
176 | -*) false ;; # don't mess with options #(
177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
178 | [ -e "$t" ] ;; #(
179 | *) false ;;
180 | esac
181 | then
182 | arg=$( cygpath --path --ignore --mixed "$arg" )
183 | fi
184 | # Roll the args list around exactly as many times as the number of
185 | # args, so each arg winds up back in the position where it started, but
186 | # possibly modified.
187 | #
188 | # NB: a `for` loop captures its iteration list before it begins, so
189 | # changing the positional parameters here affects neither the number of
190 | # iterations, nor the values presented in `arg`.
191 | shift # remove old arg
192 | set -- "$@" "$arg" # push replacement arg
193 | done
194 | fi
195 |
196 | # Collect all arguments for the java command;
197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198 | # shell script including quotes and variable substitutions, so put them in
199 | # double quotes to make sure that they get re-expanded; and
200 | # * put everything else in single quotes, so that it's not re-expanded.
201 |
202 | set -- \
203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \
204 | -classpath "$CLASSPATH" \
205 | org.gradle.wrapper.GradleWrapperMain \
206 | "$@"
207 |
208 | # Use "xargs" to parse quoted args.
209 | #
210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
211 | #
212 | # In Bash we could simply go:
213 | #
214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
215 | # set -- "${ARGS[@]}" "$@"
216 | #
217 | # but POSIX shell has neither arrays nor command substitution, so instead we
218 | # post-process each arg (as a line of input to sed) to backslash-escape any
219 | # character that might be a shell metacharacter, then use eval to reverse
220 | # that process (while maintaining the separation between arguments), and wrap
221 | # the whole thing up as a single "set" statement.
222 | #
223 | # This will of course break if any of these variables contains a newline or
224 | # an unmatched quote.
225 | #
226 |
227 | eval "set -- $(
228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
229 | xargs -n1 |
230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
231 | tr '\n' ' '
232 | )" '"$@"'
233 |
234 | exec "$JAVACMD" "$@"
235 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/lombok.config:
--------------------------------------------------------------------------------
1 | lombok.addLombokGeneratedAnnotation = true
2 | lombok.setter.flagUsage = error
3 | lombok.data.flagUsage = error
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'geektime-zero'
2 |
3 | include 'zero-bootstrap'
4 | include 'zero-identity'
--------------------------------------------------------------------------------
/zero-bootstrap/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'org.springframework.boot' version "${springBootVersion}"
3 | }
4 |
5 | style {
6 | excludePackages = [
7 | ]
8 | excludeClasses = [
9 | "com.github.dreamhead.zero.bootstrap.Bootstrap"
10 | ]
11 | }
12 |
13 | coverage {
14 | excludePackages = [
15 | ]
16 | excludeClasses = [
17 | "com.github.dreamhead.zero.bootstrap.Bootstrap"
18 | ]
19 | }
20 |
21 | springBoot {
22 | mainClass = 'com.github.dreamhead.zero.bootstrap.Bootstrap'
23 | }
24 |
25 | bootJar {
26 | classifier = 'boot'
27 |
28 | // https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes#jersey-classpath-scanning-limitations
29 | requiresUnpack '**/zero-*.jar'
30 |
31 | launchScript()
32 | }
--------------------------------------------------------------------------------
/zero-bootstrap/src/main/java/com/github/dreamhead/zero/bootstrap/Bootstrap.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.bootstrap;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.boot.autoconfigure.domain.EntityScan;
6 | import org.springframework.context.annotation.ComponentScan;
7 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8 |
9 | @SpringBootApplication
10 | @ComponentScan({
11 | "com.github.dreamhead.zero"
12 | })
13 | @EnableJpaRepositories({
14 | "com.github.dreamhead.zero"
15 | })
16 | @EntityScan({
17 | "com.github.dreamhead.zero"
18 | })
19 | public class Bootstrap {
20 | public static void main(final String[] args) {
21 | SpringApplication.run(Bootstrap.class, args);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/zero-bootstrap/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:mysql://localhost:3306/zero_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
2 | spring.datasource.username=zero
3 | spring.datasource.password=geektime
4 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
--------------------------------------------------------------------------------
/zero-identity/src/main/java/com/github/dreamhead/zero/identity/domain/User.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.domain;
2 |
3 | import lombok.Getter;
4 | import lombok.NoArgsConstructor;
5 |
6 | import javax.persistence.Column;
7 | import javax.persistence.Entity;
8 | import javax.persistence.GeneratedValue;
9 | import javax.persistence.GenerationType;
10 | import javax.persistence.Id;
11 | import javax.persistence.Table;
12 |
13 | @Entity
14 | @Table(name = "zero_users")
15 | @NoArgsConstructor
16 | public class User {
17 | @Id
18 | @GeneratedValue(strategy = GenerationType.IDENTITY)
19 | @Getter
20 | private long id;
21 |
22 | @Column(nullable = false)
23 | @Getter
24 | private String name;
25 |
26 | @Column(nullable = false)
27 | @Getter
28 | private String password;
29 |
30 | public User(final String name, final String password) {
31 | this.name = name;
32 | this.password = password;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/zero-identity/src/main/java/com/github/dreamhead/zero/identity/repository/UserRepository.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.repository;
2 |
3 | import com.github.dreamhead.zero.identity.domain.User;
4 | import org.springframework.data.repository.CrudRepository;
5 | import org.springframework.stereotype.Repository;
6 |
7 | import java.util.Optional;
8 |
9 | @Repository
10 | public interface UserRepository extends CrudRepository {
11 | Optional findByName(String name);
12 | }
13 |
--------------------------------------------------------------------------------
/zero-identity/src/main/java/com/github/dreamhead/zero/identity/resource/UserInfo.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.resource;
2 |
3 | public final class UserInfo {
4 | private final String username;
5 | private final String password;
6 |
7 | public UserInfo(final String username, final String password) {
8 | this.username = username;
9 | this.password = password;
10 | }
11 |
12 | public String getUsername() {
13 | return username;
14 | }
15 |
16 | public String getPassword() {
17 | return password;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/zero-identity/src/main/java/com/github/dreamhead/zero/identity/resource/UserRegistration.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.resource;
2 |
3 | import com.fasterxml.jackson.annotation.JsonCreator;
4 | import com.fasterxml.jackson.annotation.JsonProperty;
5 |
6 | public final class UserRegistration {
7 | private final String username;
8 | private final String password;
9 |
10 | @JsonCreator
11 | public UserRegistration(@JsonProperty("username") final String username,
12 | @JsonProperty("password") final String password) {
13 | this.username = username;
14 | this.password = password;
15 | }
16 |
17 | public String getUsername() {
18 | return username;
19 | }
20 |
21 | public String getPassword() {
22 | return password;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/zero-identity/src/main/java/com/github/dreamhead/zero/identity/resource/UserResource.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.resource;
2 |
3 | import com.github.dreamhead.zero.identity.domain.User;
4 | import com.github.dreamhead.zero.identity.service.UserService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.web.bind.annotation.GetMapping;
8 | import org.springframework.web.bind.annotation.PostMapping;
9 | import org.springframework.web.bind.annotation.RequestBody;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | import java.net.URI;
13 | import java.net.URISyntaxException;
14 | import java.util.List;
15 |
16 | import static java.util.stream.Collectors.toList;
17 |
18 | @RestController
19 | public final class UserResource {
20 | private UserService service;
21 |
22 | @Autowired
23 | public UserResource(final UserService service) {
24 | this.service = service;
25 | }
26 |
27 | @PostMapping("/users")
28 | public ResponseEntity register(@RequestBody final UserRegistration registration) throws URISyntaxException {
29 | try {
30 | User user = this.service.register(registration.getUsername(), registration.getPassword());
31 | return ResponseEntity
32 | .created(new URI("/users/" + user.getId()))
33 | .build();
34 | } catch (IllegalArgumentException e) {
35 | return ResponseEntity.badRequest().build();
36 | }
37 | }
38 |
39 | @GetMapping("/users")
40 | public List list() {
41 | return this.service.list().stream()
42 | .map(user -> new UserInfo(user.getName(), user.getPassword()))
43 | .collect(toList());
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/zero-identity/src/main/java/com/github/dreamhead/zero/identity/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.service;
2 |
3 | import com.github.dreamhead.zero.identity.domain.User;
4 | import com.github.dreamhead.zero.identity.repository.UserRepository;
5 | import com.google.common.collect.ImmutableList;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | @Service
10 | public class UserService {
11 | private final UserRepository repository;
12 |
13 | @Autowired
14 | public UserService(final UserRepository repository) {
15 | this.repository = repository;
16 | }
17 |
18 | public User register(final String name, final String password) {
19 | if (this.repository.findByName(name).isPresent()) {
20 | throw new IllegalArgumentException("User [" + name + "] is already registered");
21 | }
22 |
23 | User user = new User(name, password);
24 | this.repository.save(user);
25 | return user;
26 | }
27 |
28 | public ImmutableList list() {
29 | return ImmutableList.copyOf(this.repository.findAll());
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/zero-identity/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.jersey.type=filter
2 |
3 | spring.datasource.url=jdbc:mysql://localhost:3306/jarvis_test?useUnicode=true&characterEncoding=utf-8&useSSL=false
4 | spring.datasource.name=jarvis
5 | spring.datasource.password=thingworks
6 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
7 |
8 | spring.jpa.show-sql=true
9 |
10 | spring.redis.database=0
11 | spring.redis.host=localhost
12 | spring.redis.port=6379
13 | spring.redis.password=
14 |
15 | spring.jackson.default-property-inclusion=non_null
16 | spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
17 |
18 | jarvis.token.expiry-time-in-seconds=7200
19 |
20 | #jarvis.hitsdb.host=ts-uf60hnqss36vkrmv5.hitsdb.rds.aliyuncs.com
21 | jarvis.hitsdb.host=ts-uf66mggkpk8987fmy.hitsdb.rds.aliyuncs.com
22 | jarvis.hitsdb.port=3242
23 |
24 | jarvis.craft_app_id=D05B52C8-EBE5-4BA6-93A1-E186B77F256F
25 | jarvis.device_app_id=8B9EC63D-3FF8-4D9B-BCF4-894BB58BC31B
26 | jarvis.production_app_id=9E90725A-2A6A-435D-958B-E98CAF7DD353
27 |
--------------------------------------------------------------------------------
/zero-identity/src/test/java/com/github/dreamhead/zero/identity/resource/UserResourceTest.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.resource;
2 |
3 | import com.github.dreamhead.zero.identity.domain.User;
4 | import com.github.dreamhead.zero.identity.service.UserService;
5 | import com.google.common.collect.ImmutableList;
6 | import org.junit.jupiter.api.BeforeEach;
7 | import org.junit.jupiter.api.Test;
8 | import org.springframework.http.ResponseEntity;
9 |
10 | import java.net.URISyntaxException;
11 | import java.util.List;
12 |
13 | import static com.google.common.net.HttpHeaders.LOCATION;
14 | import static org.assertj.core.api.Assertions.assertThat;
15 | import static org.mockito.Mockito.mock;
16 | import static org.mockito.Mockito.when;
17 |
18 | public class UserResourceTest {
19 | private UserResource resource;
20 | private UserService service;
21 |
22 | @BeforeEach
23 | void setUp() {
24 | this.service = mock(UserService.class);
25 | this.resource = new UserResource(this.service);
26 |
27 | }
28 |
29 | @Test
30 | public void should_register() throws URISyntaxException {
31 | User user = mock(User.class);
32 | when(this.service.register("name", "password"))
33 | .thenReturn(user);
34 | when(user.getId()).thenReturn(1L);
35 |
36 | ResponseEntity entity = this.resource.register(new UserRegistration("name", "password"));
37 |
38 | assertThat(entity.getStatusCodeValue()).isEqualTo(201);
39 | assertThat(entity.getHeaders().get(LOCATION).get(0)).isEqualTo("/users/1");
40 | }
41 |
42 | @Test
43 | public void should_not_register_duplicated_user() throws URISyntaxException {
44 | User user = mock(User.class);
45 | when(this.service.register("name", "password"))
46 | .thenThrow(IllegalArgumentException.class);
47 |
48 | ResponseEntity entity = this.resource.register(new UserRegistration("name", "password"));
49 |
50 | assertThat(entity.getStatusCodeValue()).isEqualTo(400);
51 | }
52 |
53 | @Test
54 | public void should_list_all_users() {
55 | when(this.service.list()).thenReturn(ImmutableList.of(new User("name", "password")));
56 | List users = this.resource.list();
57 | assertThat(users.size()).isEqualTo(1);
58 | UserInfo info = users.get(0);
59 | assertThat(info.getUsername()).isEqualTo("name");
60 | assertThat(info.getPassword()).isEqualTo("password");
61 | }
62 | }
--------------------------------------------------------------------------------
/zero-identity/src/test/java/com/github/dreamhead/zero/identity/service/UserServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.github.dreamhead.zero.identity.service;
2 |
3 | import com.github.dreamhead.zero.identity.domain.User;
4 | import com.github.dreamhead.zero.identity.repository.UserRepository;
5 | import com.google.common.collect.ImmutableList;
6 | import org.junit.jupiter.api.BeforeEach;
7 | import org.junit.jupiter.api.Test;
8 |
9 | import java.util.Optional;
10 |
11 | import static org.assertj.core.api.Assertions.assertThat;
12 | import static org.junit.jupiter.api.Assertions.assertThrows;
13 | import static org.mockito.Mockito.mock;
14 | import static org.mockito.Mockito.when;
15 |
16 | public class UserServiceTest {
17 | private UserRepository repository;
18 | private UserService service;
19 |
20 | @BeforeEach
21 | void setUp() {
22 | this.repository = mock(UserRepository.class);
23 | this.service = new UserService(repository);
24 | }
25 |
26 | @Test
27 | public void should_register_user() {
28 | when(this.repository.findByName("name")).thenReturn(Optional.empty());
29 | User user = service.register("name", "password");
30 | assertThat(user.getName()).isEqualTo("name");
31 | assertThat(user.getPassword()).isEqualTo("password");
32 | }
33 |
34 | @Test
35 | public void should_not_register_same_user() {
36 | when(this.repository.findByName("name")).thenReturn(Optional.of(new User("name", "password")));
37 | assertThrows(IllegalArgumentException.class, () -> {
38 | service.register("name", "password");
39 | });
40 | }
41 |
42 | @Test
43 | public void should_list_all_users() {
44 | when(this.repository.findAll()).thenReturn(ImmutableList.of(new User("name", "password")));
45 |
46 | ImmutableList users = service.list();
47 | User user = users.get(0);
48 | assertThat(user.getName()).isEqualTo("name");
49 | assertThat(user.getPassword()).isEqualTo("password");
50 | }
51 | }
--------------------------------------------------------------------------------