2021-12-03 22:04:09 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"gitea.benny.dog/torjus/ezshare/store"
|
|
|
|
"github.com/go-chi/chi/v5"
|
2021-12-06 06:55:30 +00:00
|
|
|
"go.uber.org/zap"
|
2021-12-03 22:04:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPServer struct {
|
2021-12-06 06:55:30 +00:00
|
|
|
Logger *zap.SugaredLogger
|
2021-12-06 05:08:17 +00:00
|
|
|
store store.FileStore
|
|
|
|
serverGRPCCert []byte
|
|
|
|
grpcEndpoint string
|
2021-12-03 22:04:09 +00:00
|
|
|
|
|
|
|
http.Server
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:08:17 +00:00
|
|
|
type MetadataResponse struct {
|
|
|
|
GRPCEndpoint string `json:"grpc_endpoint"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPSever(store store.FileStore, certBytes []byte, grpcEndpoint string) *HTTPServer {
|
2021-12-03 22:04:09 +00:00
|
|
|
srv := &HTTPServer{
|
2021-12-06 06:55:30 +00:00
|
|
|
Logger: zap.NewNop().Sugar(),
|
2021-12-06 05:08:17 +00:00
|
|
|
store: store,
|
|
|
|
serverGRPCCert: certBytes,
|
|
|
|
grpcEndpoint: grpcEndpoint,
|
2021-12-03 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r := chi.NewRouter()
|
2021-12-06 05:08:17 +00:00
|
|
|
r.Get("/server.pem", srv.ServerCertHandler)
|
|
|
|
r.Get("/metadata", srv.MetadataHandler)
|
2021-12-03 22:04:09 +00:00
|
|
|
r.Get("/files/{id}", srv.FileHandler)
|
|
|
|
|
|
|
|
srv.Handler = r
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:08:17 +00:00
|
|
|
func (s *HTTPServer) ServerCertHandler(w http.ResponseWriter, r *http.Request) {
|
2021-12-06 06:55:30 +00:00
|
|
|
if _, err := w.Write(s.serverGRPCCert); err != nil {
|
|
|
|
s.Logger.Warnw("Error sending server certificate.", "error", err, "remote_addr", r.RemoteAddr)
|
|
|
|
|
|
|
|
}
|
|
|
|
s.Logger.Infow("Sent server certificate.", "remote_addr", r.RemoteAddr)
|
2021-12-06 05:08:17 +00:00
|
|
|
}
|
|
|
|
func (s *HTTPServer) MetadataHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
md := &MetadataResponse{
|
|
|
|
GRPCEndpoint: s.grpcEndpoint,
|
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(w)
|
2021-12-06 06:55:30 +00:00
|
|
|
if err := encoder.Encode(md); err != nil {
|
|
|
|
s.Logger.Warnw("Error encoding or sending metadata.", "error", err, "remote_addr", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
s.Logger.Infow("Wrote server cert.", "remote_addr", r.RemoteAddr)
|
2021-12-06 05:08:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 22:04:09 +00:00
|
|
|
func (s *HTTPServer) FileHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := chi.URLParam(r, "id")
|
|
|
|
f, err := s.store.GetFile(id)
|
|
|
|
if err != nil {
|
2021-12-05 13:55:18 +00:00
|
|
|
if err == store.ErrNoSuchItem {
|
2021-12-06 06:55:30 +00:00
|
|
|
s.Logger.Debugw("Tried to get non-existing file.", "remote_addr", r.RemoteAddr)
|
2021-12-03 22:04:09 +00:00
|
|
|
WriteErrorResponse(w, http.StatusNotFound, "file not found")
|
|
|
|
return
|
|
|
|
}
|
2021-12-06 06:55:30 +00:00
|
|
|
s.Logger.Warnw("Error getting file from store.", "error", err, "remote_addr", r.RemoteAddr)
|
2021-12-03 22:04:09 +00:00
|
|
|
WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", http.DetectContentType(f.Data))
|
2021-12-06 06:55:30 +00:00
|
|
|
if _, err := w.Write(f.Data); err != nil {
|
|
|
|
s.Logger.Warnw("Error sending file.", "error", err, "remote_addr", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
s.Logger.Infow("Sent file.", "remote_addr", r.RemoteAddr)
|
2021-12-03 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func WriteErrorResponse(w http.ResponseWriter, status int, message string) {
|
|
|
|
errMessage := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Error: message,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
encoder.Encode(&errMessage)
|
|
|
|
}
|