Skip to content

Commit fcf052d

Browse files
committed
feat: default routes added
1 parent 8809389 commit fcf052d

5 files changed

Lines changed: 48 additions & 2 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ down:
122122

123123
up-dev:
124124
@echo "🐳 Starting Docker Compose For Development..."
125-
@docker compose -f $(COMPOSE_FILE) --profile dev up -d --build
125+
@docker compose -f $(COMPOSE_FILE) --profile dev up --build
126126

127127
down-dev:
128128
@echo "🛑 Stopping Docker Compose For Development..."

handlers/auth.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"time"
77

8-
// "knowledge-capsule-api/middleware"
98
"knowledge-capsule-api/models"
109
"knowledge-capsule-api/store"
1110
"knowledge-capsule-api/utils"

handlers/default.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
func RootHandler(w http.ResponseWriter, r *http.Request) {
9+
json.NewEncoder(w).Encode(map[string]string{
10+
"message": "Welcome to Knowledge Capsule API",
11+
"status": "ok",
12+
})
13+
}
14+
15+
func ApiRootHandler(w http.ResponseWriter, r *http.Request) {
16+
json.NewEncoder(w).Encode(map[string]string{
17+
"message": "Knowledge Capsule API Root",
18+
"version": "v1",
19+
})
20+
}
21+
22+
func HealthHandler(w http.ResponseWriter, r *http.Request) {
23+
json.NewEncoder(w).Encode(map[string]string{
24+
"status": "healthy",
25+
"service": "knowledge-capsule-api",
26+
})
27+
}

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ import (
77

88
"knowledge-capsule-api/handlers"
99
"knowledge-capsule-api/middleware"
10+
"knowledge-capsule-api/utils"
1011
)
1112

1213
func main() {
1314
mux := http.NewServeMux()
1415

16+
// Default routes
17+
mux.HandleFunc("/", utils.AllowMethod(http.MethodGet, handlers.RootHandler))
18+
mux.HandleFunc("/api", utils.AllowMethod(http.MethodGet, handlers.ApiRootHandler))
19+
mux.HandleFunc("/health", utils.AllowMethod(http.MethodGet, handlers.HealthHandler))
20+
1521
// Public routes
1622
mux.HandleFunc("/api/auth/register", handlers.RegisterHandler)
1723
mux.HandleFunc("/api/auth/login", handlers.LoginHandler)

utils/method.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package utils
2+
3+
import "net/http"
4+
5+
func AllowMethod(method string, h http.HandlerFunc) http.HandlerFunc {
6+
return func(w http.ResponseWriter, r *http.Request) {
7+
if r.Method != method {
8+
w.Header().Set("Allow", method)
9+
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
10+
return
11+
}
12+
h(w, r)
13+
}
14+
}

0 commit comments

Comments
 (0)