File tree Expand file tree Collapse file tree 12 files changed +225
-3
lines changed
02-timer-task/src/main/java
03-http-server/src/main/java
ir/moke/module/with_dependency Expand file tree Collapse file tree 12 files changed +225
-3
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xmlns =" http://maven.apache.org/POM/4.0.0"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <parent >
8+ <groupId >ir.moke</groupId >
9+ <artifactId >example-modules</artifactId >
10+ <version >1.0-SNAPSHOT</version >
11+ </parent >
12+
13+ <artifactId >00-common</artifactId >
14+ <version >1.0-SNAPSHOT</version >
15+
16+
17+ <dependencies >
18+ <dependency >
19+ <groupId >ir.moke.jos</groupId >
20+ <artifactId >jos-api</artifactId >
21+ </dependency >
22+ <dependency >
23+ <groupId >org.slf4j</groupId >
24+ <artifactId >slf4j-api</artifactId >
25+ </dependency >
26+ <dependency >
27+ <groupId >com.google.code.gson</groupId >
28+ <artifactId >gson</artifactId >
29+ </dependency >
30+ </dependencies >
31+
32+ <build >
33+ <plugins >
34+ <plugin >
35+ <groupId >ir.moke.jpkg</groupId >
36+ <artifactId >maven-jpkg</artifactId >
37+ <version >1.0</version >
38+ <configuration >
39+ <name >00-common</name >
40+ <version >0.1</version >
41+ <description >This module contain some common tools</description >
42+ <maintainer >Mahdi Sheikh Hosseini</maintainer >
43+ </configuration >
44+ </plugin >
45+ </plugins >
46+ </build >
47+
48+ </project >
Original file line number Diff line number Diff line change 1+ package ir .moke .module .common ;
2+
3+ import com .google .gson .Gson ;
4+
5+ public class JsonUtils {
6+ private static final Gson gson = new Gson ();
7+
8+ public static String toJson (Object o ) {
9+ return gson .toJson (o );
10+ }
11+
12+ public static <T > T toObject (String json , Class <T > typeClass ) {
13+ return gson .fromJson (json , typeClass );
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ module p00_common {
2+ requires jos .api ;
3+ requires org .slf4j ;
4+ requires com .google .gson ;
5+ exports ir .moke .module .common ;
6+ }
Original file line number Diff line number Diff line change 11import ir .moke .module .timer .ModuleRunner ;
22
3- module p01_basic {
3+ module p02_timer_task {
44 requires jos .api ;
55 requires org .slf4j ;
66 exports ir .moke .module .timer ;
Original file line number Diff line number Diff line change 11import ir .moke .module .httpserver .ModuleRunner ;
22
3- module p01_basic {
3+ module p03_http_server {
44 requires jos .api ;
55 requires org .slf4j ;
66 requires jdk .httpserver ;
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xmlns =" http://maven.apache.org/POM/4.0.0"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <parent >
8+ <groupId >ir.moke</groupId >
9+ <artifactId >example-modules</artifactId >
10+ <version >1.0-SNAPSHOT</version >
11+ </parent >
12+
13+ <artifactId >04-simple-dependency</artifactId >
14+ <version >1.0-SNAPSHOT</version >
15+
16+ <dependencies >
17+ <dependency >
18+ <groupId >ir.moke.jos</groupId >
19+ <artifactId >jos-api</artifactId >
20+ </dependency >
21+ <dependency >
22+ <groupId >org.slf4j</groupId >
23+ <artifactId >slf4j-api</artifactId >
24+ </dependency >
25+ <dependency >
26+ <groupId >ir.moke</groupId >
27+ <artifactId >00-common</artifactId >
28+ <version >1.0-SNAPSHOT</version >
29+ <scope >provided</scope >
30+ </dependency >
31+ </dependencies >
32+
33+ <build >
34+ <plugins >
35+ <plugin >
36+ <groupId >ir.moke.jpkg</groupId >
37+ <artifactId >maven-jpkg</artifactId >
38+ <version >1.0</version >
39+ <configuration >
40+ <name >04-simple-dependency</name >
41+ <version >0.1</version >
42+ <description >This module dependent to common module</description >
43+ <maintainer >Mahdi Sheikh Hosseini</maintainer >
44+ <dependencies >
45+ <!-- see 00-common pom.xml line 39,40 -->
46+ <dependency >
47+ <name >00-common</name >
48+ <version >0.1</version >
49+ </dependency >
50+ </dependencies >
51+ </configuration >
52+ </plugin >
53+ </plugins >
54+ </build >
55+
56+
57+ </project >
Original file line number Diff line number Diff line change 1+ package ir .moke .module .with_dependency ;
2+
3+ import com .sun .net .httpserver .HttpServer ;
4+ import ir .moke .module .with_dependency .resources .PersonResources ;
5+ import org .slf4j .Logger ;
6+ import org .slf4j .LoggerFactory ;
7+
8+ import java .net .InetSocketAddress ;
9+
10+ public class HttpContainer {
11+ public static final HttpContainer instance = new HttpContainer ();
12+ private static final Logger logger = LoggerFactory .getLogger (HttpContainer .class );
13+ private HttpServer httpServer ;
14+ private HttpContainer () {
15+ try {
16+ String host = System .getenv ("HTTP_SERVER_HOST" );
17+ int port = Integer .parseInt (System .getenv ("HTTP_SERVER_PORT" ));
18+ httpServer = HttpServer .create (new InetSocketAddress (host , port ), -1 );
19+ } catch (Exception e ) {
20+ logger .error ("Http container error" , e );
21+ }
22+ }
23+
24+ public void start () {
25+ httpServer .createContext ("/person" , new PersonResources ());
26+ httpServer .start ();
27+ }
28+
29+ public void stop () {
30+ httpServer .stop (1 );
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package ir .moke .module .with_dependency ;
2+
3+ import ir .moke .jos .api .JModule ;
4+ import org .slf4j .Logger ;
5+ import org .slf4j .LoggerFactory ;
6+
7+ public class ModuleRunner implements JModule {
8+ private static final Logger logger = LoggerFactory .getLogger (ModuleRunner .class );
9+
10+ @ Override
11+ public void start () {
12+ logger .info ("Start http server" );
13+ HttpContainer .instance .start ();
14+ }
15+
16+ @ Override
17+ public void stop () {
18+ logger .info ("Stop http server" );
19+ HttpContainer .instance .stop ();
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package ir .moke .module .with_dependency .model ;
2+
3+ public record Person (String name ,String family ) {
4+ }
Original file line number Diff line number Diff line change 1+ package ir .moke .module .with_dependency .resources ;
2+
3+ import com .sun .net .httpserver .HttpExchange ;
4+ import com .sun .net .httpserver .HttpHandler ;
5+ import ir .moke .module .common .JsonUtils ;
6+ import ir .moke .module .with_dependency .model .Person ;
7+
8+ import java .io .IOException ;
9+ import java .io .OutputStream ;
10+ import java .nio .charset .StandardCharsets ;
11+
12+ public class PersonResources implements HttpHandler {
13+ @ Override
14+ public void handle (HttpExchange exchange ) throws IOException {
15+ Person person = new Person ("Stephan" , "Morphy" );
16+ String json = JsonUtils .toJson (person );
17+ sendResponse (exchange , json );
18+ }
19+
20+ private void sendResponse (HttpExchange exchange , String response ) throws IOException {
21+ exchange .getResponseHeaders ().add ("Content-Type" ,"application/json" );
22+ exchange .sendResponseHeaders (200 , response .length ());
23+ OutputStream responseBody = exchange .getResponseBody ();
24+ responseBody .write (response .getBytes (StandardCharsets .UTF_8 ));
25+ responseBody .flush ();
26+ responseBody .flush ();
27+ responseBody .close ();
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments