-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
53 lines (45 loc) · 1.42 KB
/
server.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
func startDaemon() {
initLanguageChannels()
initLearnChannels()
r := mux.NewRouter()
r.HandleFunc("/tl/{langCode}/{word}", transliterationHandler).Methods("GET")
r.HandleFunc("/rtl/{langCode}/{word}", reverseTransliterationHandler).Methods("GET")
r.HandleFunc("/meta/{langCode}", metadataHandler).Methods("GET")
r.HandleFunc("/download/{langCode}/{downloadStart}", downloadHandler).Methods("GET")
r.HandleFunc("/learn", learnHandler).Methods("POST")
r.HandleFunc("/languages", languagesHandler).Methods("GET")
r.HandleFunc("/status", statusHandler).Methods("GET")
if enableInternalApis {
r.HandleFunc("/sync/download/{langCode}/enable", enableDownload).Methods("POST")
r.HandleFunc("/sync/download/{langCode}/disable", disableDownload).Methods("POST")
}
addUI(r)
address := fmt.Sprintf("%s:%d", host, port)
log.Printf("Listening on %s", address)
if enableSSL {
if err := http.ListenAndServeTLS(address, certFilePath, keyFilePath, recoverHandler(corsHandler(r))); err != nil {
log.Fatalln(err)
}
} else {
if err := http.ListenAndServe(address, recoverHandler(corsHandler(r))); err != nil {
log.Fatalln(err)
}
}
}
func addUI(r *mux.Router) {
if uiDir == "" {
return
}
if _, err := os.Stat(uiDir); err != nil {
log.Fatalln("UI path doesnot exist", err)
}
r.PathPrefix("/").Handler(http.FileServer(http.Dir(uiDir)))
}