| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
"os" |
| 8 |
|
| 9 |
"github.com/google/s2a-go" |
| 10 |
"google.golang.org/api/option" |
| 11 |
"google.golang.org/appengine" |
| 12 |
"google.golang.org/grpc" |
| 13 |
"google.golang.org/grpc/credentials/oauth" |
| 14 |
|
| 15 |
translate "cloud.google.com/go/translate/apiv3" |
| 16 |
translatepb "cloud.google.com/go/translate/apiv3/translatepb" |
| 17 |
) |
| 18 |
|
| 19 |
const serverAddr = "translate.mtls.googleapis.com:443" |
| 20 |
|
| 21 |
func indexHandler(w http.ResponseWriter, r *http.Request) { |
| 22 |
if r.URL.Path != "/" { |
| 23 |
http.NotFound(w, r) |
| 24 |
return |
| 25 |
} |
| 26 |
ctx := appengine.NewContext(r) |
| 27 |
creds, err := s2a.NewClientCreds(&s2a.ClientOptions{S2AAddress: "metadata.google.internal:80"}) |
| 28 |
if err != nil { |
| 29 |
errorMessage := "Failed to create S2A client credentials: %v" + err.Error() |
| 30 |
log.Fatalf("%v", errorMessage) |
| 31 |
http.Error(w, errorMessage, http.StatusBadRequest) |
| 32 |
return |
| 33 |
} |
| 34 |
perRPCCreds, err := oauth.NewApplicationDefault(ctx, |
| 35 |
"https://www.googleapis.com/auth/cloud-platform") |
| 36 |
if err != nil { |
| 37 |
errorMessage := "Failed to get per-RPC credentials: %v" + err.Error() |
| 38 |
log.Fatalf("%v", errorMessage) |
| 39 |
http.Error(w, errorMessage, http.StatusBadRequest) |
| 40 |
return |
| 41 |
} |
| 42 |
opts := []grpc.DialOption{ |
| 43 |
grpc.WithTransportCredentials(creds), |
| 44 |
grpc.WithPerRPCCredentials(perRPCCreds), |
| 45 |
grpc.WithBlock(), |
| 46 |
grpc.WithReturnConnectionError(), |
| 47 |
} |
| 48 |
conn, err := grpc.DialContext(ctx, serverAddr, opts...) |
| 49 |
if err != nil { |
| 50 |
errorMessage := "Client: failed to connect: %v" + err.Error() |
| 51 |
log.Fatalf("%v", errorMessage) |
| 52 |
http.Error(w, errorMessage, http.StatusBadRequest) |
| 53 |
return |
| 54 |
} |
| 55 |
defer conn.Close() |
| 56 |
cliOpts := []option.ClientOption{option.WithGRPCConn(conn)} |
| 57 |
client, err := translate.NewTranslationClient(ctx, cliOpts...) |
| 58 |
if err != nil { |
| 59 |
errorMessage := "Failed to create client: %v" + err.Error() |
| 60 |
log.Fatalf("%v", errorMessage) |
| 61 |
http.Error(w, errorMessage, http.StatusBadRequest) |
| 62 |
} |
| 63 |
req := &translatepb.TranslateTextRequest{ |
| 64 |
Contents: []string{"Hello World!"}, |
| 65 |
SourceLanguageCode: "en", |
| 66 |
TargetLanguageCode: "es", |
| 67 |
Parent: fmt.Sprintf("projects/%s", os.Getenv("GOOGLE_CLOUD_PROJECT")), |
| 68 |
} |
| 69 |
_, err = client.TranslateText(ctx, req) |
| 70 |
if err != nil { |
| 71 |
errorMessage := "Failed to translate: %v" + err.Error() |
| 72 |
log.Fatalf("%v", errorMessage) |
| 73 |
http.Error(w, errorMessage, http.StatusBadRequest) |
| 74 |
} |
| 75 |
fmt.Fprintf(w, "success") |
| 76 |
} |
| 77 |
|
| 78 |
func main() { |
| 79 |
http.HandleFunc("/", indexHandler) |
| 80 |
|
| 81 |
port := os.Getenv("PORT") |
| 82 |
if port == "" { |
| 83 |
port = "8080" |
| 84 |
log.Printf("Defaulting to port %s", port) |
| 85 |
} |
| 86 |
|
| 87 |
log.Printf("Listening on port %s", port) |
| 88 |
if err := http.ListenAndServe(":"+port, nil); err != nil { |
| 89 |
log.Fatal(err) |
| 90 |
} |
| 91 |
} |