refactor: changed file structure
This commit is contained in:
70
internal/httpserver/routes.go
Normal file
70
internal/httpserver/routes.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"projectreshoot/internal/handler"
|
||||
"projectreshoot/internal/middleware"
|
||||
"projectreshoot/internal/view/page"
|
||||
"projectreshoot/pkg/config"
|
||||
"projectreshoot/pkg/db"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// Add all the handled routes to the mux
|
||||
func addRoutes(
|
||||
mux *http.ServeMux,
|
||||
logger *zerolog.Logger,
|
||||
config *config.Config,
|
||||
conn *db.SafeConn,
|
||||
staticFS *http.FileSystem,
|
||||
) {
|
||||
route := mux.Handle
|
||||
loggedIn := middleware.LoginReq
|
||||
loggedOut := middleware.LogoutReq
|
||||
fresh := middleware.FreshReq
|
||||
|
||||
// Health check
|
||||
mux.HandleFunc("GET /healthz", func(http.ResponseWriter, *http.Request) {})
|
||||
|
||||
// Static files
|
||||
route("GET /static/", http.StripPrefix("/static/", handler.StaticFS(staticFS)))
|
||||
|
||||
// Index page and unhandled catchall (404)
|
||||
route("GET /", handler.Root())
|
||||
|
||||
// Static content, unprotected pages
|
||||
route("GET /about", handler.HandlePage(page.About()))
|
||||
|
||||
// Login page and handlers
|
||||
route("GET /login", loggedOut(handler.LoginPage(config.TrustedHost)))
|
||||
route("POST /login", loggedOut(handler.LoginRequest(config, logger, conn)))
|
||||
|
||||
// Register page and handlers
|
||||
route("GET /register", loggedOut(handler.RegisterPage(config.TrustedHost)))
|
||||
route("POST /register", loggedOut(handler.RegisterRequest(config, logger, conn)))
|
||||
|
||||
// Logout
|
||||
route("POST /logout", handler.Logout(config, logger, conn))
|
||||
|
||||
// Reauthentication request
|
||||
route("POST /reauthenticate", loggedIn(handler.Reauthenticate(logger, config, conn)))
|
||||
|
||||
// Profile page
|
||||
route("GET /profile", loggedIn(handler.ProfilePage()))
|
||||
|
||||
// Account page
|
||||
route("GET /account", loggedIn(handler.AccountPage()))
|
||||
route("POST /account-select-page", loggedIn(handler.AccountSubpage()))
|
||||
route("POST /change-username", loggedIn(fresh(handler.ChangeUsername(logger, conn))))
|
||||
route("POST /change-bio", loggedIn(handler.ChangeBio(logger, conn)))
|
||||
route("POST /change-password", loggedIn(fresh(handler.ChangePassword(logger, conn))))
|
||||
|
||||
// Movies Search
|
||||
route("GET /movies", handler.MoviesPage())
|
||||
route("POST /search-movies", handler.SearchMovies(logger, config))
|
||||
|
||||
// Movie page
|
||||
route("GET /movie/{movie_id}", handler.Movie(logger, config))
|
||||
}
|
||||
63
internal/httpserver/server.go
Normal file
63
internal/httpserver/server.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"projectreshoot/internal/middleware"
|
||||
"projectreshoot/pkg/config"
|
||||
"projectreshoot/pkg/db"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func NewServer(
|
||||
config *config.Config,
|
||||
logger *zerolog.Logger,
|
||||
conn *db.SafeConn,
|
||||
staticFS *fs.FS,
|
||||
maint *uint32,
|
||||
) *http.Server {
|
||||
fs := http.FS(*staticFS)
|
||||
srv := createServer(config, logger, conn, &fs, maint)
|
||||
httpServer := &http.Server{
|
||||
Addr: net.JoinHostPort(config.Host, config.Port),
|
||||
Handler: srv,
|
||||
ReadHeaderTimeout: config.ReadHeaderTimeout * time.Second,
|
||||
WriteTimeout: config.WriteTimeout * time.Second,
|
||||
IdleTimeout: config.IdleTimeout * time.Second,
|
||||
}
|
||||
return httpServer
|
||||
}
|
||||
|
||||
// Returns a new http.Handler with all the routes and middleware added
|
||||
func createServer(
|
||||
config *config.Config,
|
||||
logger *zerolog.Logger,
|
||||
conn *db.SafeConn,
|
||||
staticFS *http.FileSystem,
|
||||
maint *uint32,
|
||||
) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
addRoutes(
|
||||
mux,
|
||||
logger,
|
||||
config,
|
||||
conn,
|
||||
staticFS,
|
||||
)
|
||||
var handler http.Handler = mux
|
||||
// Add middleware here, must be added in reverse order of execution
|
||||
// i.e. First in list will get executed last during the request handling
|
||||
handler = middleware.Logging(logger, handler)
|
||||
handler = middleware.Authentication(logger, config, conn, handler, maint)
|
||||
|
||||
// Gzip
|
||||
handler = middleware.Gzip(handler, config.GZIP)
|
||||
|
||||
// Start the timer for the request chain so logger can have accurate info
|
||||
handler = middleware.StartTimer(handler)
|
||||
return handler
|
||||
}
|
||||
Reference in New Issue
Block a user