You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
3.8 KiB
71 lines
3.8 KiB
import java.io.*;
|
|
import java.net.*;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class TestServer {
|
|
public static void main(String[] args) {
|
|
try {
|
|
int port = 8080;
|
|
ServerSocket server = new ServerSocket(port);
|
|
System.out.println("\nTest Server Started Successfully!");
|
|
System.out.println("Listening on Port: " + port);
|
|
System.out.println("Access Address: http://localhost:" + port + "/api");
|
|
System.out.println("\nPress Ctrl+C to Stop Service\n");
|
|
|
|
while (true) {
|
|
Socket client = server.accept();
|
|
handle(client);
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println("Server Error: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
private static void handle(Socket client) {
|
|
new Thread(() -> {
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
OutputStream out = client.getOutputStream()) {
|
|
|
|
String line = in.readLine();
|
|
System.out.println("Request: " + line);
|
|
|
|
// Read all request headers
|
|
while ((line = in.readLine()) != null && !line.isEmpty()) {}
|
|
|
|
// Determine response based on request path
|
|
String response = "{\"status\":\"success\",\"message\":\"Backend Service Started!\"}";
|
|
|
|
// Handle product search request
|
|
if (line != null && line.contains("/api/products/search")) {
|
|
// Create mock product data
|
|
response = "{\"status\":200,\"message\":\"success\",\"data\":[{\"id\":\"1\",\"title\":\"iPhone X\",\"description\":\"iPhone X 64GB\",\"category\":\"手机\",\"price\":3999.0,\"image_urls\":[\"image_url1\"],\"contact\":\"13800138000\",\"publish_time\":1620000000,\"seller_id\":\"1\"},{\"id\":\"2\",\"title\":\"MacBook Pro\",\"description\":\"MacBook Pro 13寸\",\"category\":\"电脑\",\"price\":8999.0,\"image_urls\":[\"image_url2\"],\"contact\":\"13800138001\",\"publish_time\":1620000001,\"seller_id\":\"2\"}]}";
|
|
}
|
|
|
|
// Handle product list request
|
|
if (line != null && line.contains("/api/products")) {
|
|
// Create mock product list data
|
|
response = "{\"status\":200,\"message\":\"success\",\"data\":[{\"id\":\"1\",\"title\":\"iPhone X\",\"description\":\"iPhone X 64GB\",\"category\":\"手机\",\"price\":3999.0,\"image_urls\":[\"image_url1\"],\"contact\":\"13800138000\",\"publish_time\":1620000000,\"seller_id\":\"1\"},{\"id\":\"2\",\"title\":\"MacBook Pro\",\"description\":\"MacBook Pro 13寸\",\"category\":\"电脑\",\"price\":8999.0,\"image_urls\":[\"image_url2\"],\"contact\":\"13800138001\",\"publish_time\":1620000001,\"seller_id\":\"2\"}]}";
|
|
}
|
|
|
|
// Response
|
|
String headers = "HTTP/1.1 200 OK\r\n"
|
|
+ "Content-Type: application/json; charset=UTF-8\r\n"
|
|
+ "Access-Control-Allow-Origin: *\r\n"
|
|
+ "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"
|
|
+ "Access-Control-Allow-Headers: Content-Type\r\n"
|
|
+ "Content-Length: " + response.getBytes().length + "\r\n"
|
|
+ "\r\n";
|
|
|
|
out.write(headers.getBytes());
|
|
out.write(response.getBytes());
|
|
out.flush();
|
|
|
|
} catch (Exception e) {
|
|
System.err.println("Handle Error: " + e.getMessage());
|
|
} finally {
|
|
try { client.close(); } catch (Exception e) {}
|
|
}
|
|
}).start();
|
|
}
|
|
} |