Merge pull request #25 from fossyy/staging

Staging
This commit is contained in:
2024-05-14 11:35:55 +07:00
committed by GitHub
20 changed files with 119 additions and 63 deletions

View File

@ -85,7 +85,8 @@ func init() {
func GET(w http.ResponseWriter, r *http.Request) { func GET(w http.ResponseWriter, r *http.Request) {
if _, ok := CsrfTokens[r.URL.Query().Get("state")]; !ok { if _, ok := CsrfTokens[r.URL.Query().Get("state")]; !ok {
http.Error(w, "csrf token mismatch", http.StatusInternalServerError) //csrf token mismatch error
w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -106,16 +107,16 @@ func GET(w http.ResponseWriter, r *http.Request) {
resp, err := http.Post("https://oauth2.googleapis.com/token", "application/x-www-form-urlencoded", bytes.NewBufferString(formData.Encode())) resp, err := http.Post("https://oauth2.googleapis.com/token", "application/x-www-form-urlencoded", bytes.NewBufferString(formData.Encode()))
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error("Error:", err) log.Error("Error:", err)
http.Error(w, "Failed to get token", http.StatusInternalServerError)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
var oauthData OauthToken var oauthData OauthToken
if err := json.NewDecoder(resp.Body).Decode(&oauthData); err != nil { if err := json.NewDecoder(resp.Body).Decode(&oauthData); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error("Error reading token response body:", err) log.Error("Error reading token response body:", err)
http.Error(w, "Failed to read token response body", http.StatusInternalServerError)
return return
} }
@ -124,8 +125,8 @@ func GET(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest("GET", "https://www.googleapis.com/oauth2/v1/userinfo?alt=json", nil) req, err := http.NewRequest("GET", "https://www.googleapis.com/oauth2/v1/userinfo?alt=json", nil)
req.Header.Set("Authorization", "Bearer "+oauthData.AccessToken) req.Header.Set("Authorization", "Bearer "+oauthData.AccessToken)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error("Error creating user info request:", err) log.Error("Error creating user info request:", err)
http.Error(w, "Failed to create user info request", http.StatusInternalServerError)
return return
} }
@ -134,14 +135,14 @@ func GET(w http.ResponseWriter, r *http.Request) {
var oauthUser OauthUser var oauthUser OauthUser
if err := json.NewDecoder(userInfoResp.Body).Decode(&oauthUser); err != nil { if err := json.NewDecoder(userInfoResp.Body).Decode(&oauthUser); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error("Error reading user info response body:", err) log.Error("Error reading user info response body:", err)
http.Error(w, "Failed to read user info response body", http.StatusInternalServerError)
return return
} }
if oauthUser.Email == "" { if oauthUser.Email == "" {
w.WriteHeader(http.StatusInternalServerError)
log.Error("Error reading user info response body: email not found") log.Error("Error reading user info response body: email not found")
http.Error(w, "Invalid email is given", http.StatusInternalServerError)
return return
} }
@ -159,7 +160,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
user, err := cache.GetUser(oauthUser.Email) user, err := cache.GetUser(oauthUser.Email)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -3,16 +3,24 @@ package googleOauthHandler
import ( import (
"fmt" "fmt"
googleOauthCallbackHandler "github.com/fossyy/filekeeper/handler/auth/google/callback" googleOauthCallbackHandler "github.com/fossyy/filekeeper/handler/auth/google/callback"
"github.com/fossyy/filekeeper/logger"
"github.com/fossyy/filekeeper/utils" "github.com/fossyy/filekeeper/utils"
"net/http" "net/http"
"time" "time"
) )
var log *logger.AggregatedLogger
func init() {
log = logger.Logger()
}
func GET(w http.ResponseWriter, r *http.Request) { func GET(w http.ResponseWriter, r *http.Request) {
token, err := utils.GenerateCSRFToken() token, err := utils.GenerateCSRFToken()
googleOauthCallbackHandler.CsrfTokens[token] = &googleOauthCallbackHandler.CsrfToken{Token: token, CreateTime: time.Now()} googleOauthCallbackHandler.CsrfTokens[token] = &googleOauthCallbackHandler.CsrfToken{Token: token, CreateTime: time.Now()}
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error())
return return
} }
http.Redirect(w, r, fmt.Sprintf("https://accounts.google.com/o/oauth2/auth?scope=email profile&response_type=code&access_type=offline&state=%s&redirect_uri=%s&client_id=%s", token, utils.Getenv("GOOGLE_CALLBACK"), utils.Getenv("GOOGLE_CLIENT_ID")), http.StatusFound) http.Redirect(w, r, fmt.Sprintf("https://accounts.google.com/o/oauth2/auth?scope=email profile&response_type=code&access_type=offline&state=%s&redirect_uri=%s&client_id=%s", token, utils.Getenv("GOOGLE_CALLBACK"), utils.Getenv("GOOGLE_CLIENT_ID")), http.StatusFound)

View File

@ -67,7 +67,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -82,7 +82,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
} }
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -97,7 +97,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -121,7 +121,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -20,7 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
userSession := r.Context().Value("user").(types.User) userSession := r.Context().Value("user").(types.User)
files, err := db.DB.GetFiles(userSession.UserID.String()) files, err := db.DB.GetFiles(userSession.UserID.String())
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -37,7 +37,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
component := downloadView.Main("Download Page", filesData) component := downloadView.Main("Download Page", filesData)
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -19,7 +19,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
fileID := r.PathValue("id") fileID := r.PathValue("id")
file, err := db.DB.GetFile(fileID) file, err := db.DB.GetFile(fileID)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -32,13 +32,13 @@ func GET(w http.ResponseWriter, r *http.Request) {
if filepath.Dir(saveFolder) != filepath.Join(basePath, file.OwnerID.String()) { if filepath.Dir(saveFolder) != filepath.Join(basePath, file.OwnerID.String()) {
log.Error("invalid path") log.Error("invalid path")
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
openFile, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_RDONLY, 0) openFile, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_RDONLY, 0)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -46,7 +46,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
stat, err := openFile.Stat() stat, err := openFile.Stat()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -1,6 +1,7 @@
package errorHandler package errorHandler
import ( import (
"fmt"
"net/http" "net/http"
"github.com/fossyy/filekeeper/logger" "github.com/fossyy/filekeeper/logger"
@ -13,11 +14,21 @@ func init() {
log = logger.Logger() log = logger.Logger()
} }
func ALL(w http.ResponseWriter, r *http.Request) { func NotFound(w http.ResponseWriter, r *http.Request) {
component := errorView.Main("Not Found") component := errorView.NotFound("Not Found")
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) fmt.Fprint(w, err.Error())
log.Error(err.Error())
return
}
}
func InternalServerError(w http.ResponseWriter, r *http.Request) {
component := errorView.InternalServerError("Internal Server Error")
err := component.Render(r.Context(), w)
if err != nil {
fmt.Fprint(w, err.Error())
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -71,7 +71,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -95,7 +95,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -111,7 +111,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
err = verifyForgot(userData) err = verifyForgot(userData)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -119,7 +119,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
component := forgotPasswordView.EmailSend("Forgot Password Page") component := forgotPasswordView.EmailSend("Forgot Password Page")
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -39,7 +39,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -58,7 +58,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -72,7 +72,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -81,14 +81,14 @@ func POST(w http.ResponseWriter, r *http.Request) {
hashedPassword, err := utils.HashPassword(password) hashedPassword, err := utils.HashPassword(password)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
err = db.DB.UpdateUserPassword(data.User.Email, hashedPassword) err = db.DB.UpdateUserPassword(data.User.Email, hashedPassword)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -103,7 +103,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
component := forgotPasswordView.ChangeSuccess("Forgot Password Page") component := forgotPasswordView.ChangeSuccess("Forgot Password Page")
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -19,7 +19,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
component := indexView.Main("main page", userSession) component := indexView.Main("main page", userSession)
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -20,7 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
if errors.Is(err, &session.SessionNotFoundError{}) { if errors.Is(err, &session.SessionNotFoundError{}) {
storeSession.Destroy(w) storeSession.Destroy(w)
} }
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }

View File

@ -60,7 +60,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -84,7 +84,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
log.Error(err.Error()) log.Error(err.Error())
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -134,7 +134,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -70,7 +70,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -79,7 +79,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
func POST(w http.ResponseWriter, r *http.Request) { func POST(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -94,7 +94,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -116,7 +116,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
}) })
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -125,7 +125,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
err = verifyEmail(&newUser) err = verifyEmail(&newUser)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -133,7 +133,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
component := signupView.EmailSend("Sign up Page") component := signupView.EmailSend("Sign up Page")
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -33,7 +33,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
}) })
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -47,7 +47,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
err = component.Render(r.Context(), w) err = component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -28,13 +28,13 @@ func POST(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
handleError(w, err, http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
var fileInfo types.FileInfo var fileInfo types.FileInfo
if err := json.Unmarshal(body, &fileInfo); err != nil { if err := json.Unmarshal(body, &fileInfo); err != nil {
handleError(w, err, http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -43,7 +43,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
upload, err := handleNewUpload(userSession, fileInfo) upload, err := handleNewUpload(userSession, fileInfo)
if err != nil { if err != nil {
handleError(w, err, http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
respondJSON(w, upload) respondJSON(w, upload)

View File

@ -21,7 +21,7 @@ func init() {
func GET(w http.ResponseWriter, r *http.Request) { func GET(w http.ResponseWriter, r *http.Request) {
component := filesView.Main("upload page") component := filesView.Main("upload page")
if err := component.Render(r.Context(), w); err != nil { if err := component.Render(r.Context(), w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
} }
@ -29,7 +29,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
func POST(w http.ResponseWriter, r *http.Request) { func POST(w http.ResponseWriter, r *http.Request) {
fileID := r.PathValue("id") fileID := r.PathValue("id")
if err := r.ParseMultipartForm(32 << 20); err != nil { if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -39,7 +39,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(uploadDir); os.IsNotExist(err) { if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
if err := os.Mkdir(uploadDir, os.ModePerm); err != nil { if err := os.Mkdir(uploadDir, os.ModePerm); err != nil {
log.Error("error getting upload info: " + err.Error()) log.Error("error getting upload info: " + err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
} }
@ -47,7 +47,7 @@ func POST(w http.ResponseWriter, r *http.Request) {
file, err := cache.GetFile(fileID) file, err := cache.GetFile(fileID)
if err != nil { if err != nil {
log.Error("error getting upload info: " + err.Error()) log.Error("error getting upload info: " + err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -57,14 +57,14 @@ func POST(w http.ResponseWriter, r *http.Request) {
if filepath.Dir(saveFolder) != filepath.Join(basePath, userSession.UserID.String()) { if filepath.Dir(saveFolder) != filepath.Join(basePath, userSession.UserID.String()) {
log.Error("invalid path") log.Error("invalid path")
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
fileByte, fileHeader, err := r.FormFile("chunk") fileByte, fileHeader, err := r.FormFile("chunk")
if err != nil { if err != nil {
log.Error("error getting upload info: " + err.Error()) log.Error("error getting upload info: " + err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
defer fileByte.Close() defer fileByte.Close()
@ -80,14 +80,14 @@ func POST(w http.ResponseWriter, r *http.Request) {
dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) dst, err := os.OpenFile(filepath.Join(saveFolder, file.Name), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil { if err != nil {
log.Error("error making upload folder: " + err.Error()) log.Error("error making upload folder: " + err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
defer dst.Close() defer dst.Close()
if _, err := io.Copy(dst, fileByte); err != nil { if _, err := io.Copy(dst, fileByte); err != nil {
log.Error("error copying byte to file dst: " + err.Error()) log.Error("error copying byte to file dst: " + err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }

View File

@ -20,7 +20,7 @@ func GET(w http.ResponseWriter, r *http.Request) {
component := userView.Main("User Page", userSession, session.GetSessions(userSession.Email)) component := userView.Main("User Page", userSession, session.GetSessions(userSession.Email))
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Error(err.Error()) log.Error(err.Error())
return return
} }

View File

@ -25,13 +25,23 @@ type wrapper struct {
} }
func (w *wrapper) WriteHeader(code int) { func (w *wrapper) WriteHeader(code int) {
w.statusCode = code
if code == http.StatusNotFound { if code == http.StatusNotFound {
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
errorHandler.ALL(w.ResponseWriter, w.request) w.ResponseWriter.WriteHeader(code)
errorHandler.NotFound(w.ResponseWriter, w.request)
return
}
if code == http.StatusInternalServerError {
w.Header().Set("Content-Type", "text/html")
w.ResponseWriter.WriteHeader(code)
errorHandler.InternalServerError(w.ResponseWriter, w.request)
return return
} }
w.ResponseWriter.WriteHeader(code) w.ResponseWriter.WriteHeader(code)
w.statusCode = code
return return
} }

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 128 KiB

View File

@ -193,6 +193,11 @@ func SetupRoutes() *http.ServeMux {
http.ServeFile(w, r, "public/favicon.ico") http.ServeFile(w, r, "public/favicon.ico")
}) })
//TODO add error message catching to the middleware so we can show the error message at the error page
handler.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "anjay", http.StatusInternalServerError)
})
fileServer := http.FileServer(http.Dir("./public")) fileServer := http.FileServer(http.Dir("./public"))
handler.Handle("/public/", http.StripPrefix("/public", fileServer)) handler.Handle("/public/", http.StripPrefix("/public", fileServer))

View File

@ -2,17 +2,17 @@ package errorView
import "github.com/fossyy/filekeeper/view/layout" import "github.com/fossyy/filekeeper/view/layout"
templ content(title string){ templ NotFound(title string){
@layout.Base(title){ @layout.Base(title){
<div class="flex flex-col items-center justify-center w-full min-h-[calc(100vh-1rem)] py-10 text-center gap-4 md:gap-8"> <div class="flex flex-col items-center justify-center w-full min-h-[calc(100vh-1rem)] py-10 text-center gap-4 md:gap-8">
<div class="space-y-2"> <div class="space-y-2">
<h1 class="text-4xl font-bold tracking-tighter sm:text-5xl">404 Not Found</h1> <h1 class="text-4xl font-bold tracking-tighter sm:text-5xl">404 Not Found</h1>
<p class="max-w-[600px] text-gray-500 md:text-xl/relaxed lg:text-base/relaxed xl:text-xl/relaxed dark:text-gray-400"> <p class="max-w-[600px] text-gray-500 md:text-xl/relaxed lg:text-base/relaxed xl:text-xl/relaxed">
The page you are looking for does not exist. It might have been moved or deleted. The page you are looking for does not exist. It might have been moved or deleted.
</p> </p>
</div> </div>
<a <a
class="inline-flex h-10 items-center rounded-md border border-gray-200 border-gray-200 bg-white px-8 text-sm font-medium shadow-sm gap-2 transition-colors hover:bg-gray-100 hover:text-gray-900 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950 dark:border-gray-800 dark:border-gray-800 dark:bg-gray-950 dark:hover:bg-gray-800 dark:hover:text-gray-50 dark:focus-visible:ring-gray-300" class="inline-flex h-10 items-center rounded-md border border-gray-200 border-gray-200 bg-white px-8 text-sm font-medium shadow-sm gap-2 transition-colors hover:bg-gray-100 hover:text-gray-900 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950"
href="/" href="/"
> >
<svg <svg
@ -34,6 +34,26 @@ templ content(title string){
} }
} }
templ Main(title string) { templ InternalServerError(title string){
@content(title) @layout.Base(title){
<main class="container mx-auto px-4 md:px-6">
<div class="flex h-screen w-full flex-col items-center justify-center bg-white">
<image class="w-32 md:w-64 lg:w-128" src="/public/InternalServerErrorIcon.svg" alt="brand image" />
<div class="mx-auto max-w-md space-y-4 text-center">
<h1 class="text-4xl font-bold tracking-tight text-gray-900">Oops! Something went wrong.</h1>
<p class="text-gray-500">
We're sorry, but an internal server error has occurred. Please try again later.
</p>
<div class="grid gap-2">
<a
class="inline-flex h-10 items-center justify-center rounded-md bg-gray-900 px-6 text-sm font-medium text-gray-50 shadow transition-colors hover:bg-gray-900/90 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-950 disabled:pointer-events-none disabled:opacity-50"
href="/"
>
Go back to homepage
</a>
</div>
</div>
</div>
</main>
}
} }