Compare commits

...

7 Commits

Author SHA1 Message Date
2739dafe3d Add list to server 2022-04-20 22:53:41 +02:00
a0c06e5615 Add list to store 2022-04-20 22:51:06 +02:00
54ff777532 Add healthcheck to Dockerfile 2022-04-20 18:59:54 +02:00
f5de660a4c Fix README name 2022-04-20 03:44:00 +02:00
76bdfd8166 Add placeholder README 2022-04-20 03:11:24 +02:00
a66d7b9be9 Add Dockerfile 2022-04-20 02:41:57 +02:00
ec4a1e8294 Fix go.mod 2022-04-20 02:39:05 +02:00
7 changed files with 49 additions and 1 deletions

13
Dockerfile Normal file
View File

@@ -0,0 +1,13 @@
FROM golang:alpine as builder
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o minipaste minipaste.go
FROM alpine:latest
RUN apk add --no-cache curl
COPY --from=builder /app/minipaste /usr/bin/minipaste
HEALTHCHECK --interval=10s --start-period=5s CMD curl --fail http://localhost:8080 || exit 1
CMD ["/usr/bin/minipaste"]

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# minipaste
TODO

2
go.mod
View File

@@ -4,4 +4,4 @@ go 1.18
require github.com/google/uuid v1.3.0
require github.com/go-chi/chi/v5 v5.0.7 // indirect
require github.com/go-chi/chi/v5 v5.0.7

View File

@@ -10,3 +10,7 @@ type ResponseIndex struct {
type ResponseAPIPost struct {
ID string `json:"id"`
}
type ResponseAPIList struct {
IDs []string `json:"ids"`
}

View File

@@ -34,6 +34,7 @@ func NewServer(s store.Store) *Server {
r.Get("/", srv.HandlerIndexGet)
r.Route("/api", func(r chi.Router) {
r.Get("/{id}", srv.HandlerAPIGet)
r.Get("/", srv.HandlerAPIList)
r.Post("/", srv.HandlerAPIPost)
})
@@ -101,6 +102,7 @@ func (s *Server) HandlerAPIPost(w http.ResponseWriter, r *http.Request) {
}
} else {
// Is not multipart
// TODO: Not working
if mediaType == "application/x-www-form-urlencoded" {
if err := r.ParseForm(); err != nil {
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)
}
}
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)
}
}

View File

@@ -8,6 +8,8 @@ import (
"github.com/google/uuid"
)
var _ Store = &MemoryStore{}
type MemoryStore struct {
data map[string][]byte
}
@@ -44,3 +46,12 @@ func (s *MemoryStore) Get(id string) (io.ReadCloser, error) {
return r, nil
}
func (s *MemoryStore) List() ([]string, error) {
var ids []string
for id := range s.data {
ids = append(ids, id)
}
return ids, nil
}

View File

@@ -11,4 +11,5 @@ type Store interface {
Add(r io.Reader) (string, error)
Delete(id string) error
Get(id string) (io.ReadCloser, error)
List() ([]string, error)
}