Add binary handler to http
This commit is contained in:
parent
75735c0e43
commit
dd1649c3ba
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.idea/*
|
||||
tmp/*
|
||||
dist/*
|
||||
ezshare.toml
|
||||
ezshare.test.toml
|
@ -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
|
||||
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user