mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
refac: separated api content into sub-packages & renamed usecases
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
package api
|
||||
package auth
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/domain/user"
|
||||
"GoCMS/useCases"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RegisterCredentials struct {
|
||||
@@ -25,14 +27,14 @@ type LoginCredentials struct {
|
||||
}
|
||||
|
||||
var shouldCookieBeSecure = os.Getenv("ENVIRONMENT") == "production"
|
||||
var TokenAuth *jwtauth.JWTAuth
|
||||
var Token *jwtauth.JWTAuth
|
||||
|
||||
// TODO Move into its own file or package that handles api errors
|
||||
const logsErrorMessage = "Access to the requested resource is forbidden due to incorrect password and/or username."
|
||||
const bodyErrorMessage = "The request cannot be processed due to a mismatch in the format of the body."
|
||||
const LogsErrorMessage = "Access to the requested resource is forbidden due to incorrect password and/or username."
|
||||
const BodyErrorMessage = "The request cannot be processed due to a mismatch in the format of the body."
|
||||
|
||||
func SetJwtCookie(w *http.ResponseWriter, userId uint32) error {
|
||||
_, tokenString, err := TokenAuth.Encode(map[string]interface{}{"user_id": userId})
|
||||
_, tokenString, err := Token.Encode(map[string]interface{}{"user_id": userId})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -48,7 +50,7 @@ func SetJwtCookie(w *http.ResponseWriter, userId uint32) error {
|
||||
}
|
||||
|
||||
func SomeUsersVerified() bool {
|
||||
users := Container.ListUsersUseCase.ListUsers()
|
||||
users := api.Container.ListUsersUseCase.ListUsers()
|
||||
for _, localUser := range users {
|
||||
if localUser.IsVerified {
|
||||
return true
|
||||
@@ -58,13 +60,13 @@ func SomeUsersVerified() bool {
|
||||
}
|
||||
|
||||
func IsUserTableEmpty() bool {
|
||||
users := Container.ListUsersUseCase.ListUsers()
|
||||
users := api.Container.ListUsersUseCase.ListUsers()
|
||||
return len(users) == 0
|
||||
}
|
||||
|
||||
func IsLoggedIn(r *http.Request) bool {
|
||||
token, err := jwtauth.VerifyRequest(
|
||||
TokenAuth,
|
||||
Token,
|
||||
r,
|
||||
jwtauth.TokenFromCookie,
|
||||
jwtauth.TokenFromHeader,
|
||||
@@ -74,7 +76,7 @@ func IsLoggedIn(r *http.Request) bool {
|
||||
|
||||
func IsVerified(r *http.Request) bool {
|
||||
token, err := jwtauth.VerifyRequest(
|
||||
TokenAuth,
|
||||
Token,
|
||||
r,
|
||||
jwtauth.TokenFromCookie,
|
||||
jwtauth.TokenFromHeader,
|
||||
@@ -83,12 +85,12 @@ func IsVerified(r *http.Request) bool {
|
||||
return false
|
||||
}
|
||||
userId := token.PrivateClaims()["user_id"].(float64)
|
||||
currentUser, _ := Container.GetUserUseCase.GetUser(uint32(userId))
|
||||
currentUser, _ := api.Container.GetUserUseCase.GetUser(uint32(userId))
|
||||
return currentUser.IsVerified
|
||||
}
|
||||
|
||||
func getUserFromCredentials(credentials LoginCredentials) (user.User, error) {
|
||||
dbUser, err := Container.GetUserUseCase.GetUserByUsername(credentials.Username)
|
||||
func GetUserFromCredentials(credentials LoginCredentials) (user.User, error) {
|
||||
dbUser, err := api.Container.GetUserUseCase.GetUserByUsername(credentials.Username)
|
||||
if err != nil {
|
||||
return user.User{}, err
|
||||
}
|
||||
@@ -101,8 +103,8 @@ func getUserFromCredentials(credentials LoginCredentials) (user.User, error) {
|
||||
return dbUser, nil
|
||||
}
|
||||
|
||||
func getNewUser(newUserCredentials RegisterCredentials, verificationCode string) (user.User, error) {
|
||||
createdUser, err := Container.CreateUserUseCase.CreateUser(useCases.CreateUserCommand{
|
||||
func GetNewUser(newUserCredentials RegisterCredentials, verificationCode string) (user.User, error) {
|
||||
createdUser, err := api.Container.CreateUserUseCase.CreateUser(useCases.CreateUserCommand{
|
||||
Username: newUserCredentials.Username,
|
||||
Password: newUserCredentials.Password,
|
||||
Email: newUserCredentials.Email,
|
||||
@@ -119,19 +121,19 @@ func login(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&credentials)
|
||||
if err != nil {
|
||||
http.Error(w, bodyErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = validate.Struct(credentials)
|
||||
err = api.Validate.Struct(credentials)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
dbUser, err := getUserFromCredentials(credentials)
|
||||
dbUser, err := GetUserFromCredentials(credentials)
|
||||
if err != nil {
|
||||
http.Error(w, logsErrorMessage, http.StatusForbidden)
|
||||
http.Error(w, LogsErrorMessage, http.StatusForbidden)
|
||||
}
|
||||
|
||||
_ = SetJwtCookie(&w, dbUser.ID)
|
||||
@@ -149,18 +151,18 @@ func register(w http.ResponseWriter, r *http.Request) {
|
||||
var credentials RegisterCredentials
|
||||
err := json.NewDecoder(r.Body).Decode(&credentials)
|
||||
if err != nil {
|
||||
http.Error(w, bodyErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = validate.Struct(credentials)
|
||||
err = api.Validate.Struct(credentials)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
verificationCode := uuid.NewString()
|
||||
createdUser, err := getNewUser(credentials, verificationCode)
|
||||
createdUser, err := GetNewUser(credentials, verificationCode)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -1,13 +1,15 @@
|
||||
package api
|
||||
package image
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func postImage(w http.ResponseWriter, r *http.Request) {
|
||||
func PostImage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
|
||||
id := chi.URLParam(r, "id")
|
||||
@@ -19,13 +21,13 @@ func postImage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
newImage, err := Container.CreateImageUseCase.CreateImage(file, *fileHeader)
|
||||
newImage, err := api.Container.CreateImageUseCase.CreateImage(file, *fileHeader)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = Container.UpdatePostUseCase.AddImage(uint32(idInt), newImage.ID)
|
||||
err = api.Container.UpdatePostUseCase.AddImage(uint32(idInt), newImage.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -1,13 +1,14 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetHomePage(w http.ResponseWriter, _ *http.Request) {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
homeTmpl, _ := Container.GetPageUseCase.GetPage("home", map[string]interface{}{
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
homeTmpl, _ := api.Container.GetPageUseCase.GetPage("home", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
})
|
||||
@@ -1,14 +1,15 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func GetPageIntegration(w http.ResponseWriter, r *http.Request) {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
templ, _ := Container.GetPageUseCase.GetPage("integration", map[string]interface{}{
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
templ, _ := api.Container.GetPageUseCase.GetPage("integration", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Host": os.Getenv("HOST"),
|
||||
@@ -1,6 +1,8 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/auth"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
@@ -19,7 +21,7 @@ var EmptyLoginPage = &LoginPage{
|
||||
|
||||
func GetLoginPageHandler(loginPage *LoginPage) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if IsLoggedIn(r) {
|
||||
if auth.IsLoggedIn(r) {
|
||||
http.Redirect(w, r, "/home", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -27,13 +29,13 @@ func GetLoginPageHandler(loginPage *LoginPage) http.HandlerFunc {
|
||||
PostLoginPage(w, r)
|
||||
return
|
||||
}
|
||||
if IsUserTableEmpty() {
|
||||
if auth.IsUserTableEmpty() {
|
||||
http.Redirect(w, r, "/register", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
success, _ := url.QueryUnescape(r.URL.Query().Get("success"))
|
||||
failure, _ := url.QueryUnescape(r.URL.Query().Get("failure"))
|
||||
bs, _ := Container.GetPageUseCase.GetPage("login", map[string]interface{}{
|
||||
bs, _ := api.Container.GetPageUseCase.GetPage("login", map[string]interface{}{
|
||||
"PageError": loginPage.PageError,
|
||||
"Username": loginPage.Username,
|
||||
"Head": headTmpl,
|
||||
@@ -47,11 +49,11 @@ func GetLoginPageHandler(loginPage *LoginPage) http.HandlerFunc {
|
||||
func PostLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
|
||||
credentials := LoginCredentials{
|
||||
credentials := auth.LoginCredentials{
|
||||
Username: r.FormValue("username"),
|
||||
Password: r.FormValue("password"),
|
||||
}
|
||||
err := validate.Struct(credentials)
|
||||
err := api.Validate.Struct(credentials)
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
GetLoginPageHandler(&LoginPage{
|
||||
@@ -61,7 +63,7 @@ func PostLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
dbUser, err := getUserFromCredentials(credentials)
|
||||
dbUser, err := auth.GetUserFromCredentials(credentials)
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
GetLoginPageHandler(&LoginPage{
|
||||
@@ -71,7 +73,7 @@ func PostLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
_ = SetJwtCookie(&w, dbUser.ID)
|
||||
_ = auth.SetJwtCookie(&w, dbUser.ID)
|
||||
|
||||
http.Redirect(w, r, "/home", http.StatusSeeOther)
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/auth"
|
||||
"GoCMS/api/controllers/image"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
var headTmpl template.HTML
|
||||
@@ -35,7 +39,7 @@ func NewPageError(message string) *PageError {
|
||||
|
||||
func IsLoggedInMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !IsLoggedIn(r) {
|
||||
if !auth.IsLoggedIn(r) {
|
||||
http.Redirect(w, r, LoginRoute, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -45,7 +49,7 @@ func IsLoggedInMiddleware(next http.Handler) http.Handler {
|
||||
|
||||
func IsVerifiedMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !IsVerified(r) {
|
||||
if !auth.IsVerified(r) {
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -55,7 +59,7 @@ func IsVerifiedMiddleware(next http.Handler) http.Handler {
|
||||
|
||||
func IsNotVerifiedMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if IsVerified(r) {
|
||||
if auth.IsVerified(r) {
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -68,7 +72,7 @@ func GetLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func GetLogout(w http.ResponseWriter, r *http.Request) {
|
||||
RemoveJwtCookie(w)
|
||||
auth.RemoveJwtCookie(w)
|
||||
http.Redirect(w, r, LoginRoute, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -92,7 +96,7 @@ func StaticFileServerWithContentType(fsys http.FileSystem) http.Handler {
|
||||
}
|
||||
|
||||
func InitHeadTmpl() {
|
||||
headTmplHtml, _ := Container.GetPageUseCase.GetPage("utilsHead", nil)
|
||||
headTmplHtml, _ := api.Container.GetPageUseCase.GetPage("utilsHead", nil)
|
||||
headTmpl = template.HTML(headTmplHtml)
|
||||
}
|
||||
|
||||
@@ -146,7 +150,7 @@ func NewPageRouter() http.Handler {
|
||||
r.Get("/post/create", GetPostCreatePage)
|
||||
r.Post("/post/create", PostPostCreatePage)
|
||||
|
||||
r.Post("/post/{id}/image/create", postImage)
|
||||
r.Post("/post/{id}/image/create", image.PostImage)
|
||||
|
||||
r.Get("/post/{id}/publish", GetPostPublishPage)
|
||||
r.Get("/post/{id}/unpublish", GetPostUnpublishPage)
|
||||
@@ -1,9 +1,11 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"GoCMS/api"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PasswordResetRequest struct {
|
||||
@@ -16,7 +18,7 @@ func GetPasswordResetRequest(w http.ResponseWriter, r *http.Request) {
|
||||
success := r.URL.Query().Get("success")
|
||||
email := r.URL.Query().Get("email")
|
||||
|
||||
bs, _ := Container.GetPageUseCase.GetPage("passwordResetRequest", map[string]interface{}{
|
||||
bs, _ := api.Container.GetPageUseCase.GetPage("passwordResetRequest", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
"Email": email,
|
||||
"Success": success,
|
||||
@@ -31,22 +33,22 @@ func PostPasswordResetRequest(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
var getRedirectUrl = "/register/reset/request?success=" + successMessage + "&email=" + passResetReq.Email
|
||||
|
||||
err := validate.Struct(passResetReq)
|
||||
err := api.Validate.Struct(passResetReq)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, getRedirectUrl, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
fetchedUser, err := Container.GetUserUseCase.GetUserByEmail(passResetReq.Email)
|
||||
fetchedUser, err := api.Container.GetUserUseCase.GetUserByEmail(passResetReq.Email)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, getRedirectUrl, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
verificationCode := uuid.NewString()
|
||||
updatedUser, _ := Container.UpdateUserUseCase.UpdatePasswordResetCode(fetchedUser.ID, verificationCode)
|
||||
updatedUser, _ := api.Container.UpdateUserUseCase.UpdatePasswordResetCode(fetchedUser.ID, verificationCode)
|
||||
|
||||
_ = Container.SendMailUseCase.SendMail(updatedUser.Email, "passwordReset", map[string]string{
|
||||
_ = api.Container.SendMailUseCase.SendMail(updatedUser.Email, "passwordReset", map[string]string{
|
||||
"Host": os.Getenv("HOST"),
|
||||
"VerificationCode": verificationCode,
|
||||
"Email": updatedUser.Email,
|
||||
@@ -1,8 +1,10 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"GoCMS/api"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var PasswordLinkErrorMessage = "The reset password link you used is invalid."
|
||||
@@ -14,7 +16,7 @@ func GetPasswordResetValidate(w http.ResponseWriter, r *http.Request) {
|
||||
if failure != "" {
|
||||
pageError = NewPageError(failure)
|
||||
}
|
||||
template, _ := Container.GetPageUseCase.GetPage("passwordResetValidate", map[string]interface{}{
|
||||
template, _ := api.Container.GetPageUseCase.GetPage("passwordResetValidate", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
"Error": pageError,
|
||||
"Email": r.URL.Query().Get("email"),
|
||||
@@ -36,7 +38,7 @@ func PostPasswordResetValidate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
fetchedUser, err := Container.GetUserUseCase.GetUserByEmail(email)
|
||||
fetchedUser, err := api.Container.GetUserUseCase.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, redirectionErrorLink+"&failure="+PasswordLinkErrorMessage, http.StatusSeeOther)
|
||||
return
|
||||
@@ -48,8 +50,8 @@ func PostPasswordResetValidate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = Container.UpdateUserUseCase.UpdatePassword(fetchedUser.ID, password)
|
||||
_, _ = Container.UpdateUserUseCase.UpdatePasswordResetCode(fetchedUser.ID, "")
|
||||
_, _ = api.Container.UpdateUserUseCase.UpdatePassword(fetchedUser.ID, password)
|
||||
_, _ = api.Container.UpdateUserUseCase.UpdatePasswordResetCode(fetchedUser.ID, "")
|
||||
|
||||
http.Redirect(w, r, "/login?success="+PasswordLinkSuccessMessage, http.StatusSeeOther)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/useCases"
|
||||
"html/template"
|
||||
"net/http"
|
||||
@@ -14,8 +15,8 @@ type PostCreatePageError struct {
|
||||
}
|
||||
|
||||
func GetPostCreatePageTemplate(postName string, errorMessage string) ([]byte, error) {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
return Container.GetPageUseCase.GetPage("postCreate", map[string]interface{}{
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
return api.Container.GetPageUseCase.GetPage("postCreate", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"PageError": PostCreatePageError{
|
||||
@@ -38,7 +39,7 @@ func PostPostCreatePage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
post, err := Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
|
||||
post, err := api.Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
|
||||
Title: postName,
|
||||
Body: "",
|
||||
})
|
||||
@@ -1,20 +1,23 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/post"
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||
if err != nil {
|
||||
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, post.IdUint32ErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
localPost, err := Container.GetPostUseCase.GetPost(uint32(id))
|
||||
localPost, err := api.Container.GetPostUseCase.GetPost(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
@@ -22,14 +25,14 @@ func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
fmt.Println(localPost.Images)
|
||||
for _, image := range localPost.Images {
|
||||
err = Container.DeleteImageUseCase.DeleteImage(image.ID)
|
||||
err = api.Container.DeleteImageUseCase.DeleteImage(image.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
err = api.Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(400), http.StatusBadRequest)
|
||||
return
|
||||
@@ -1,12 +1,14 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/domain/post"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PostEditPageAlert struct {
|
||||
@@ -15,8 +17,8 @@ type PostEditPageAlert struct {
|
||||
}
|
||||
|
||||
func getPostEditPageTemplate(post post.Post, alert PostEditPageAlert) []byte {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postTmpl, _ := Container.GetPageUseCase.GetPage("postEdit", map[string]interface{}{
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postTmpl, _ := api.Container.GetPageUseCase.GetPage("postEdit", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Post": post,
|
||||
@@ -40,8 +42,8 @@ func PostPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
postBody := r.FormValue("postBody")
|
||||
|
||||
getPost, _ := Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
updatedPost, err := Container.UpdatePostUseCase.UpdateBody(getPost.ID, postBody)
|
||||
getPost, _ := api.Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
updatedPost, err := api.Container.UpdatePostUseCase.UpdateBody(getPost.ID, postBody)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getPostEditPageTemplate(post.Post{Body: postBody}, PostEditPageAlert{
|
||||
IsError: true,
|
||||
@@ -64,7 +66,7 @@ func GetPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
getPost, _ := Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
getPost, _ := api.Container.GetPostUseCase.GetPost(uint32(postIDint))
|
||||
|
||||
_, _ = w.Write(getPostEditPageTemplate(getPost, PostEditPageAlert{
|
||||
IsError: false,
|
||||
@@ -1,10 +1,12 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"errors"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func updateIsOnline(postId string, isOnline bool) (error, int) {
|
||||
@@ -13,7 +15,7 @@ func updateIsOnline(postId string, isOnline bool) (error, int) {
|
||||
return errors.New("the server expects the ID to be in the format of an unsigned 32-bit integer (uint32)"), http.StatusBadRequest
|
||||
}
|
||||
|
||||
_, err = Container.UpdatePostUseCase.UpdateIsOnline(uint32(postIdInt), isOnline)
|
||||
_, err = api.Container.UpdatePostUseCase.UpdateIsOnline(uint32(postIdInt), isOnline)
|
||||
if err != nil {
|
||||
return errors.New("the requested resource, identified by its unique ID, could not be found on the server"), http.StatusNotFound
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetPostsPage(w http.ResponseWriter, _ *http.Request) {
|
||||
posts := Container.ListPostsUseCase.ListPosts()
|
||||
posts := api.Container.ListPostsUseCase.ListPosts()
|
||||
|
||||
var formattedPosts []map[string]interface{}
|
||||
for _, post := range posts {
|
||||
@@ -19,8 +20,8 @@ func GetPostsPage(w http.ResponseWriter, _ *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postsTmpl, _ := Container.GetPageUseCase.GetPage("posts", map[string]interface{}{
|
||||
navbarTmpl, _ := api.Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postsTmpl, _ := api.Container.GetPageUseCase.GetPage("posts", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Posts": formattedPosts,
|
||||
@@ -1,9 +1,12 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/auth"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RegisterPageError struct {
|
||||
@@ -30,10 +33,10 @@ var EmptyRegisterPage = &RegisterPage{
|
||||
|
||||
func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if !IsLoggedIn(r) && SomeUsersVerified() {
|
||||
if !auth.IsLoggedIn(r) && auth.SomeUsersVerified() {
|
||||
http.Redirect(w, r, "/login?failure=There is already a verified account, please login.", http.StatusSeeOther)
|
||||
}
|
||||
if (IsLoggedIn(r) && IsVerified(r)) || SomeUsersVerified() {
|
||||
if (auth.IsLoggedIn(r) && auth.IsVerified(r)) || auth.SomeUsersVerified() {
|
||||
http.Redirect(w, r, "/home", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -41,7 +44,7 @@ func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
|
||||
PostRegisterPage(w, r)
|
||||
return
|
||||
}
|
||||
bs, err := Container.GetPageUseCase.GetPage("register", map[string]interface{}{
|
||||
bs, err := api.Container.GetPageUseCase.GetPage("register", map[string]interface{}{
|
||||
"PageError": registerPage.PageError,
|
||||
"Username": registerPage.Username,
|
||||
"Email": registerPage.Email,
|
||||
@@ -57,12 +60,12 @@ func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
|
||||
func PostRegisterPage(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
|
||||
credentials := RegisterCredentials{
|
||||
credentials := auth.RegisterCredentials{
|
||||
Username: r.FormValue("username"),
|
||||
Password: r.FormValue("password"),
|
||||
Email: r.FormValue("email"),
|
||||
}
|
||||
err := validate.Struct(credentials)
|
||||
err := api.Validate.Struct(credentials)
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
GetRegisterPageHandler(&RegisterPage{
|
||||
@@ -78,7 +81,7 @@ func PostRegisterPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
verificationCode := uuid.NewString()
|
||||
createdUser, err := getNewUser(credentials, verificationCode)
|
||||
createdUser, err := auth.GetNewUser(credentials, verificationCode)
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
GetRegisterPageHandler(&RegisterPage{
|
||||
@@ -93,12 +96,12 @@ func PostRegisterPage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
_ = Container.SendMailUseCase.SendMail(createdUser.Email, "mailValidation", map[string]string{
|
||||
_ = api.Container.SendMailUseCase.SendMail(createdUser.Email, "mailValidation", map[string]string{
|
||||
"Host": os.Getenv("HOST"),
|
||||
"VerificationCode": verificationCode,
|
||||
})
|
||||
|
||||
_ = SetJwtCookie(&w, createdUser.ID)
|
||||
_ = auth.SetJwtCookie(&w, createdUser.ID)
|
||||
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
}
|
||||
@@ -1,32 +1,35 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/auth"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
)
|
||||
|
||||
func PostRegisterPendingPage(w http.ResponseWriter, r *http.Request) {
|
||||
token, _ := jwtauth.VerifyRequest(
|
||||
TokenAuth, r,
|
||||
auth.Token, r,
|
||||
jwtauth.TokenFromCookie,
|
||||
jwtauth.TokenFromHeader,
|
||||
jwtauth.TokenFromQuery)
|
||||
userId := token.PrivateClaims()["user_id"].(float64)
|
||||
err := Container.DeleteUserUseCase.DeleteUser(uint32(userId))
|
||||
err := api.Container.DeleteUserUseCase.DeleteUser(uint32(userId))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
r.Method = http.MethodGet
|
||||
http.Redirect(w, r, "/register/pending", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
RemoveJwtCookie(w)
|
||||
auth.RemoveJwtCookie(w)
|
||||
|
||||
http.Redirect(w, r, "/register", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func GetRegisterPendingPage(w http.ResponseWriter, _ *http.Request) {
|
||||
registerPendingTmpl, _ := Container.GetPageUseCase.GetPage("registerPending", map[string]interface{}{
|
||||
registerPendingTmpl, _ := api.Container.GetPageUseCase.GetPage("registerPending", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
})
|
||||
_, _ = w.Write(registerPendingTmpl)
|
||||
@@ -1,10 +1,13 @@
|
||||
package api
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/auth"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func GetRegisterValidatePage(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -15,27 +18,27 @@ func GetRegisterValidatePage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
token, _ := jwtauth.VerifyRequest(
|
||||
TokenAuth,
|
||||
auth.Token,
|
||||
r,
|
||||
jwtauth.TokenFromCookie,
|
||||
jwtauth.TokenFromHeader,
|
||||
jwtauth.TokenFromQuery)
|
||||
|
||||
userId := token.PrivateClaims()["user_id"].(float64)
|
||||
user, _ := Container.GetUserUseCase.GetUser(uint32(userId))
|
||||
user, _ := api.Container.GetUserUseCase.GetUser(uint32(userId))
|
||||
errorMessage := ""
|
||||
|
||||
err := bcrypt.CompareHashAndPassword([]byte(user.VerificationCode), []byte(queryVerificationCode))
|
||||
if err != nil || user.VerificationExpiration.Before(time.Now()) {
|
||||
errorMessage = "Verification link is incorrect or has expired."
|
||||
} else {
|
||||
_, err := Container.UpdateUserUseCase.UpdateVerificationStatus(user.ID, true)
|
||||
_, err := api.Container.UpdateUserUseCase.UpdateVerificationStatus(user.ID, true)
|
||||
if err != nil {
|
||||
errorMessage = "Something went wrong server-side. User account may not exist."
|
||||
}
|
||||
}
|
||||
|
||||
registerValidateTmpl, _ := Container.GetPageUseCase.GetPage("registerValidate", map[string]interface{}{
|
||||
registerValidateTmpl, _ := api.Container.GetPageUseCase.GetPage("registerValidate", map[string]interface{}{
|
||||
"Head": headTmpl,
|
||||
"PageError": NewPageError(errorMessage),
|
||||
})
|
||||
@@ -1,12 +1,15 @@
|
||||
package api
|
||||
package post
|
||||
|
||||
import (
|
||||
"GoCMS/api"
|
||||
"GoCMS/api/controllers/auth"
|
||||
"GoCMS/domain/post"
|
||||
"GoCMS/useCases"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PostPost struct {
|
||||
@@ -14,16 +17,16 @@ type PostPost struct {
|
||||
Body string `json:"body" validate:"required,max=10000"`
|
||||
}
|
||||
|
||||
const idUint32ErrorMessage = "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32)."
|
||||
const IdUint32ErrorMessage = "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32)."
|
||||
|
||||
func getPost(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||
if err != nil {
|
||||
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, IdUint32ErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
localPost, err := Container.GetPostUseCase.GetPost(uint32(id))
|
||||
localPost, err := api.Container.GetPostUseCase.GetPost(uint32(id))
|
||||
if err != nil || !localPost.IsOnline {
|
||||
http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", http.StatusNotFound)
|
||||
return
|
||||
@@ -37,17 +40,17 @@ func postPost(w http.ResponseWriter, r *http.Request) {
|
||||
var localPost PostPost
|
||||
err := json.NewDecoder(r.Body).Decode(&localPost)
|
||||
if err != nil {
|
||||
http.Error(w, bodyErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, auth.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = validate.Struct(localPost)
|
||||
err = api.Validate.Struct(localPost)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
createdPost, err := Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
|
||||
createdPost, err := api.Container.CreatePostUseCase.CreatePost(useCases.CreatePostCommand{
|
||||
Title: localPost.Title,
|
||||
Body: localPost.Body,
|
||||
})
|
||||
@@ -61,7 +64,7 @@ func postPost(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func listPosts(w http.ResponseWriter, _ *http.Request) {
|
||||
posts := Container.ListPostsUseCase.ListPosts()
|
||||
posts := api.Container.ListPostsUseCase.ListPosts()
|
||||
onlinePosts := make([]post.Post, 0)
|
||||
for _, localPost := range posts {
|
||||
if localPost.IsOnline {
|
||||
@@ -75,10 +78,10 @@ func listPosts(w http.ResponseWriter, _ *http.Request) {
|
||||
func deletePost(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||
if err != nil {
|
||||
http.Error(w, idUint32ErrorMessage, http.StatusBadRequest)
|
||||
http.Error(w, IdUint32ErrorMessage, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
err = Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
err = api.Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
}
|
||||
+2
-2
@@ -2,8 +2,8 @@ package api
|
||||
|
||||
import "github.com/go-playground/validator/v10"
|
||||
|
||||
var validate *validator.Validate
|
||||
var Validate *validator.Validate
|
||||
|
||||
func InitValidator() {
|
||||
validate = validator.New()
|
||||
Validate = validator.New()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user