Skip to content

Commit a0533d7

Browse files
Add openapi endpoint
1 parent d0d87aa commit a0533d7

File tree

5 files changed

+536
-2
lines changed

5 files changed

+536
-2
lines changed

.golangci.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
run:
2+
concurrency: 4
3+
allow-parallel-runners: true
4+
5+
# output:
6+
# formats:
7+
# - format: json
8+
# path: stderr
9+
# - format: checkstyle
10+
# path: report.xml
11+
# - format: colored-line-number

cmd/internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Load(getenv func(string) string, logger logging.Logger) (Config, error) {
1616
cfg := Config{
1717
Server: ServerConfig{
1818
AppEnv: "local",
19-
Addr: "8999",
19+
Addr: "8080",
2020
ReadTimeout: 30 * time.Second,
2121
WriteTimeout: 30 * time.Second,
2222
},

cmd/internal/routes/routes.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func RegisterRoutes(mux *http.ServeMux, logger logging.Logger) {
3232
mux.Handle("GET /api/v1/recitals/{id}/audio", streamRecitalAudioHandler(logger))
3333
mux.Handle("POST /api/v1/users", createUserHandler(logger))
3434
mux.Handle("GET /api/v1/auth", loginUserHandler(logger))
35+
mux.Handle("GET /api/v1/openapi.json", swaggerHandler(logger))
3536
}
3637

3738
func mainpageHandler(logger logging.Logger) http.Handler {
@@ -302,3 +303,21 @@ func readIntQueryParam(name string, otherwise int, w http.ResponseWriter, r *htt
302303
return value, nil
303304
}
304305
}
306+
307+
func swaggerHandler(logger logging.Logger) http.Handler {
308+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
309+
swaggerPath := "openapi.json"
310+
swaggerData, err := os.ReadFile(swaggerPath)
311+
if err != nil {
312+
logger.Err.Printf("Failed to read swagger file: %v", err)
313+
repondWithErrorMessage(w, r, "Failed to read OpenAPI specification", http.StatusInternalServerError, logger)
314+
return
315+
}
316+
317+
w.Header().Set("Content-Type", "application/json")
318+
w.WriteHeader(http.StatusOK)
319+
if _, err := w.Write(swaggerData); err != nil {
320+
logger.Err.Printf("Failed to write swagger response: %v", err)
321+
}
322+
})
323+
}

cmd/internal/routes/user_routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func createUserHandler(logger logging.Logger) http.Handler {
4343
return
4444
}
4545

46-
status := http.StatusOK
46+
status := http.StatusCreated
4747
w.WriteHeader(status)
4848
// A bit hacky way towrite override the existing context
4949
ctx = context.WithValue(ctx, constants.StatusCodeKey, status)

0 commit comments

Comments
 (0)