-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (84 loc) · 2.18 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Package main is the start point for text2anki
package main
import (
"context"
"net"
"net/http"
"os"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/s12chung/text2anki/db/pkg/db"
"github.com/s12chung/text2anki/pkg/api"
"github.com/s12chung/text2anki/pkg/api/config"
"github.com/s12chung/text2anki/pkg/util/logg"
)
const host = "http://localhost"
const port = "3000"
var plog = logg.Default() //nolint:forbidigo // main package
func configFromEnv() config.Config {
c := config.Config{
Log: plog,
TxPool: api.TxPool{},
StorageConfig: config.StorageConfig{
StorageType: config.StorageLocalStore,
LocalStoreConfig: config.LocalStoreConfig{
Origin: host + ":" + port,
KeyBasePath: "db/tmp/filestore",
EncryptorPath: "db/tmp",
},
},
}
if os.Getenv("TOKENIZER") == "komoran" {
c.TokenizerType = config.TokenizerKomoran
}
if os.Getenv("DICTIONARY") == "koreanbasic" {
c.DictionaryType = config.DictionaryKoreanBasic
}
return c
}
func main() {
if err := run(); err != nil {
plog.Error("main", logg.Err(err))
os.Exit(-1)
}
}
func run() error {
if err := db.SetDB("db/data.sqlite3"); err != nil {
return err
}
ctx := context.Background() //nolint:forbidigo // this is main
routes := api.NewRoutes(ctx, configFromEnv())
if err := routes.Setup(ctx); err != nil {
return err
}
defer func() {
if err := routes.Cleanup(); err != nil {
plog.Error("main routes.Cleanup()", logg.Err(err))
}
}()
r := chi.NewRouter()
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"http://localhost:5173"},
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"},
AllowedHeaders: []string{"Content-Type"},
ExposedHeaders: []string{},
AllowCredentials: false,
MaxAge: 86400,
}))
r.Use(middleware.Logger)
r.Use(middleware.Heartbeat("/healthz"))
r.Mount("/", routes.Router())
server := http.Server{
Addr: ":" + port,
Handler: r,
ReadHeaderTimeout: time.Second,
}
ln, err := net.Listen("tcp", server.Addr)
if err != nil {
return err
}
plog.Info("Server running on " + host + server.Addr)
return server.Serve(ln)
}