-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (64 loc) · 1.99 KB
/
main.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/twinj/uuid"
"log"
"net/http"
"pittapi/controllers"
_ "pittapi/docs"
"time"
)
func RequestIDMiddleware() gin.HandlerFunc {
return func(context *gin.Context) {
uuid := uuid.NewV4()
context.Writer.Header().Add("X-Request-ID", uuid.String())
context.Next() // goes to next middleware
}
}
// @title University of Pittsburgh API
// @description This is a sample server for Pitt API.
// @version 1.0.0
// @BasePath /v1/api
// @host 0.0.0.0:8080
// @schemes http https
// @name University of Pittsburgh API
// @description Github: https://github.com/day-mon/Pitt-API-Final. [v1 and v2 is available through same endpoints]
func main() {
const VERSION = "1.0.3"
router := gin.New()
router.Use(gzip.Gzip(gzip.DefaultCompression))
router.Use(RequestIDMiddleware())
router.Use(gin.Logger())
// @Summary Simple health check
// @Description Simple health check
// @Tags Health
// @Accept json
// @Produce json
// @Success 200 {object} map[string]string
// @Router /health [get]
router.GET("/health", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"healthy": "true",
})
})
store := persistence.NewInMemoryStore(time.Second)
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.NoRoute(func(context *gin.Context) {
context.Redirect(http.StatusMovedPermanently, "/swagger/index.html")
})
v1 := router.Group("/v1/api")
{
cc := new(controllers.CourseController)
v1.POST("/courses/info", cc.GetCourseInfo)
v1.GET("/course/:term/:number", cache.CachePage(store, time.Minute, cc.GetCourse))
lc := new(controllers.LaundryController)
v1.GET("/laundry/:dormitory", cache.CachePage(store, time.Minute, lc.GetByDormitory))
}
log.Println("Running Pitt API version " + VERSION)
router.Run(":8080")
}