-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (40 loc) · 930 Bytes
/
main.go
File metadata and controls
47 lines (40 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
)
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
}
var books = []Book{
{ID: "1", Title: "Harry Potter", Author: "J. K. Rowling"},
{ID: "2", Title: "The Lord of the Rings", Author: "J. R. R. Tolkien"},
{ID: "3", Title: "The Wizard of Oz", Author: "L. Frank Baum"},
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.GET("/books", func(c *gin.Context) {
log.Println("Handling GET /books request")
c.JSON(http.StatusOK, books)
})
// Health check route
r.GET("/hc", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
})
return r
}
// @Go Gin API
// @version 1.0
// @description This is a sample server for Your API.
// @host localhost:8080
// @BasePath /
func main() {
r := setupRouter()
log.Println("Starting server on port 8080")
r.Run(":8080")
}