udemy-go-web-1/internal/helpers/helpers.go
2024-07-19 18:40:19 +02:00

31 lines
726 B
Go

package helpers
import (
"fmt"
"go-udemy-web-1/internal/config"
"net/http"
"runtime/debug"
)
var app *config.AppConfig
// NewHelpers sets up app config for helpers
func NewHelpers(a *config.AppConfig) {
app = a
}
func ClientError(w http.ResponseWriter, status int) {
app.InfoLog.Println("Client error with status of", status)
http.Error(w, http.StatusText(status), status)
}
func ServerError(w http.ResponseWriter, err error) {
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
app.ErrorLog.Println(trace)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func IsAuthenticated(r *http.Request) bool {
return app.Session.Exists(r.Context(), "user_id")
}