feat: Implement Swagger documentation and integrate Gin framework for API
This commit is contained in:
+26
-4
@@ -3,14 +3,36 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
|
||||
_ "github.com/timetracker/backend/docs" // This line is important for swag to work
|
||||
)
|
||||
|
||||
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Hello from the Time Tracker Backend!")
|
||||
// @title Time Tracker API
|
||||
// @version 1.0
|
||||
// @description This is a simple time tracker API.
|
||||
// @host localhost:8080
|
||||
// @BasePath /
|
||||
|
||||
// @Summary Say hello
|
||||
// @Description Get a hello message
|
||||
// @ID hello
|
||||
// @Produce plain
|
||||
// @Success 200 {string} string "Hello from the Time Tracker Backend!"
|
||||
// @Router / [get]
|
||||
func helloHandler(c *gin.Context) {
|
||||
c.String(http.StatusOK, "Hello from the Time Tracker Backend!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", helloHandler)
|
||||
r := gin.Default()
|
||||
|
||||
r.GET("/", helloHandler)
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
fmt.Println("Server listening on port 8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
r.Run(":8080") // Use Gin's Run method
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user