30 lines
530 B
Go
30 lines
530 B
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"gosimplenpm/storage"
|
|
)
|
|
|
|
func Get(w http.ResponseWriter, r *http.Request) {
|
|
packageName := mux.Vars(r)["name"]
|
|
|
|
fileToServe, err := storage.GetPackageFromStore(packageName)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if fileToServe == "" {
|
|
ret := fmt.Sprintf("Package not found: %s", packageName)
|
|
http.Error(w, ret, http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
// serve file
|
|
http.ServeFile(w, r, fileToServe)
|
|
}
|