Compare commits
	
		
			2 Commits
		
	
	
		
			54ff777532
			...
			2739dafe3d
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 2739dafe3d | |||
| a0c06e5615 | 
@@ -10,3 +10,7 @@ type ResponseIndex struct {
 | 
				
			|||||||
type ResponseAPIPost struct {
 | 
					type ResponseAPIPost struct {
 | 
				
			||||||
	ID string `json:"id"`
 | 
						ID string `json:"id"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ResponseAPIList struct {
 | 
				
			||||||
 | 
						IDs []string `json:"ids"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,6 +34,7 @@ func NewServer(s store.Store) *Server {
 | 
				
			|||||||
	r.Get("/", srv.HandlerIndexGet)
 | 
						r.Get("/", srv.HandlerIndexGet)
 | 
				
			||||||
	r.Route("/api", func(r chi.Router) {
 | 
						r.Route("/api", func(r chi.Router) {
 | 
				
			||||||
		r.Get("/{id}", srv.HandlerAPIGet)
 | 
							r.Get("/{id}", srv.HandlerAPIGet)
 | 
				
			||||||
 | 
							r.Get("/", srv.HandlerAPIList)
 | 
				
			||||||
		r.Post("/", srv.HandlerAPIPost)
 | 
							r.Post("/", srv.HandlerAPIPost)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -101,6 +102,7 @@ func (s *Server) HandlerAPIPost(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// Is not multipart
 | 
							// Is not multipart
 | 
				
			||||||
 | 
							// TODO: Not working
 | 
				
			||||||
		if mediaType == "application/x-www-form-urlencoded" {
 | 
							if mediaType == "application/x-www-form-urlencoded" {
 | 
				
			||||||
			if err := r.ParseForm(); err != nil {
 | 
								if err := r.ParseForm(); err != nil {
 | 
				
			||||||
				log.Printf("Error parsing form: %s", err)
 | 
									log.Printf("Error parsing form: %s", err)
 | 
				
			||||||
@@ -145,3 +147,17 @@ func (s *Server) HandlerAPIGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		log.Panicf("Error writing to client: %s", err)
 | 
							log.Panicf("Error writing to client: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *Server) HandlerAPIList(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						ids, err := s.store.List()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Panicf("Error listing store contents: %s", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resp := &ResponseAPIList{IDs: ids}
 | 
				
			||||||
 | 
						encoder := json.NewEncoder(w)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := encoder.Encode(resp); err != nil {
 | 
				
			||||||
 | 
							log.Panicf("Error encoding response to client: %s", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,8 @@ import (
 | 
				
			|||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var _ Store = &MemoryStore{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MemoryStore struct {
 | 
					type MemoryStore struct {
 | 
				
			||||||
	data map[string][]byte
 | 
						data map[string][]byte
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -44,3 +46,12 @@ func (s *MemoryStore) Get(id string) (io.ReadCloser, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return r, nil
 | 
						return r, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *MemoryStore) List() ([]string, error) {
 | 
				
			||||||
 | 
						var ids []string
 | 
				
			||||||
 | 
						for id := range s.data {
 | 
				
			||||||
 | 
							ids = append(ids, id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return ids, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,4 +11,5 @@ type Store interface {
 | 
				
			|||||||
	Add(r io.Reader) (string, error)
 | 
						Add(r io.Reader) (string, error)
 | 
				
			||||||
	Delete(id string) error
 | 
						Delete(id string) error
 | 
				
			||||||
	Get(id string) (io.ReadCloser, error)
 | 
						Get(id string) (io.ReadCloser, error)
 | 
				
			||||||
 | 
						List() ([]string, error)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user