From db7d0ed8f6c7351177d18d6e4dcd73249d602ede Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Steffen=20S=C3=A6ther?= <steffels@stud.ntnu.no>
Date: Wed, 6 Mar 2024 19:42:03 +0000
Subject: [PATCH] Upload New File

---
 main.go | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 main.go

diff --git a/main.go b/main.go
new file mode 100644
index 0000000..e382d2f
--- /dev/null
+++ b/main.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+	"fmt"
+	"main/endpoints"
+	"net/http"
+)
+
+// EndpointHandler defines the structure of handlers.
+type EndpointHandler func(http.ResponseWriter, *http.Request)
+
+func main() {
+	http.HandleFunc("/librarystats/v1/bookcount/", makeHandler(endpoints.BookCountHandler))
+	http.HandleFunc("/librarystats/v1/readership/", makeHandler(endpoints.ReadershipHandler))
+	http.HandleFunc("/librarystats/v1/status/", makeHandler(endpoints.StatusHandler))
+	http.HandleFunc("/", makeHandler(endpoints.Errorhandler))
+
+	fmt.Println("Server is running on :8080...")
+	http.ListenAndServe(":8080", nil)
+}
+
+// makeHandler is a wrapper to handle panics and set common headers.
+func makeHandler(handler EndpointHandler) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		defer func() {
+			if err := recover(); err != nil {
+				http.Error(w, "Internal Server Error", http.StatusInternalServerError)
+			}
+		}()
+
+		w.Header().Set("Content-Type", "application/json")
+		handler(w, r)
+	}
+}
-- 
GitLab