-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
162 lines (134 loc) · 4.05 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package http
import (
"fmt"
"github.com/SyncYomi/SyncYomi/internal/config"
"github.com/SyncYomi/SyncYomi/internal/database"
"github.com/SyncYomi/SyncYomi/internal/logger"
"github.com/SyncYomi/SyncYomi/web"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/gorilla/sessions"
"github.com/r3labs/sse/v2"
"github.com/rs/cors"
"github.com/rs/zerolog"
"net"
"net/http"
)
type Server struct {
log zerolog.Logger
sse *sse.Server
db *database.DB
config *config.AppConfig
cookieStore *sessions.CookieStore
version string
commit string
date string
apiService apikeyService
authService authService
notificationService notificationService
updateService updateService
deviceService deviceService
syncService syncService
mangaDataService mangaDataService
}
func NewServer(
log logger.Logger,
config *config.AppConfig,
sse *sse.Server,
db *database.DB,
version string,
commit string,
date string,
apiService apikeyService,
authService authService,
notificationSvc notificationService,
updateSvc updateService,
deviceService deviceService,
syncService syncService,
mangaDataService mangaDataService,
) Server {
return Server{
log: log.With().Str("module", "http").Logger(),
config: config,
sse: sse,
db: db,
version: version,
commit: commit,
date: date,
cookieStore: sessions.NewCookieStore([]byte(config.Config.SessionSecret)),
apiService: apiService,
authService: authService,
notificationService: notificationSvc,
updateService: updateSvc,
deviceService: deviceService,
syncService: syncService,
mangaDataService: mangaDataService,
}
}
func (s Server) Open() error {
addr := fmt.Sprintf("%v:%v", s.config.Config.Host, s.config.Config.Port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
server := http.Server{
Handler: s.Handler(),
}
s.log.Info().Msgf("Starting server. Listening on %s", listener.Addr().String())
return server.Serve(listener)
}
func (s Server) Handler() http.Handler {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
r.Use(LoggerMiddleware(&s.log))
c := cors.New(cors.Options{
AllowCredentials: true,
AllowedMethods: []string{"HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE"},
AllowOriginFunc: func(origin string) bool { return true },
OptionsPassthrough: true,
// Enable Debugging for testing, consider disabling in production
Debug: false,
})
r.Use(c.Handler)
encoder := encoder{}
web.RegisterHandler(r)
r.Route("/api", func(r chi.Router) {
r.Route("/auth", newAuthHandler(encoder, s.log, s.config.Config, s.cookieStore, s.authService).Routes)
r.Route("/healthz", newHealthHandler(encoder, s.db).Routes)
r.Group(func(r chi.Router) {
r.Use(s.IsAuthenticated)
r.Route("/config", newConfigHandler(encoder, s, s.config).Routes)
r.Route("/keys", newAPIKeyHandler(encoder, s.apiService).Routes)
r.Route("/logs", newLogsHandler(s.config).Routes)
r.Route("/notification", newNotificationHandler(encoder, s.notificationService).Routes)
r.Route("/updates", newUpdateHandler(encoder, s.updateService).Routes)
r.Route("/device", newDeviceHandler(encoder, s.deviceService).Routes)
r.Route("/sync", newSyncHandler(encoder, s.syncService).Routes)
r.Route("/manga", newMangaDataHandler(encoder, s.mangaDataService).Routes)
r.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
// inject CORS headers to bypass checks
s.sse.Headers = map[string]string{
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
}
s.sse.ServeHTTP(w, r)
})
})
// serve the parsed index.html
r.Get("/", s.index)
r.Get("/*", s.index)
})
return r
}
func (s Server) index(w http.ResponseWriter, r *http.Request) {
p := web.IndexParams{
Title: "Dashboard",
Version: s.version,
BaseUrl: s.config.Config.BaseURL,
}
web.Index(w, p)
}