Logging middleware added.
This commit is contained in:
52
cmd/start.go
52
cmd/start.go
@ -1,9 +1,24 @@
|
||||
/*
|
||||
Copyright © 2025 Wiki Cafe <mail@wiki.cafe>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.coopcloud.tech/wiki-cafe/member-console/internal/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
@ -27,8 +42,7 @@ var startCmd = &cobra.Command{
|
||||
|
||||
// Create a middleware stack
|
||||
stack := middleware.CreateStack(
|
||||
loggingMiddleware,
|
||||
authMiddleware,
|
||||
middleware.Logging,
|
||||
)
|
||||
|
||||
server := http.Server{
|
||||
@ -36,9 +50,6 @@ var startCmd = &cobra.Command{
|
||||
Handler: stack(httpRequestRouter),
|
||||
}
|
||||
|
||||
// Register the login page handler
|
||||
httpRequestRouter.HandleFunc("/login", serveLoginPage)
|
||||
|
||||
// Serve the components directory
|
||||
httpRequestRouter.Handle("/", http.FileServer(http.Dir("./components")))
|
||||
log.Println("Starting server on port", port)
|
||||
@ -59,32 +70,3 @@ func init() {
|
||||
// Add the command to the root command
|
||||
rootCmd.AddCommand(startCmd)
|
||||
}
|
||||
|
||||
// authMiddleware is a simple middleware function that checks for a valid session cookie
|
||||
func authMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check for the session cookie
|
||||
cookie, err := r.Cookie("session")
|
||||
if err != nil || cookie.Value != "authenticated" {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
// Call the next handler
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// loggerMiddleware is a simple middleware function that logs the request method and URL
|
||||
func loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
log.Println(r.Method, r.URL.Path, time.Since(start))
|
||||
})
|
||||
}
|
||||
|
||||
// serveLoginPage is a simple handler function that serves the login page
|
||||
func serveLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./components/login.html")
|
||||
}
|
||||
|
Reference in New Issue
Block a user