Skip to content

Commit 962eef5

Browse files
committed
ics file endpoints finished
1 parent fa36498 commit 962eef5

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

internal/configurator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func ChiConfig() *chi.Mux {
127127
r.Get("/health", routes.HealthCheck)
128128

129129
r.Route("/api/v1", func(r chi.Router) {
130-
//r.Use(AuthMiddleware)
130+
r.Use(AuthMiddleware)
131131
r.Route("/posts", func(r chi.Router) {
132132
r.Get("/", posts.GetAllPosts)
133133
r.Get("/{postId}", posts.GetPost)
@@ -149,6 +149,7 @@ func ChiConfig() *chi.Mux {
149149

150150
r.Route("/ics", func(r chi.Router) {
151151
r.Get("/", ics.GetAllAvailableFileNames)
152+
r.Get("/{fileName}", ics.GetIcsFile)
152153
})
153154
})
154155

internal/server/routes/v1/ics/endpoint.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ func GetAllAvailableFileNames(w http.ResponseWriter, r *http.Request) {
3636
return
3737
}
3838
}
39+
40+
func GetIcsFile(w http.ResponseWriter, r *http.Request) {
41+
log := logger.GetLogger()
42+
43+
fileName := r.PathValue("fileName")
44+
if fileName == "" {
45+
log.Error().Msgf("Invalid request without fileName")
46+
http.Error(w, "Invalid request without fileName", http.StatusBadRequest)
47+
return
48+
}
49+
50+
filePath := server.FileStoragePath + "/" + fileName
51+
52+
if _, err := os.Stat(filePath); os.IsNotExist(err) {
53+
log.Error().Msgf("File %s does not exist", fileName)
54+
http.Error(w, "File does not exist", http.StatusNotFound)
55+
return
56+
}
57+
58+
w.Header().Set("Content-Type", "application/octet-stream")
59+
http.ServeFile(w, r, filePath)
60+
}

0 commit comments

Comments
 (0)