summaryrefslogtreecommitdiffstats
path: root/api/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'api/handlers')
-rw-r--r--api/handlers/html.go27
-rw-r--r--api/handlers/record.go35
2 files changed, 62 insertions, 0 deletions
diff --git a/api/handlers/html.go b/api/handlers/html.go
new file mode 100644
index 0000000..dd5d4e8
--- /dev/null
+++ b/api/handlers/html.go
@@ -0,0 +1,27 @@
+package handlers
+
+import (
+ "net/http"
+
+ "git.leonardobishop.net/history/pkg/entries"
+ "git.leonardobishop.net/history/pkg/html"
+)
+
+func GetEntriesHtml(entriesService entries.Service, htmlService html.Service) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ entries, err := entriesService.GetEntries()
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ html, err := htmlService.GenerateHtml(entries)
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "text/html;charset=UTF-8")
+ w.Write([]byte(html))
+ }
+}
diff --git a/api/handlers/record.go b/api/handlers/record.go
new file mode 100644
index 0000000..0f98f11
--- /dev/null
+++ b/api/handlers/record.go
@@ -0,0 +1,35 @@
+package handlers
+
+import (
+ "crypto/subtle"
+ "net/http"
+
+ "git.leonardobishop.net/history/api/dto"
+ "git.leonardobishop.net/history/pkg/entries"
+)
+
+func RecordEntry(service entries.Service, token string) http.HandlerFunc {
+ return dto.WrapResponseFunc(func(w http.ResponseWriter, r *http.Request) error {
+ var request dto.CreateEntryRequest
+ if err := dto.ReadDto(r, &request); err != nil {
+ return err
+ }
+
+ if subtle.ConstantTimeCompare([]byte(token), []byte(request.Token)) != 1 {
+ return &dto.ErrorResponse{
+ Code: http.StatusForbidden,
+ Message: "Forbidden",
+ }
+ }
+
+ entry, err := service.CreateEntry(request.Title, request.Kind, request.Url, request.Description)
+ if err != nil {
+ return err
+ }
+
+ return &dto.OkResponse{
+ Code: http.StatusCreated,
+ Data: entry,
+ }
+ })
+}