generated from noi-techpark/java-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (139 loc) · 4.32 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import (
"encoding/xml"
"fmt"
"log/slog"
"net/http"
"opendatahub/transmodel-api/config"
"opendatahub/transmodel-api/netex"
"opendatahub/transmodel-api/provider"
"opendatahub/transmodel-api/siri"
"os"
"slices"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
sloggin "github.com/samber/slog-gin"
)
func main() {
InitLogger()
config.InitConfig()
r := gin.New()
if os.Getenv("GIN_LOG") == "PRETTY" {
r.Use(gin.Logger())
} else {
// Enable slog logging for gin framework
// https://github.com/samber/slog-gin
r.Use(sloggin.NewWithFilters(
slog.Default(),
sloggin.IgnorePath("/health", "/favicon.ico")),
)
}
r.Use(gin.Recovery()) //recover from panics
r.Use(cors.Default()) //allow all origins
r.NoMethod(docErr(http.StatusMethodNotAllowed, "Method not allowed"))
r.NoRoute(docErr(http.StatusNotFound, "Path not found"))
r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, swaggerUrl()) })
r.GET("/apispec", func(c *gin.Context) { c.File("./openapi3.yaml") })
r.GET("/netex", netexEndpoint(netexAll))
r.GET("/netex/parking", netexEndpoint(netexParking))
r.GET("/netex/sharing", netexEndpoint(netexSharing))
r.GET("/siri-lite/facility-monitoring", siriLite(siriFM))
r.GET("/siri-lite/facility-monitoring/parking", siriLite(siriFMParking))
r.GET("/siri-lite/facility-monitoring/sharing", siriLite(siriFMSharing))
r.GET("/health", health)
r.Run()
}
func swaggerUrl() string {
return fmt.Sprintf("%s?url=%s/apispec", os.Getenv("SWAGGER_URL"), apiUrl())
}
func apiUrl() string {
return os.Getenv("API_URL")
}
func health(c *gin.Context) {
c.Status(http.StatusOK)
}
func docErr(code int, msg string) func(*gin.Context) {
return func(c *gin.Context) {
c.JSON(code,
gin.H{"error": gin.H{
"message": fmt.Sprintf("%s. Please refer to our OpenAPI documentation", msg),
"openapi": gin.H{
"document": fmt.Sprintf("%s/apispec", apiUrl()),
"swagger": swaggerUrl(),
},
}},
)
}
}
type netexFn func() ([]netex.CompositeFrame, error)
func netexEndpoint(fn netexFn) func(*gin.Context) {
return func(c *gin.Context) {
comp, err := fn()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
n := netex.NewNetexFrame()
n.DataObjects.Frames = append(n.DataObjects.Frames, comp...)
prettyXML(c, http.StatusOK, n)
}
}
func netexAll() ([]netex.CompositeFrame, error) {
ret := []netex.CompositeFrame{}
for _, s := range []netexFn{netexParking, netexSharing} {
sr, err := s()
if err != nil {
return ret, err
}
ret = append(ret, sr...)
}
return ret, nil
}
func netexParking() ([]netex.CompositeFrame, error) {
return netex.GetParking(provider.ParkingStatic)
}
func netexSharing() ([]netex.CompositeFrame, error) {
return netex.GetSharing(provider.SharingBikesStatic, provider.SharingCarsStatic)
}
type siriFn func(siri.Query) (siri.Siri, error)
func siriLite(fn siriFn) func(*gin.Context) {
return func(c *gin.Context) {
query := siri.WrapQuery(c.Request.URL.Query())
res, err := fn(query)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if wantsXml(c) {
prettyXML(c, http.StatusOK, res)
} else {
c.JSONP(http.StatusOK, struct{ Siri siri.Siri }{Siri: res}) // wrap root level
}
}
}
func wantsXml(c *gin.Context) bool {
accept := c.Request.Header.Values("Accept")
return (slices.Contains(accept, "application/xml") &&
!slices.Contains(accept, "application/json")) ||
c.Query("format") == "xml"
}
func siriFM(query siri.Query) (siri.Siri, error) {
return siri.FM(append(provider.ParkingRt, provider.SharingRt...), query)
}
func siriFMParking(query siri.Query) (siri.Siri, error) {
return siri.FM(provider.ParkingRt, query)
}
func siriFMSharing(query siri.Query) (siri.Siri, error) {
return siri.FM(provider.SharingRt, query)
}
func prettyXML(c *gin.Context, code int, object any) {
// Due to a request, this renders the xml in a pretty format.
// Once this is production ready, should probably switch back to just c.XML(...)
data, err := xml.MarshalIndent(object, "", " ")
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
c.Render(code, render.Data{Data: data, ContentType: "application/xml; charset=utf-8"})
}