Skip to content

Commit

Permalink
fix: improve file handling in webserver to prevent potential errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishrb committed Oct 9, 2024
1 parent 9bb188e commit 5a455ac
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ func (client *Client) Serve(file string) error {
// Serve website with rendered markdown
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
f, err := dir.Open(r.URL.Path)
defer f.Close()
if err == nil {
defer f.Close()
}

if err == nil && regex.MatchString(r.URL.Path) {
// Open file and convert to html
Expand All @@ -55,7 +57,9 @@ func (client *Client) Serve(file string) error {
// If README.md exists then open README.md at beginning
readme := "README.md"
f, err := dir.Open(readme)
defer f.Close()
if err == nil {
defer f.Close()
}
if err == nil && client.OpenReadme {
addr = path.Join(addr, readme)
}
Expand Down

0 comments on commit 5a455ac

Please sign in to comment.