diff --git a/.gitignore b/.gitignore index 216da7c..07cfe30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* tmp/* +dist/* ezshare.toml ezshare.test.toml \ No newline at end of file diff --git a/actions/serve.go b/actions/serve.go index 989d222..6e1dc1a 100644 --- a/actions/serve.go +++ b/actions/serve.go @@ -183,7 +183,7 @@ func ActionServe(c *cli.Context) error { if c.IsSet("http-addr") { httpAddr = c.String("http-addr") } - httpServer := server.NewHTTPSever(s, srvCertBytes, cfg.Server.GRPCEndpoint) + httpServer := server.NewHTTPSever(s, binaryStore, srvCertBytes, cfg.Server.GRPCEndpoint) httpServer.Logger = httpLogger httpServer.Addr = httpAddr diff --git a/server/http.go b/server/http.go index f4d4223..b906fb4 100644 --- a/server/http.go +++ b/server/http.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "time" "gitea.benny.dog/torjus/ezshare/store" @@ -14,6 +15,7 @@ import ( type HTTPServer struct { Logger *zap.SugaredLogger store store.FileStore + binaryStore store.BinaryStore serverGRPCCert []byte grpcEndpoint string @@ -24,10 +26,11 @@ type MetadataResponse struct { GRPCEndpoint string `json:"grpc_endpoint"` } -func NewHTTPSever(store store.FileStore, certBytes []byte, grpcEndpoint string) *HTTPServer { +func NewHTTPSever(store store.FileStore, binaryStore store.BinaryStore, certBytes []byte, grpcEndpoint string) *HTTPServer { srv := &HTTPServer{ Logger: zap.NewNop().Sugar(), store: store, + binaryStore: binaryStore, serverGRPCCert: certBytes, grpcEndpoint: grpcEndpoint, } @@ -36,6 +39,8 @@ func NewHTTPSever(store store.FileStore, certBytes []byte, grpcEndpoint string) r.Get("/server.pem", srv.ServerCertHandler) r.Get("/metadata", srv.MetadataHandler) r.Get("/files/{id}", srv.FileHandler) + r.Get("/b", srv.BinaryIndexHandler) + r.Get("/b/{filename}", srv.BinaryHandler) srv.Handler = r return srv @@ -48,6 +53,37 @@ func (s *HTTPServer) ServerCertHandler(w http.ResponseWriter, r *http.Request) { } s.Logger.Infow("Sent server certificate.", "remote_addr", r.RemoteAddr) } +func (s *HTTPServer) BinaryIndexHandler(w http.ResponseWriter, r *http.Request) { + binaries, err := s.binaryStore.List() + if err != nil { + WriteErrorResponse(w, http.StatusInternalServerError, "error listing binaries") + return + } + for _, bin := range binaries { + w.Write([]byte(fmt.Sprintf("%s\n", bin))) + } +} +func (s *HTTPServer) BinaryHandler(w http.ResponseWriter, r *http.Request) { + filename := chi.URLParam(r, "filename") + split := strings.Split(filename, "-") + if len(split) != 4 { + WriteErrorResponse(w, http.StatusBadRequest, "invalid filename") + return + } + version := split[1][1:] + operatingSystem := split[2] + arch := split[3] + release, err := s.binaryStore.GetBinary(version, operatingSystem, arch) + if err != nil { + WriteErrorResponse(w, http.StatusNotFound, "release not found") + return + } + if operatingSystem == "windows" { + filename = fmt.Sprintf("%s.exe", filename) + } + w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename)) + w.Write(release.Data) +} func (s *HTTPServer) MetadataHandler(w http.ResponseWriter, r *http.Request) { md := &MetadataResponse{ GRPCEndpoint: s.grpcEndpoint,