-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (63 loc) · 1.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"github.com/mattn/go-skkdic"
"github.com/ogen-go/ogen/middleware"
oas "github.com/Cj-bc/skkishoe/internal/oas"
)
// Insert raw *http.Request pointer to context
func StoreRawRequestMiddleware(req middleware.Request, next middleware.Next) (middleware.Response, error) {
req.SetContext(context.WithValue(req.Context, "rawRequest", req.Raw))
return next(req)
}
func usage() {
fmt.Fprintf(os.Stderr, `skkishoe usage:
skkishoe [FLAGS] DICTIONARY... # Start skkishoe server using DICTIONARY
Flags:
`)
flag.PrintDefaults()
os.Exit(0)
}
func main() {
port := flag.Int("port", 8080, "server port number")
flag.Parse()
if flag.NArg() <= 0 {
usage()
}
slog.Info("Setting up dictionaries", "dictionaries", flag.Args())
dict := skkdic.New()
var err error
for _, d := range flag.Args() {
f, err := os.Open(d)
defer f.Close()
if err != nil {
slog.Warn("Failed to open dictionary", "dictionary", d, "error", err)
}
if err = dict.Load(f); err != nil {
slog.Warn("Get error while reading dictionary file", "dictionary", d, "error", err)
} else {
slog.Info(fmt.Sprintf("dictionary loaded: %s", d))
}
}
if err != nil {
slog.Error("Unknown error occured", "error", err.Error())
os.Exit(1)
}
service := MidashiService{dict}
slog.Info("Setting up Server")
srv, err := oas.NewServer(service, oas.WithMiddleware(StoreRawRequestMiddleware))
if err != nil {
slog.Error("Unknown error occured", "error", err.Error())
os.Exit(1)
}
slog.Info("Server is ready. Start listening", "port", *port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), srv); err != nil {
slog.Error("Unknown error occured", "error", err.Error())
}
os.Exit(1)
}