Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
Distributed-Search-Engine
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
tammam.alsoleman
Distributed-Search-Engine
Commits
1660c00d
Commit
1660c00d
authored
Jan 21, 2026
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create the CoordinatorWebServer
parent
67251eae
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
0 deletions
+85
-0
CoordinatorWebServer.java
...java/com/distributed/search/web/CoordinatorWebServer.java
+85
-0
No files found.
src/main/java/com/distributed/search/web/CoordinatorWebServer.java
0 → 100644
View file @
1660c00d
package
com
.
distributed
.
search
.
web
;
import
com.distributed.search.grpc.SearchClient
;
import
com.distributed.search.logic.FileManager
;
import
com.distributed.search.model.SearchResponse
;
import
com.google.gson.Gson
;
import
com.sun.net.httpserver.HttpExchange
;
import
com.sun.net.httpserver.HttpServer
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.net.InetSocketAddress
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.concurrent.Executors
;
/**
* Internal HTTP Server running on the Leader node.
* It receives search queries from the Frontend and returns results in JSON format.
*/
public
class
CoordinatorWebServer
{
private
static
final
String
SEARCH_ENDPOINT
=
"/search"
;
private
final
int
port
;
private
final
SearchClient
searchClient
;
private
final
String
storageDir
;
private
HttpServer
server
;
private
final
Gson
gson
=
new
Gson
();
public
CoordinatorWebServer
(
int
port
,
SearchClient
searchClient
,
String
storageDir
)
{
this
.
port
=
port
;
this
.
searchClient
=
searchClient
;
this
.
storageDir
=
storageDir
;
}
public
void
startServer
()
{
try
{
this
.
server
=
HttpServer
.
create
(
new
InetSocketAddress
(
port
),
0
);
server
.
createContext
(
SEARCH_ENDPOINT
,
this
::
handleSearchRequest
);
server
.
setExecutor
(
Executors
.
newFixedThreadPool
(
8
));
server
.
start
();
System
.
out
.
println
(
"Leader Internal Web Server started on port: "
+
port
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
void
stopServer
()
{
if
(
server
!=
null
)
server
.
stop
(
0
);
}
/**
* Handles search requests from the Frontend.
* Expects query param: /search?query=word1+word2
*/
private
void
handleSearchRequest
(
HttpExchange
exchange
)
throws
IOException
{
if
(!
exchange
.
getRequestMethod
().
equalsIgnoreCase
(
"get"
))
{
exchange
.
close
();
return
;
}
// Extract query from URI
String
query
=
exchange
.
getRequestURI
().
getQuery
().
split
(
"="
)[
1
];
List
<
String
>
terms
=
Arrays
.
asList
(
query
.
toLowerCase
().
split
(
"\\+"
));
System
.
out
.
println
(
"Internal API received query: "
+
terms
);
// Execute distributed search
List
<
String
>
allFiles
=
FileManager
.
getSortedDocumentNames
(
storageDir
);
List
<
SearchResponse
.
DocumentResult
>
results
=
searchClient
.
performSearch
(
terms
,
allFiles
);
// Convert results to JSON using GSON
String
jsonResponse
=
gson
.
toJson
(
results
);
sendResponse
(
jsonResponse
.
getBytes
(),
exchange
);
}
private
void
sendResponse
(
byte
[]
responseBytes
,
HttpExchange
exchange
)
throws
IOException
{
exchange
.
getResponseHeaders
().
add
(
"Content-Type"
,
"application/json"
);
exchange
.
sendResponseHeaders
(
200
,
responseBytes
.
length
);
OutputStream
outputStream
=
exchange
.
getResponseBody
();
outputStream
.
write
(
responseBytes
);
outputStream
.
flush
();
outputStream
.
close
();
}
}
\ 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