improved readme File and removed unnessasary part in reply
This commit is contained in:
parent
a108298c37
commit
adeb09253e
63
README.md
63
README.md
|
@ -1,4 +1,4 @@
|
||||||
# Go Server with Translation Functionality
|
# Translator-GO-Endpoint
|
||||||
|
|
||||||
This documentation provides an overview and setup guide for a Go-based server designed to handle translation tasks with streaming support. The application leverages Python scripts for actual translation and package management functionalities, taking advantage of both Go's robust HTTP serving capabilities and Python's extensive libraries.
|
This documentation provides an overview and setup guide for a Go-based server designed to handle translation tasks with streaming support. The application leverages Python scripts for actual translation and package management functionalities, taking advantage of both Go's robust HTTP serving capabilities and Python's extensive libraries.
|
||||||
|
|
||||||
|
@ -26,57 +26,46 @@ Ensure you have the following installed:
|
||||||
|
|
||||||
1. Clone the Repository: Clone or download the source code to your local machine.
|
1. Clone the Repository: Clone or download the source code to your local machine.
|
||||||
2. Install Required Python Packages: Navigate to the `translator` directory and install necessary Python packages using pip:
|
2. Install Required Python Packages: Navigate to the `translator` directory and install necessary Python packages using pip:
|
||||||
```bash
|
```bash
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
3. Running the Server: From the root directory of the project, start the server with the following command:
|
3. Running the Server: From the root directory of the project, start the server with the following command:
|
||||||
```bash
|
```bash
|
||||||
go run main.go
|
go run main.go
|
||||||
|
```
|
||||||
|
- alternativly: Convert to exectuable via:
|
||||||
|
```bash
|
||||||
|
go build main.go
|
||||||
|
```
|
||||||
|
|
||||||
The server listens on port 53184 by default, but you can set the `PORT` environment variable to use a different port.
|
The server listens on port 53184 by default, but you can set the `port` variable, inside the `main.go` File, to use a different port.
|
||||||
|
|
||||||
## API Usage
|
## API Usage
|
||||||
|
|
||||||
To perform a translation, send a JSON-formatted POST request to `/api` with the following structure:
|
To perform a translation, send a JSON-formatted POST request to /api with the following structure:
|
||||||
|
```bash
|
||||||
```json
|
|
||||||
{
|
{
|
||||||
"message": "Hello, world!",
|
"message": "Hello, world!",
|
||||||
"from": "en",
|
"from": "en",
|
||||||
"to": "de"
|
"to": "de"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
- message: The text you wish to translate.
|
||||||
|
- from: The source language code (ISO 639-1 format).
|
||||||
|
- to: The target language code (ISO 639-1 format).
|
||||||
|
|
||||||
- `message`: The text you wish to translate.
|
|
||||||
- `from`: The source language code (ISO 639-1 format).
|
|
||||||
- `to`: The target language code (ISO 639-1 format).
|
|
||||||
|
|
||||||
The response will be a stream of translation progress, including package download updates if necessary.
|
### Request Example:
|
||||||
|
|
||||||
## Code Structure
|
Request:
|
||||||
|
```bash
|
||||||
|
curl -X POST -H "Content-Type: application/json" -d '{"message": "Hello, world!", "from": "en", "to": "de"}' "http://localhost:53184"
|
||||||
|
```
|
||||||
|
|
||||||
### Main Components
|
Reply:
|
||||||
|
```bash
|
||||||
- `Message` and `StreamResponse` structs: Define the structures for handling translation requests and streaming responses.
|
{"from":"en","to":"de","createdAt":"2024-05-14T16:32:36Z","response":"Hallo, Welt!"}
|
||||||
- `streamResponse`: Sets up the HTTP headers for SSE and streams translation or download progress.
|
```
|
||||||
- `downloadPackages` & `executeTranslator`: Handle the package management and execution of translation scripts.
|
|
||||||
- `CheckLanguagesInstalled`: Checks if the necessary language packages are installed.
|
|
||||||
- `handleRequest`: API endpoint handler that processes translation requests.
|
|
||||||
|
|
||||||
### Considerations
|
|
||||||
|
|
||||||
* Verify that both Python and the necessary packages are installed and accessible by the Go application. It is recommended to use a virtual environment for managing Python dependencies.
|
|
||||||
* Make sure to adjust the Python script paths in the Go code to match your directory structure.
|
|
||||||
* Customize the error handling and logging mechanisms according to your operational requirements. Consider using structured logging for better log analysis and monitoring.
|
|
||||||
|
|
||||||
### Best Practices
|
|
||||||
|
|
||||||
* Separate configuration from code by using a configuration file or environment variables.
|
|
||||||
* Implement security best practices, such as validating and sanitizing user inputs, protecting against common web vulnerabilities, and using secure connections.
|
|
||||||
* Use continuous integration and testing to ensure the reliability and stability of the application.
|
|
||||||
|
|
||||||
### Conclusion
|
|
||||||
|
|
||||||
This server application showcases an effective approach to integrating Go's high-performance web serving capabilities with Python's powerful scripting abilities for dynamic content translation and real-time user feedback. By following best practices and continuously monitoring and improving the application, you can build a robust, secure, and scalable solution for your needs.
|
|
||||||
|
|
||||||
### License
|
### License
|
||||||
|
|
||||||
|
|
26
main.go
26
main.go
|
@ -57,7 +57,7 @@ func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, mess
|
||||||
|
|
||||||
for msg := range messages {
|
for msg := range messages {
|
||||||
formattedMessage := createStreamResponse(fromLanguage, toLanguage, msg)
|
formattedMessage := createStreamResponse(fromLanguage, toLanguage, msg)
|
||||||
fmt.Fprintf(w, "data: %s\n\n", formattedMessage)
|
fmt.Fprintf(w, " %s\n\n", formattedMessage)
|
||||||
flusher.Flush() // Ensure client receives the update immediately
|
flusher.Flush() // Ensure client receives the update immediately
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -216,17 +216,17 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Define the HTTP handler function
|
// Define the HTTP handler function
|
||||||
http.HandleFunc("/", handleRequest) // This ensures all requests are routed to handleRequest
|
http.HandleFunc("/", handleRequest) // This ensures all requests are routed to handleRequest
|
||||||
|
|
||||||
// Define the port and ensure listening on localhost only
|
// Define the port and ensure listening on localhost only
|
||||||
port := "53184"
|
port := "53184"
|
||||||
address := fmt.Sprintf("localhost:%s", port) // Listen on localhost interface only
|
address := fmt.Sprintf("localhost:%s", port) // Listen on localhost interface only
|
||||||
|
|
||||||
// Start the HTTP server
|
// Start the HTTP server
|
||||||
fmt.Printf("Listening on %s...\n", address)
|
fmt.Printf("Listening on %s...\n", address)
|
||||||
err := http.ListenAndServe(address, nil)
|
err := http.ListenAndServe(address, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to start server: %v", err)
|
log.Fatalf("Failed to start server: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
argostranslate
|
Loading…
Reference in New Issue