chore: better path joining
This commit is contained in:
parent
ab47d5c35a
commit
4b2fd8dd04
1 changed files with 10 additions and 3 deletions
13
main.go
13
main.go
|
@ -5,7 +5,9 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
@ -46,15 +48,20 @@ or: cmd | curl --data-binary @- %s
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.WriteFile("db/"+uuid.String(), body, 0644)
|
os.WriteFile(path.Join("db", uuid.String()), body, 0644)
|
||||||
fmt.Fprintf(w, hostURL+uuid.String())
|
url, err := url.JoinPath(hostURL, uuid.String())
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, url)
|
||||||
log.Printf("paste created: uuid=%s", uuid.String())
|
log.Printf("paste created: uuid=%s", uuid.String())
|
||||||
default:
|
default:
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.Handle("/", http.FileServer(http.Dir("db")))
|
http.Handle("/", http.FileServer(http.Dir(databasePath)))
|
||||||
|
|
||||||
|
log.Printf("Starting server on %s", listenAddr)
|
||||||
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
log.Fatal(http.ListenAndServe(listenAddr, nil))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue