├── go.sum ├── README.md ├── go.mod ├── .gitignore ├── interfaces ├── dto.go ├── entity.go └── store.go ├── entities └── flashcard.go ├── instances └── store.go ├── dtos └── flashcard.go ├── middlewares └── default.go ├── serializers └── default.go ├── stores └── default.go ├── main.go ├── LICENSE └── handlers └── flashcard.go /go.sum: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/elbiseu/flashcards 2 | 3 | go 1.22 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ID config 2 | .idea/ 3 | 4 | # OS files 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /interfaces/dto.go: -------------------------------------------------------------------------------- 1 | package interfaces 2 | 3 | type DTO interface { 4 | ToMap() map[string]any 5 | } 6 | -------------------------------------------------------------------------------- /interfaces/entity.go: -------------------------------------------------------------------------------- 1 | package interfaces 2 | 3 | type Entity interface { 4 | Container() string 5 | } 6 | -------------------------------------------------------------------------------- /interfaces/store.go: -------------------------------------------------------------------------------- 1 | package interfaces 2 | 3 | type Store interface { 4 | Put(entity Entity) error 5 | Get(entity Entity) error 6 | Remove(entity Entity) error 7 | } 8 | -------------------------------------------------------------------------------- /entities/flashcard.go: -------------------------------------------------------------------------------- 1 | package entities 2 | 3 | type Flashcard struct { 4 | Key string 5 | Type string 6 | Value string 7 | } 8 | 9 | func (f *Flashcard) Container() string { 10 | return "flashcard" 11 | } 12 | -------------------------------------------------------------------------------- /instances/store.go: -------------------------------------------------------------------------------- 1 | package instances 2 | 3 | import ( 4 | "github.com/elbiseu/flashcards/interfaces" 5 | "github.com/elbiseu/flashcards/stores" 6 | ) 7 | 8 | var Store interfaces.Store 9 | 10 | func init() { 11 | Store = stores.NewDefaultStore() 12 | } 13 | -------------------------------------------------------------------------------- /dtos/flashcard.go: -------------------------------------------------------------------------------- 1 | package dtos 2 | 3 | type Flashcard struct { 4 | Key string 5 | Type string 6 | Value string 7 | } 8 | 9 | func (f *Flashcard) ToMap() map[string]any { 10 | return map[string]any{ 11 | "key": f.Key, 12 | "type": f.Type, 13 | "value": f.Value, 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /middlewares/default.go: -------------------------------------------------------------------------------- 1 | package middlewares 2 | 3 | import ( 4 | "net/http" 5 | ) 6 | 7 | func DefaultMiddleware(handler http.HandlerFunc) http.HandlerFunc { 8 | return func(w http.ResponseWriter, r *http.Request) { 9 | defer func() { 10 | if err := recover(); err != nil { 11 | http.Error(w, "Internal server error", http.StatusInternalServerError) 12 | return 13 | } 14 | }() 15 | handler(w, r) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /serializers/default.go: -------------------------------------------------------------------------------- 1 | package serializers 2 | 3 | import ( 4 | "encoding/json" 5 | "github.com/elbiseu/flashcards/interfaces" 6 | ) 7 | 8 | type DefaultSerializer struct { 9 | dto interfaces.DTO 10 | } 11 | 12 | func NewDefaultSerializer(dto interfaces.DTO) *DefaultSerializer { 13 | return &DefaultSerializer{dto: dto} 14 | } 15 | 16 | func (ds *DefaultSerializer) Serialize() ([]byte, error) { 17 | return json.Marshal(ds.dto.ToMap()) 18 | } 19 | -------------------------------------------------------------------------------- /stores/default.go: -------------------------------------------------------------------------------- 1 | package stores 2 | 3 | import ( 4 | "github.com/elbiseu/flashcards/interfaces" 5 | ) 6 | 7 | type DefaultStore struct { 8 | } 9 | 10 | func NewDefaultStore() *DefaultStore { 11 | return &DefaultStore{} 12 | } 13 | 14 | func (ds DefaultStore) Put(entity interfaces.Entity) error { 15 | return nil 16 | } 17 | 18 | func (ds DefaultStore) Get(entity interfaces.Entity) error { 19 | return nil 20 | } 21 | 22 | func (ds DefaultStore) Remove(entity interfaces.Entity) error { 23 | return nil 24 | } 25 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/elbiseu/flashcards/handlers" 6 | "github.com/elbiseu/flashcards/middlewares" 7 | "net/http" 8 | ) 9 | 10 | func main() { 11 | serveMux := http.NewServeMux() 12 | serveMux.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static")))) 13 | serveMux.HandleFunc("/flashcard/{key}", middlewares.DefaultMiddleware(handlers.Flashcard)) 14 | if err := http.ListenAndServe(":8080", serveMux); err != nil { 15 | fmt.Printf("The service needs help getting up and running: %v\n", err) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Elvis Sánchez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /handlers/flashcard.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "encoding/json" 5 | "github.com/elbiseu/flashcards/dtos" 6 | "github.com/elbiseu/flashcards/entities" 7 | "github.com/elbiseu/flashcards/instances" 8 | "github.com/elbiseu/flashcards/serializers" 9 | "net/http" 10 | ) 11 | 12 | func Flashcard(w http.ResponseWriter, r *http.Request) { 13 | switch r.Method { 14 | case http.MethodGet: 15 | key := r.PathValue("key") 16 | flashcardEntity := &entities.Flashcard{ 17 | Key: key, 18 | } 19 | if err := instances.Store.Get(flashcardEntity); err != nil { 20 | http.Error(w, err.Error(), http.StatusInternalServerError) 21 | return 22 | } 23 | flashcardDTO := &dtos.Flashcard{ 24 | Key: flashcardEntity.Key, 25 | Type: flashcardEntity.Type, 26 | Value: flashcardEntity.Value, 27 | } 28 | b, err := serializers.NewDefaultSerializer(flashcardDTO).Serialize() 29 | if err != nil { 30 | http.Error(w, err.Error(), http.StatusInternalServerError) 31 | return 32 | } 33 | if _, err := w.Write(b); err != nil { 34 | http.Error(w, err.Error(), http.StatusInternalServerError) 35 | return 36 | } 37 | case http.MethodPost: 38 | flashcardDTO := &dtos.Flashcard{} 39 | if err := json.NewDecoder(r.Body).Decode(flashcardDTO); err != nil { 40 | http.Error(w, err.Error(), http.StatusInternalServerError) 41 | return 42 | } 43 | flashcardEntity := &entities.Flashcard{ 44 | Key: flashcardDTO.Key, 45 | Type: flashcardDTO.Type, 46 | Value: flashcardDTO.Value, 47 | } 48 | if err := instances.Store.Put(flashcardEntity); err != nil { 49 | http.Error(w, err.Error(), http.StatusInternalServerError) 50 | return 51 | } 52 | w.WriteHeader(http.StatusCreated) 53 | default: 54 | w.WriteHeader(http.StatusNotImplemented) 55 | } 56 | } 57 | --------------------------------------------------------------------------------