-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (115 loc) · 2.96 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:generate bash -c "go generate ./ent"
//go:generate bash -c "mkdir -p oapi && go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=oapi.yaml api/openapi.yaml"
package main
import (
"context"
_ "embed"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"github.com/getkin/kin-openapi/openapi3"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/nuomizi-fw/stargazer/core"
"github.com/nuomizi-fw/stargazer/oapi"
"github.com/nuomizi-fw/stargazer/repository"
"github.com/nuomizi-fw/stargazer/router"
"github.com/nuomizi-fw/stargazer/service"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
)
var (
//go:embed api/openapi.yaml
docYAML string
//go:embed api/index.html
docHTML string
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
kill := make(chan os.Signal, 1)
signal.Notify(kill, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-kill
cancel()
}()
app := fx.New(
fx.WithLogger(func() fxevent.Logger {
logger := core.NewStargazerLogger()
return logger.GetFxLogger()
}),
core.Module,
router.Module,
service.Module,
repository.Module,
fx.Invoke(StartStargazer),
)
if err := app.Start(ctx); err != nil {
if err != context.Canceled {
log.Fatalf("Failed to start app: %s", err)
}
}
<-ctx.Done()
}
func StartStargazer(
lc fx.Lifecycle,
config core.StargazerConfig,
db core.StargazerDB,
logger core.StargazerLogger,
server core.StargazerServer,
router router.StargazerRouter,
) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
if config.Database.Migrate {
logger.Info("Migrating database...")
if err := db.Schema.Create(ctx); err != nil {
logger.Panic("Failed to migrate database: %s", zap.Error(err))
}
}
swagger, err := oapi.GetSwagger()
if err != nil {
logger.Panic("Failed to get swagger: %s", zap.Error(err))
}
server.App.Use(adaptor.HTTPHandler(NewDocsRouter(swagger, docHTML, docYAML)))
go func() {
if config.Server.TLS.Enabled {
logger.Fatal(server.App.ListenTLS(config.Server.Port, config.Server.TLS.CertFile, config.Server.TLS.KeyFile))
} else {
logger.Fatal(server.App.Listen(config.Server.Port))
}
}()
return nil
},
OnStop: func(ctx context.Context) error {
if err := server.App.Shutdown(); err != nil {
return err
}
return nil
},
})
}
func NewDocsRouter(swagger *openapi3.T, docHTML, docYAML string) http.Handler {
u, err := url.Parse(swagger.Servers[0].URL)
if err != nil {
return nil
}
apiPath := strings.TrimRight(u.Path, "/")
docsPath := "/docs" + apiPath
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == docsPath {
if _, err := w.Write([]byte(docHTML)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
return
}
if r.URL.Path == "/openapi.yaml" {
if _, err := w.Write([]byte(docYAML)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
})
}