udemy-go-web-1/internal/helpers/helpers.go

31 lines
726 B
Go
Raw Normal View History

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)
}
2024-07-19 16:40:19 +00:00
func IsAuthenticated(r *http.Request) bool {
return app.Session.Exists(r.Context(), "user_id")
}