-
Notifications
You must be signed in to change notification settings - Fork 7
/
route.go
128 lines (106 loc) · 3.01 KB
/
route.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
package gin
import (
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gookit/color"
"github.com/goravel/framework/contracts/config"
httpcontract "github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/support"
)
type Route struct {
route.Route
config config.Config
instance *gin.Engine
}
func NewRoute(config config.Config) *Route {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
if debugLog := getDebugLog(config); debugLog != nil {
engine.Use(debugLog)
}
return &Route{
Route: NewGroup(engine.Group("/"),
"",
[]httpcontract.Middleware{},
[]httpcontract.Middleware{ResponseMiddleware()},
),
config: config,
instance: engine,
}
}
func (r *Route) Fallback(handler httpcontract.HandlerFunc) {
r.instance.NoRoute(handlerToGinHandler(handler))
}
func (r *Route) GlobalMiddleware(middlewares ...httpcontract.Middleware) {
if len(middlewares) > 0 {
r.instance.Use(middlewaresToGinHandlers(middlewares)...)
}
r.Route = NewGroup(
r.instance.Group("/"),
"",
[]httpcontract.Middleware{},
[]httpcontract.Middleware{ResponseMiddleware()},
)
}
func (r *Route) Run(host ...string) error {
if len(host) == 0 {
defaultHost := r.config.GetString("http.host")
if defaultHost == "" {
return errors.New("host can't be empty")
}
defaultPort := r.config.GetString("http.port")
if defaultPort == "" {
return errors.New("port can't be empty")
}
completeHost := defaultHost + ":" + defaultPort
host = append(host, completeHost)
}
r.outputRoutes()
color.Greenln("[HTTP] Listening and serving HTTP on " + host[0])
server := &http.Server{
Addr: host[0],
Handler: http.AllowQuerySemicolons(r.instance),
}
return server.ListenAndServe()
}
func (r *Route) RunTLS(host ...string) error {
if len(host) == 0 {
defaultHost := r.config.GetString("http.tls.host")
if defaultHost == "" {
return errors.New("host can't be empty")
}
defaultPort := r.config.GetString("http.tls.port")
if defaultPort == "" {
return errors.New("port can't be empty")
}
completeHost := defaultHost + ":" + defaultPort
host = append(host, completeHost)
}
certFile := r.config.GetString("http.tls.ssl.cert")
keyFile := r.config.GetString("http.tls.ssl.key")
return r.RunTLSWithCert(host[0], certFile, keyFile)
}
func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
if host == "" {
return errors.New("host can't be empty")
}
if certFile == "" || keyFile == "" {
return errors.New("certificate can't be empty")
}
r.outputRoutes()
color.Greenln("[HTTPS] Listening and serving HTTPS on " + host)
return r.instance.RunTLS(host, certFile, keyFile)
}
func (r *Route) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
r.instance.ServeHTTP(writer, request)
}
func (r *Route) outputRoutes() {
if r.config.GetBool("app.debug") && support.Env != support.EnvArtisan {
for _, item := range r.instance.Routes() {
fmt.Printf("%-10s %s\n", item.Method, colonToBracket(item.Path))
}
}
}