Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
S
Simple Web App with HAProxy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamadbashar.disoki
Simple Web App with HAProxy
Commits
c675e93f
Commit
c675e93f
authored
Feb 23, 2025
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simple web app
parent
06b0dc91
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
184 additions
and
10 deletions
+184
-10
workspace.xml
.idea/workspace.xml
+46
-9
pom.xml
pom.xml
+9
-0
Main.java
src/main/java/org/ds/Main.java
+9
-1
WebServer.java
src/main/java/org/ds/WebServer.java
+108
-0
index.html
src/main/resources/index.html
+12
-0
No files found.
.idea/workspace.xml
View file @
c675e93f
...
@@ -5,32 +5,58 @@
...
@@ -5,32 +5,58 @@
</component>
</component>
<component
name=
"ChangeListManager"
>
<component
name=
"ChangeListManager"
>
<list
default=
"true"
id=
"ad904d3c-917a-41ec-8997-0c459fd2925c"
name=
"Changes"
comment=
""
>
<list
default=
"true"
id=
"ad904d3c-917a-41ec-8997-0c459fd2925c"
name=
"Changes"
comment=
""
>
<change
afterPath=
"$PROJECT_DIR$/src/main/java/org/ds/WebServer.java"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/.idea/workspace.xml"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/.idea/workspace.xml"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/pom.xml"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/pom.xml"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/pom.xml"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/pom.xml"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/src/main/java/org/ds/Main.java"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/src/main/java/org/ds/Main.java"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/src/main/resources/index.html"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/src/main/resources/index.html"
afterDir=
"false"
/>
</list>
</list>
<option
name=
"SHOW_DIALOG"
value=
"false"
/>
<option
name=
"SHOW_DIALOG"
value=
"false"
/>
<option
name=
"HIGHLIGHT_CONFLICTS"
value=
"true"
/>
<option
name=
"HIGHLIGHT_CONFLICTS"
value=
"true"
/>
<option
name=
"HIGHLIGHT_NON_ACTIVE_CHANGELIST"
value=
"false"
/>
<option
name=
"HIGHLIGHT_NON_ACTIVE_CHANGELIST"
value=
"false"
/>
<option
name=
"LAST_RESOLUTION"
value=
"IGNORE"
/>
<option
name=
"LAST_RESOLUTION"
value=
"IGNORE"
/>
</component>
</component>
<component
name=
"FileTemplateManagerImpl"
>
<option
name=
"RECENT_TEMPLATES"
>
<list>
<option
value=
"Class"
/>
</list>
</option>
</component>
<component
name=
"Git.Settings"
>
<component
name=
"Git.Settings"
>
<option
name=
"RECENT_GIT_ROOT_PATH"
value=
"$PROJECT_DIR$"
/>
<option
name=
"RECENT_GIT_ROOT_PATH"
value=
"$PROJECT_DIR$"
/>
</component>
</component>
<component
name=
"ProjectColorInfo"
>
<![CDATA[
{
<component
name=
"ProjectColorInfo"
>
{
"associatedIndex"
: 1
"
associatedIndex
"
: 1
}
]]>
</component>
}
</component>
<component
name=
"ProjectId"
id=
"2tREiXPX513LksO5LzaY0KttqM7"
/>
<component
name=
"ProjectId"
id=
"2tREiXPX513LksO5LzaY0KttqM7"
/>
<component
name=
"ProjectLevelVcsManager"
settingsEditedManually=
"true"
/>
<component
name=
"ProjectLevelVcsManager"
settingsEditedManually=
"true"
/>
<component
name=
"ProjectViewState"
>
<component
name=
"ProjectViewState"
>
<option
name=
"hideEmptyMiddlePackages"
value=
"true"
/>
<option
name=
"hideEmptyMiddlePackages"
value=
"true"
/>
<option
name=
"showLibraryContents"
value=
"true"
/>
<option
name=
"showLibraryContents"
value=
"true"
/>
</component>
</component>
<component
name=
"PropertiesComponent"
>
<![CDATA[
{
<component
name=
"PropertiesComponent"
>
{
"keyToString"
: {
"
keyToString
"
: {
"RunOnceActivity.ShowReadmeOnStart": "true"
,
"
RunOnceActivity.ShowReadmeOnStart
"
:
"
true
"
,
"git-widget-placeholder": "master"
,
"
git-widget-placeholder
"
:
"
master
"
,
"kotlin-language-version-configured": "true"
"
kotlin-language-version-configured
"
:
"
true
"
}
}
}]]>
</component>
}
</component>
<component
name=
"RunManager"
>
<configuration
default=
"true"
type=
"JetRunConfigurationType"
>
<module
name=
"simple-web-app"
/>
<method
v=
"2"
>
<option
name=
"Make"
enabled=
"true"
/>
</method>
</configuration>
<configuration
default=
"true"
type=
"KotlinStandaloneScriptRunConfigurationType"
>
<module
name=
"simple-web-app"
/>
<option
name=
"filePath"
/>
<method
v=
"2"
>
<option
name=
"Make"
enabled=
"true"
/>
</method>
</configuration>
</component>
<component
name=
"SpellCheckerSettings"
RuntimeDictionaries=
"0"
Folders=
"0"
CustomDictionaries=
"0"
DefaultDictionary=
"application-level"
UseSingleDictionary=
"true"
transferred=
"true"
/>
<component
name=
"SpellCheckerSettings"
RuntimeDictionaries=
"0"
Folders=
"0"
CustomDictionaries=
"0"
DefaultDictionary=
"application-level"
UseSingleDictionary=
"true"
transferred=
"true"
/>
<component
name=
"TaskManager"
>
<component
name=
"TaskManager"
>
<task
active=
"true"
id=
"Default"
summary=
"Default task"
>
<task
active=
"true"
id=
"Default"
summary=
"Default task"
>
...
@@ -42,4 +68,15 @@
...
@@ -42,4 +68,15 @@
</task>
</task>
<servers
/>
<servers
/>
</component>
</component>
<component
name=
"Vcs.Log.Tabs.Properties"
>
<option
name=
"TAB_STATES"
>
<map>
<entry
key=
"MAIN"
>
<value>
<State
/>
</value>
</entry>
</map>
</option>
</component>
</project>
</project>
\ No newline at end of file
pom.xml
View file @
c675e93f
...
@@ -14,4 +14,13 @@
...
@@ -14,4 +14,13 @@
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>
org.jsoup
</groupId>
<artifactId>
jsoup
</artifactId>
<version>
1.12.1
</version>
</dependency>
</dependencies>
</project>
</project>
\ No newline at end of file
src/main/java/org/ds/Main.java
View file @
c675e93f
...
@@ -2,6 +2,14 @@ package org.ds;
...
@@ -2,6 +2,14 @@ package org.ds;
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello world!"
);
if
(
args
.
length
!=
2
)
{
System
.
out
.
println
(
"java -jar (jar name) PORT_NUMBER SERVER_NAME"
);
}
int
currentServerPort
=
Integer
.
parseInt
(
args
[
0
]);
String
serverName
=
args
[
1
];
WebServer
webServer
=
new
WebServer
(
currentServerPort
,
serverName
);
webServer
.
startServer
();
}
}
}
}
\ No newline at end of file
src/main/java/org/ds/WebServer.java
0 → 100644
View file @
c675e93f
package
org
.
ds
;
import
com.sun.net.httpserver.HttpExchange
;
import
com.sun.net.httpserver.HttpServer
;
import
org.jsoup.Jsoup
;
import
org.jsoup.nodes.Document
;
import
org.jsoup.nodes.Element
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.InetSocketAddress
;
import
java.util.concurrent.Executors
;
public
class
WebServer
{
private
static
final
String
STATUS_ENDPOINT
=
"/status"
;
private
static
final
String
HOME_PAGE_ENDPOINT
=
"/"
;
private
static
final
String
HTML_PAGE
=
"index.html"
;
private
final
int
port
;
private
HttpServer
server
;
private
final
String
serverName
;
public
WebServer
(
int
port
,
String
serverName
)
{
this
.
port
=
port
;
this
.
serverName
=
serverName
;
}
public
void
startServer
()
{
try
{
this
.
server
=
HttpServer
.
create
(
new
InetSocketAddress
(
port
),
0
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
;
}
server
.
createContext
(
STATUS_ENDPOINT
,
this
::
handleStatusCheckRequest
);
server
.
createContext
(
HOME_PAGE_ENDPOINT
,
this
::
handleHomePageRequest
);
server
.
setExecutor
(
Executors
.
newFixedThreadPool
(
8
));
System
.
out
.
println
(
String
.
format
(
"Started server %s on port %d "
,
serverName
,
port
));
server
.
start
();
}
private
void
handleHomePageRequest
(
HttpExchange
exchange
)
throws
IOException
{
if
(!
exchange
.
getRequestMethod
().
equalsIgnoreCase
(
"get"
))
{
exchange
.
close
();
return
;
}
System
.
out
.
println
(
String
.
format
(
"%s received a request"
,
this
.
serverName
));
exchange
.
getResponseHeaders
().
add
(
"Content-Type"
,
"text/html"
);
exchange
.
getResponseHeaders
().
add
(
"Cache-Control"
,
"no-cache"
);
byte
[]
response
=
loadHtml
(
HTML_PAGE
);
sendResponse
(
response
,
exchange
);
}
/**
* Loads the HTML page to be fetched to the web browser
*
* @param htmlFilePath - The relative path to the html file
* @throws IOException
*/
private
byte
[]
loadHtml
(
String
htmlFilePath
)
throws
IOException
{
InputStream
htmlInputStream
=
getClass
().
getResourceAsStream
(
htmlFilePath
);
if
(
htmlInputStream
==
null
)
{
return
new
byte
[]{};
}
Document
document
=
Jsoup
.
parse
(
htmlInputStream
,
"UTF-8"
,
""
);
String
modifiedHtml
=
modifyHtmlDocument
(
document
);
return
modifiedHtml
.
getBytes
();
}
/**
* Fills the server's name and local time in theHTML document
*
* @param document - original HTML document
*/
private
String
modifyHtmlDocument
(
Document
document
)
{
Element
serverNameElement
=
document
.
selectFirst
(
"#server_name"
);
serverNameElement
.
appendText
(
serverName
);
return
document
.
toString
();
}
private
void
handleStatusCheckRequest
(
HttpExchange
exchange
)
throws
IOException
{
if
(!
exchange
.
getRequestMethod
().
equalsIgnoreCase
(
"get"
))
{
exchange
.
close
();
return
;
}
System
.
out
.
println
(
"Received a health check"
);
String
responseMessage
=
"Server is alive\n"
;
sendResponse
(
responseMessage
.
getBytes
(),
exchange
);
}
private
void
sendResponse
(
byte
[]
responseBytes
,
HttpExchange
exchange
)
throws
IOException
{
exchange
.
sendResponseHeaders
(
200
,
responseBytes
.
length
);
OutputStream
outputStream
=
exchange
.
getResponseBody
();
outputStream
.
write
(
responseBytes
);
outputStream
.
flush
();
outputStream
.
close
();
}
}
src/main/resources/index.html
View file @
c675e93f
<!DOCTYPE html>
<html>
<head>
<title>
Distributed Search
</title>
<meta
http-equiv=
"cache-control"
content=
"no-cache"
/>
</head>
<body
style=
"background: #e6f3ff;"
>
<h1
style=
"color:blue; text-align: center; font-style: bold"
id=
"server_name_title"
>
Welcome to
</h1>
<h1
style=
"color:#00b4ff; text-align: center; font-style: italic"
id=
"server_name"
></h1>
</body>
</html>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment