autobrr/internal/http/server.go
KaiserBh edae1bbf4b
refactor(web): migrate create-react-app to vite (#787)
* removed react-app type instead use vite.

* removed index.html from public since vite uses it from root: read more: https://vitejs.dev/guide/#index-html-and-project-root

* yarn.lock update.

* added vite config file. With commented rollUp option if we want the build to be called build but using default stuff for now.

* updated tsconfig to use vite and include vite.config.ts

* changed package json build commands to use vite.

* for some reason there is an error in vite config when we put project as tsconfig.json.

* build.go updated to use the new dist folder.

* refactored as well updated to use dist and web.AssetHandler again.

* Fixed issue forcing the frontend to be reloaded for all routes to work if logged in fresh without reloading it will always go back to dashboard.

* updated it to use the new function; need to fix the Index for baseUrl I believe, if enabled it works except logs route will crash due to cors.

* refactored and default port to 7474, don't think we need the rollUpOptions.

* added tmp/ to ignore .

* init air.toml, for dev hot reloading both app and backend. https://github.com/cosmtrek/air run it using air but make sure it's in PATH

* updated the start command to build and watch for changes, works great with air.

* revert

* added proxy for vite config. To be used for dev.

* refactor: I think this should fix it, when logs route etc getting accessed usually it throws error but by getting rid of the catch-all it should work as intended, since web.RegisterHandler(r) will catch the unmatched ones.

* fix: baseurl and build

* fix(build): docker ignore !web/dist

* fix(build): dockerignore add exclusions

* docs: update README.md

* build: update postcss config

---------

Co-authored-by: KaiserBh <kaiserbh@proton.me>
Co-authored-by: ze0s <ze0s@riseup.net>
2023-04-07 16:04:10 +02:00

159 lines
4.8 KiB
Go

package http
import (
"fmt"
"net"
"net/http"
"github.com/autobrr/autobrr/internal/config"
"github.com/autobrr/autobrr/internal/database"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/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"
)
type Server struct {
log zerolog.Logger
sse *sse.Server
db *database.DB
config *config.AppConfig
cookieStore *sessions.CookieStore
version string
commit string
date string
actionService actionService
apiService apikeyService
authService authService
downloadClientService downloadClientService
filterService filterService
feedService feedService
indexerService indexerService
ircService ircService
notificationService notificationService
releaseService releaseService
updateService updateService
}
func NewServer(log logger.Logger, config *config.AppConfig, sse *sse.Server, db *database.DB, version string, commit string, date string, actionService actionService, apiService apikeyService, authService authService, downloadClientSvc downloadClientService, filterSvc filterService, feedSvc feedService, indexerSvc indexerService, ircSvc ircService, notificationSvc notificationService, releaseSvc releaseService, updateSvc updateService) 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)),
actionService: actionService,
apiService: apiService,
authService: authService,
downloadClientService: downloadClientSvc,
filterService: filterSvc,
feedService: feedSvc,
indexerService: indexerSvc,
ircService: ircSvc,
notificationService: notificationSvc,
releaseService: releaseSvc,
updateService: updateSvc,
}
}
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("/actions", newActionHandler(encoder, s.actionService).Routes)
r.Route("/config", newConfigHandler(encoder, s, s.config).Routes)
r.Route("/download_clients", newDownloadClientHandler(encoder, s.downloadClientService).Routes)
r.Route("/filters", newFilterHandler(encoder, s.filterService).Routes)
r.Route("/feeds", newFeedHandler(encoder, s.feedService).Routes)
r.Route("/irc", newIrcHandler(encoder, s.ircService).Routes)
r.Route("/indexer", newIndexerHandler(encoder, s.indexerService, s.ircService).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("/release", newReleaseHandler(encoder, s.releaseService).Routes)
r.Route("/updates", newUpdateHandler(encoder, s.updateService).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)
}