-
Notifications
You must be signed in to change notification settings - Fork 0
/
shenmeci.go
48 lines (41 loc) · 1020 Bytes
/
shenmeci.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
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net"
"os"
"github.com/rhcarvalho/shenmeci/internal/segmentation"
"github.com/rhcarvalho/shenmeci/internal/shenmeci"
)
//go:embed static
var static embed.FS
func main() {
shenmeci.LoadConfig()
shenmeci.ValidateConfig()
config := shenmeci.GlobalConfig
static, err := fs.Sub(static, "static")
if err != nil {
panic(err)
}
// Test whether we can listen on the provided Host and Port.
// If the Host:Port is already in use, we can exit before wasting more resources.
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.Http.Host, config.Http.Port))
if err != nil {
if err.(*net.OpError).Err.Error() == "address already in use" {
os.Exit(0)
}
log.Fatal(err)
}
ln.Close()
vocabulary := shenmeci.LoadCEDICT()
shenmeci.LoadDB()
defer shenmeci.CloseDB()
shenmeci.Serve(&shenmeci.HTTPConfig{
Host: config.Http.Host,
Port: config.Http.Port,
Static: static,
Segmenter: segmentation.NewSegmenter(vocabulary),
})
}