Skip to content

Commit

Permalink
refactor(main): add a change to the handler signatures so they can re…
Browse files Browse the repository at this point in the history
…turn error and errors can be handled independently
  • Loading branch information
danvergara committed Mar 27, 2024
1 parent 2198a0b commit 0f06f94
Showing 1 changed file with 38 additions and 51 deletions.
89 changes: 38 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type ConvertedFile struct {
FileType string
}

func index(w http.ResponseWriter, _ *http.Request) {
func index(w http.ResponseWriter, _ *http.Request) error {
tmpls := []string{
"templates/base.tmpl",
"templates/partials/htmx.tmpl",
Expand All @@ -115,19 +115,19 @@ func index(w http.ResponseWriter, _ *http.Request) {
tmpl, err := template.ParseFS(templatesHTML, tmpls...)
if err != nil {
log.Printf("error ocurred parsing templates: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

err = tmpl.ExecuteTemplate(w, "base", nil)
if err != nil {
log.Printf("error ocurred executing template: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

return nil
}

func handleUploadFile(w http.ResponseWriter, r *http.Request) {
func handleUploadFile(w http.ResponseWriter, r *http.Request) error {
var (
convertedFile []byte
convertedFilePath string
Expand All @@ -139,16 +139,14 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
file, fileHeader, err := r.FormFile(uploadFileFormField)
if err != nil {
log.Printf("error ocurred getting file from form: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}
defer file.Close()

fileBytes, err := io.ReadAll(file)
if err != nil {
log.Printf("error ocurred reading file: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

targetFileSubType := r.FormValue("input_format")
Expand All @@ -158,22 +156,19 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
fileType, subType, err := files.TypeAndSupType(detectedFileType.String())
if err != nil {
log.Printf("error occurred getting type and subtype from mimetype: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

fileFactory, err := files.BuildFactory(fileType, fileHeader.Filename)
if err != nil {
log.Printf("error occurred while getting a file factory: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

f, err := fileFactory.NewFile(subType)
if err != nil {
log.Printf("error occurred getting the file object: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

targetFileType := files.SupportedFileTypes()[targetFileSubType]
Expand All @@ -184,8 +179,7 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
)
if err != nil {
log.Printf("error ocurred while converting image %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

if fileType == "application" {
Expand All @@ -198,14 +192,12 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
newFile, err := os.Create(convertedFilePath)
if err != nil {
log.Printf("error occurred converting file: %v", err)
renderError(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}
defer newFile.Close()
if _, err := newFile.Write(convertedFile); err != nil {
log.Printf("error occurred writing file: %v", err)
renderError(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

tmpls := []string{
Expand All @@ -216,17 +208,15 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(templatesHTML, tmpls...)
if err != nil {
log.Printf("error occurred parsing template files: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

convertedFileMimeType := mimetype.Detect(convertedFile)

convertedFileType, _, err := files.TypeAndSupType(convertedFileMimeType.String())
if err != nil {
log.Printf("error occurred getting the file type of the result file: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

err = tmpl.ExecuteTemplate(
Expand All @@ -236,25 +226,23 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {
)
if err != nil {
log.Printf("error occurred executing template files: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}
}

func handleFileFormat(w http.ResponseWriter, r *http.Request) {
return nil
}

func handleFileFormat(w http.ResponseWriter, r *http.Request) error {
file, _, err := r.FormFile(uploadFileFormField)
if err != nil {
log.Printf("error ocurred while getting file from form: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

fileBytes, err := io.ReadAll(file)
if err != nil {
log.Printf("error occurred while executing template files: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

detectedFileType := mimetype.Detect(fileBytes)
Expand All @@ -266,33 +254,31 @@ func handleFileFormat(w http.ResponseWriter, r *http.Request) {
fileType, subType, err := files.TypeAndSupType(detectedFileType.String())
if err != nil {
log.Printf("error occurred getting type and subtype from mimetype: %v", err)
renderError(w, "invalid file format", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

fileFactory, err := files.BuildFactory(fileType, "")
if err != nil {
log.Printf("error occurred while getting a file factory: %v", err)
renderError(w, "invalid file", http.StatusBadRequest)
return
return WithHTTPStatus(err, http.StatusBadRequest)
}

f, err := fileFactory.NewFile(subType)
if err != nil {
log.Printf("error occurred getting the file object: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

tmpl, err := template.ParseFS(templatesHTML, templates...)
if err = tmpl.ExecuteTemplate(w, "format-elements", f.SupportedFormats()); err != nil {
log.Printf("error occurred parsing template files: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

return nil
}

func handleModal(w http.ResponseWriter, r *http.Request) {
func handleModal(w http.ResponseWriter, r *http.Request) error {
filename := r.URL.Query().Get("filename")
filetype := r.URL.Query().Get("filetype")

Expand All @@ -303,15 +289,15 @@ func handleModal(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(templatesHTML, tmpls...)
if err != nil {
log.Printf("error occurred parsing template files: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

if err = tmpl.ExecuteTemplate(w, "content", ConvertedFile{Filename: filename, FileType: filetype}); err != nil {
log.Printf("error occurred executing template files: %v", err)
renderError(w, "internal error", http.StatusInternalServerError)
return
return WithHTTPStatus(err, http.StatusInternalServerError)
}

return nil
}

func main() {
Expand All @@ -331,14 +317,15 @@ func main() {

r.Handle("/static/*", fs)
r.Handle("/files/*", http.StripPrefix("/files", fsUpload))
r.Get("/", index)
r.Post("/upload", handleUploadFile)
r.Post("/format", handleFileFormat)
r.Get("/modal", handleModal)
r.Get("/", toHandler(index))
r.Post("/upload", toHandler(handleUploadFile))
r.Post("/format", toHandler(handleFileFormat))
r.Get("/modal", toHandler(handleModal))

http.ListenAndServe(fmt.Sprintf(":%s", port), r)
}

// renderError functions executes the error template.
func renderError(w http.ResponseWriter, message string, statusCode int) {
w.WriteHeader(statusCode)
tmpl, _ := template.ParseFS(templatesHTML, "templates/partials/error.tmpl")
Expand Down

0 comments on commit 0f06f94

Please sign in to comment.