From fb01c8836c8aa68016d213781114ec3733f09a63 Mon Sep 17 00:00:00 2001 From: OLUWADAMILOLA OKUSANYA Date: Sat, 10 Jun 2023 20:32:02 -0400 Subject: [PATCH] Initial commit - Get file from server successful --- Makefile | 11 +++++++++++ app.go | 28 ++++++++++++++++++++++++++++ examples/package1/index.json | 5 +++++ examples/package2/index.json | 5 +++++ go.mod | 7 +++++++ go.sum | 2 ++ handler/get.go | 29 +++++++++++++++++++++++++++++ handler/publish.go | 9 +++++++++ handler/tagdelete.go | 9 +++++++++ handler/tagget.go | 9 +++++++++ handler/tagput.go | 9 +++++++++ handler/tar.go | 9 +++++++++ main.go | 13 +++++++++++++ storage/fs.go | 33 +++++++++++++++++++++++++++++++++ 14 files changed, 178 insertions(+) create mode 100644 Makefile create mode 100644 app.go create mode 100644 examples/package1/index.json create mode 100644 examples/package2/index.json create mode 100644 go.mod create mode 100644 go.sum create mode 100644 handler/get.go create mode 100644 handler/publish.go create mode 100644 handler/tagdelete.go create mode 100644 handler/tagget.go create mode 100644 handler/tagput.go create mode 100644 handler/tar.go create mode 100644 main.go create mode 100644 storage/fs.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7749aa3 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +clean: + go clean +dep: + go mod tidy +fmt: + go fmt ./... +lint: + golangci-lint run +lint-all: + golangci-lint run --enable-all +.PHONY: dep lint clean diff --git a/app.go b/app.go new file mode 100644 index 0000000..8ddca7b --- /dev/null +++ b/app.go @@ -0,0 +1,28 @@ +package main + +import ( + "log" + + "github.com/gorilla/mux" + + "gosimplenpm/handler" +) + +type application struct { + logger *log.Logger +} + +func (app *application) Routes() *mux.Router { + m := mux.NewRouter() + + // main handler + m.HandleFunc("/{name}", handler.Get).Methods("GET") + m.HandleFunc("/{name}", handler.Publish).Methods("PUT") + // tar handlers + m.HandleFunc("/{name}/-/{tar}", handler.Tar).Methods("GET") + // tag handlers + m.HandleFunc("/-/package/{name}/dist-tags/{tag}", handler.DistTagDelete).Methods("DELETE") + m.HandleFunc("/-/package/{name}/dist-tags/{tag}", handler.DistTagPut).Methods("PUT") + m.HandleFunc("/-/package/{name}/dist-tags", handler.DistTagGet).Methods("GET") + return m +} diff --git a/examples/package1/index.json b/examples/package1/index.json new file mode 100644 index 0000000..b0b282e --- /dev/null +++ b/examples/package1/index.json @@ -0,0 +1,5 @@ +{ + "_id": "@test/bar1", + "description": "", +, "versions": {} +} \ No newline at end of file diff --git a/examples/package2/index.json b/examples/package2/index.json new file mode 100644 index 0000000..b57e6a3 --- /dev/null +++ b/examples/package2/index.json @@ -0,0 +1,5 @@ +{ + "_id": "@test/bar", + "description": "", +, "versions": {} +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a111b6f --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module gosimplenpm + +go 1.20 + +require github.com/gorilla/mux v1.8.0 + +// replace gosimplenpm/handler => ./handler diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5350288 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= diff --git a/handler/get.go b/handler/get.go new file mode 100644 index 0000000..0fe3ec7 --- /dev/null +++ b/handler/get.go @@ -0,0 +1,29 @@ +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) +} diff --git a/handler/publish.go b/handler/publish.go new file mode 100644 index 0000000..f99312c --- /dev/null +++ b/handler/publish.go @@ -0,0 +1,9 @@ +package handler + +import ( + "net/http" +) + +func Publish(w http.ResponseWriter, r *http.Request) { + +} diff --git a/handler/tagdelete.go b/handler/tagdelete.go new file mode 100644 index 0000000..bfa906a --- /dev/null +++ b/handler/tagdelete.go @@ -0,0 +1,9 @@ +package handler + +import ( + "net/http" +) + +func DistTagDelete(w http.ResponseWriter, r *http.Request) { + +} diff --git a/handler/tagget.go b/handler/tagget.go new file mode 100644 index 0000000..568c2af --- /dev/null +++ b/handler/tagget.go @@ -0,0 +1,9 @@ +package handler + +import ( + "net/http" +) + +func DistTagGet(w http.ResponseWriter, r *http.Request) { + +} diff --git a/handler/tagput.go b/handler/tagput.go new file mode 100644 index 0000000..b050b3d --- /dev/null +++ b/handler/tagput.go @@ -0,0 +1,9 @@ +package handler + +import ( + "net/http" +) + +func DistTagPut(w http.ResponseWriter, r *http.Request) { + +} diff --git a/handler/tar.go b/handler/tar.go new file mode 100644 index 0000000..534c602 --- /dev/null +++ b/handler/tar.go @@ -0,0 +1,9 @@ +package handler + +import ( + "net/http" +) + +func Tar(w http.ResponseWriter, r *http.Request) { + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3ac3e91 --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" + "net/http" +) + +func main() { + app := new(application) + log.Print("Starting server on port 4000") + err := http.ListenAndServe(":4000", app.Routes()) + log.Fatal(err) +} diff --git a/storage/fs.go b/storage/fs.go new file mode 100644 index 0000000..7b5d53f --- /dev/null +++ b/storage/fs.go @@ -0,0 +1,33 @@ +package storage + +import ( + "fmt" + "io/fs" + "path" + "strings" + + "path/filepath" +) + +func GetPackageFromStore(packageName string) (string, error) { + fileToServe := "" + searchDir, err := filepath.Abs("./examples") + if err != nil { + fmt.Printf("File repo not found: +%v/n", err) + return "", err + } + + err = filepath.WalkDir(searchDir, func(fp string, info fs.DirEntry, e error) error { + if strings.Contains(fp, path.Join(packageName, "index.json")) { + fileToServe = fp + } + return e + }) + + if err != nil { + fmt.Printf("List files error: +%v/n", err) + return "", err + } + + return fileToServe, nil +}