Initial commit - Get file from server successful
This commit is contained in:
		
						commit
						fb01c8836c
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
{
 | 
			
		||||
    "_id": "@test/bar1",
 | 
			
		||||
    "description": "",
 | 
			
		||||
,    "versions": {}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
{
 | 
			
		||||
    "_id": "@test/bar",
 | 
			
		||||
    "description": "",
 | 
			
		||||
,   "versions": {}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
module gosimplenpm
 | 
			
		||||
 | 
			
		||||
go 1.20
 | 
			
		||||
 | 
			
		||||
require github.com/gorilla/mux v1.8.0
 | 
			
		||||
 | 
			
		||||
// replace gosimplenpm/handler => ./handler
 | 
			
		||||
| 
						 | 
				
			
			@ -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=
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package handler
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Publish(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package handler
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func DistTagDelete(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package handler
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func DistTagGet(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package handler
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func DistTagPut(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,9 @@
 | 
			
		|||
package handler
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Tar(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue