52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package templ
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gorilla/csrf"
|
|
"github.com/zhang2092/go-url-shortener/internal/db"
|
|
"github.com/zhang2092/go-url-shortener/internal/middleware"
|
|
"github.com/zhang2092/go-url-shortener/internal/templ/auth"
|
|
"github.com/zhang2092/go-url-shortener/internal/templ/err"
|
|
"github.com/zhang2092/go-url-shortener/internal/templ/home"
|
|
"github.com/zhang2092/go-url-shortener/internal/templ/models"
|
|
"github.com/zhang2092/go-url-shortener/internal/templ/url"
|
|
)
|
|
|
|
func Login(w http.ResponseWriter, r *http.Request, form *models.LoginPageData) {
|
|
checkErr(w, auth.Login(r, form).Render(r.Context(), w))
|
|
}
|
|
|
|
func Register(w http.ResponseWriter, r *http.Request, form *models.RegisterPageData) {
|
|
checkErr(w, auth.Register(r, form).Render(r.Context(), w))
|
|
}
|
|
|
|
func Home(w http.ResponseWriter, r *http.Request, data []*db.UserRelateUrl) {
|
|
checkErr(w, home.Home(r, data).Render(r.Context(), w))
|
|
}
|
|
|
|
func CreateUrl(w http.ResponseWriter, r *http.Request, errorMsg string) {
|
|
checkErr(w, url.CreateUrl(r, errorMsg).Render(r.Context(), w))
|
|
}
|
|
|
|
func Error404(w http.ResponseWriter, r *http.Request) {
|
|
checkErr(w, err.Error404(r).Render(r.Context(), w))
|
|
}
|
|
|
|
func defaultData(r *http.Request, data map[string]any) map[string]any {
|
|
if data == nil {
|
|
data = make(map[string]any, 3)
|
|
}
|
|
|
|
data["AuthUser"] = middleware.GetUser(r.Context())
|
|
data["CsrfToken"] = csrf.Token(r)
|
|
data["CsrfTokenField"] = csrf.TemplateField(r)
|
|
return data
|
|
}
|
|
|
|
func checkErr(w http.ResponseWriter, err error) {
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|