-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
62 lines (51 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*Package main initalizes the application services and sets up the serveMux that handles http requests. */
package main
import (
"context"
"log"
"net/http"
"cloud.google.com/go/translate"
"github.com/frahman5/googletranslateclone/services/config"
"github.com/frahman5/googletranslateclone/services/publicapi"
"github.com/frahman5/googletranslateclone/services/utils"
"google.golang.org/appengine"
)
// Services
var cfg config.Config
var api publicapi.API
func main() {
var (
ctx context.Context
client *translate.Client
buildDir string
err error
)
// Create COnfig
if cfg, err = config.NewConfig(); err != nil {
log.Fatalf("Failed to create config: %v", err)
}
// Create google translate client
ctx = context.Background()
if client, err = translate.NewClient(ctx); err != nil {
log.Fatalf("Failed to create client: %v", err)
} // We don't do a defer client.Close() because the client should never close! :)
// Create the API object
api = publicapi.API{Config: cfg, TranslationClient: client, Context: ctx}
// List supported languages
if err = utils.ListSupportedLanguages("en"); err != nil {
log.Fatalf("Failed to get supported languages: %v", err)
}
// Set up ServeMux
if buildDir, err = utils.GetAbsoluteFilepath("/build"); err != nil {
log.Fatal(err)
}
http.HandleFunc("/api/v1/translate", api.HandleTranslate)
http.HandleFunc("/api/v1/detectlanguage", api.HandleDetectLanguage)
http.HandleFunc("/_ah/health", api.HealthCheckHandler)
http.Handle("/", http.FileServer(http.Dir(buildDir)))
// Load her up for app engine
appengine.Main()
// Load her up for dev
// log.Print("Listening on port 8080")
// log.Fatal(http.ListenAndServe(":8080", nil))
}