8000 change the module registration impl · gigapi/gigapi@ec402db · GitHub
[go: up one dir, main page]

Skip to content

Commit ec402db

Browse files
committed
change the module registration impl
1 parent a6b455e commit ec402db

File tree

5 files changed

+52
-24
lines changed

5 files changed

+52
-24
lines changed

main.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,26 @@ import (
44
"fmt"
55
"github.com/gigapi/gigapi/config"
66
"github.com/gigapi/gigapi/merge"
7+
"github.com/gigapi/gigapi/modules"
78
"github.com/gigapi/gigapi/router"
89
"github.com/gigapi/gigapi/stdin"
910
"net/http"
1011
)
1112

13+
type api struct {
14+
}
15+
16+
func (a api) RegisterRoute(r *modules.Route) {
17+
router.RegisterRoute(r)
18+
}
19+
20+
func (a api) GetPathParams(r *http.Request) map[string]string {
21+
return router.GetPathParams(r)
22+
}
23+
1224
func initModules() {
1325
stdin.Init()
14-
merge.Init()
26+
merge.Init(&api{})
1527
}
1628

1729
func main() {

merge/handlers/insert_into.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import (
55
"context"
66
"github.com/gigapi/gigapi/merge/parsers"
77
"github.com/gigapi/gigapi/merge/repository"
8+
"github.com/gigapi/gigapi/modules"
89
"github.com/gigapi/gigapi/utils"
9-
"github.com/gorilla/mux"
1010
"io"
1111
"net/http"
1212
)
1313

14+
var API modules.Api
15+
1416
func getDatabase(r *http.Request) string {
1517
if db := r.URL.Query().Get("db"); db != "" {
1618
return db
1719
}
18-
vars := mux.Vars(r)
20+
vars := API.GetPathParams(r)
1921
if db, ok := vars["db"]; ok {
2022
return db
2123
}

merge/merge.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"github.com/gigapi/gigapi/merge/handlers"
66
"github.com/gigapi/gigapi/merge/repository"
77
"github.com/gigapi/gigapi/merge/utils"
8-
"github.com/gigapi/gigapi/router"
8+
"github.com/gigapi/gigapi/modules"
99
"net/http"
1010
"os"
1111
)
1212

13-
func Init() {
13+
func Init(api modules.Api) {
1414
err := os.MkdirAll(config.Config.Gigapi.Root, 0750)
1515
if err != nil {
1616
panic(err)
@@ -36,49 +36,50 @@ func Init() {
3636
panic(err)
3737
}
3838

39-
InitHandlers()
39+
InitHandlers(api)
4040
}
4141

42-
func InitHandlers() {
43-
router.RegisterRoute(&router.Route{
42+
func InitHandlers(api modules.Api) {
43+
handlers.API = api
44+
api.RegisterRoute(&modules.Route{
4445
Path: "/gigapi/create",
4546
Methods: []string{"POST"},
4647
Handler: handlers.CreateTableHandler,
4748
})
48-
router.RegisterRoute(&router.Route{
49+
api.RegisterRoute(&modules.Route{
4950
Path: "/gigapi/insert",
5051
Methods: []string{"POST"},
5152
Handler: handlers.InsertIntoHandler,
5253
})
5354

54-
router.RegisterRoute(&router.Route{
55+
api.RegisterRoute(&modules.Route{
5556
Path: "/gigapi/write/{db}",
5657
Methods: []string{"POST"},
5758
Handler: handlers.InsertIntoHandler,
5859
})
59-
router.RegisterRoute(&router.Route{
60+
api.RegisterRoute(&modules.Route{
6061
Path: "/gigapi/write",
6162
Methods: []string{"POST"},
6263
Handler: handlers.InsertIntoHandler,
6364
})
6465

6566
// InfluxDB 2+3 compatibility endpoints
66-
router.RegisterRoute(&router.Route{
67+
api.RegisterRoute(&modules.Route{
6768
Path: "/write",
6869
Methods: []string{"POST"},
6970
Handler: handlers.InsertIntoHandler,
7071
})
71-
router.RegisterRoute(&router.Route{
72+
api.RegisterRoute(&modules.Route{
7273
Path: "/api/v2/write",
7374
Methods: []string{"POST"},
7475
Handler: handlers.InsertIntoHandler,
7576
})
76-
router.RegisterRoute(&router.Route{
77+
api.RegisterRoute(&modules.Route{
7778
Path: "/api/v3/write_lp",
7879
Methods: []string{"POST"},
7980
Handler: handlers.InsertIntoHandler,
8081
})
81-
router.RegisterRoute(&router.Route{
82+
api.RegisterRoute(&modules.Route{
8283
Path: "/health",
8384
Methods: []string{"GET"},
8485
Handler: func(w http.ResponseWriter, r *http.Request) error {
@@ -89,7 +90,7 @@ func InitHandlers() {
8990
return nil
9091
},
9192
})
92-
router.RegisterRoute(&router.Route{
93+
api.RegisterRoute(&modules.Route{
9394
Path: "/ping",
9495
Methods: []string{"GET"},
9596
Handler: func(w http.ResponseWriter, r *http.Request) error {

modules/api.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package modules
2+
3+
import "net/http"
4+
5+
type Api interface {
6+
RegisterRoute(r *Route)
7+
GetPathParams(r *http.Request) map[string]string
8+
}
9+
10+
type Route struct {
11+
Path string
12+
Methods []string
13+
Handler func(w http.ResponseWriter, r *http.Request) error
14+
}

router/route.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package router
22

33
import (
4+
"github.com/gigapi/gigapi/modules"
45
"github.com/gorilla/mux"
56
"net/http"
67
)
78

8-
type Route struct {
9-
Path string
10-
Methods []string
11-
Handler func(w http.ResponseWriter, r *http.Request) error
12-
}
13-
149
func WithErrorHandle(hndl func(w http.ResponseWriter, r *http.Request) error,
1510
) func(w http.ResponseWriter, r *http.Request) {
1611
return func(w http.ResponseWriter, r *http.Request) {
@@ -22,9 +17,9 @@ func WithErrorHandle(hndl func(w http.ResponseWriter, r *http.Request) error,
2217
}
2318
}
2419

25-
var handlerRegistry []*Route = nil
20+
var handlerRegistry []*modules.Route = nil
2621

27-
func RegisterRoute(r *Route) {
22+
func RegisterRoute(r *modules.Route) {
2823
handlerRegistry = append(handlerRegistry, r)
2924
}
3025

@@ -35,3 +30,7 @@ func NewRouter() *mux.Router {
3530
}
3631
return router
3732
}
33+
34+
func GetPathParams(r *http.Request) map[string]string {
35+
return mux.Vars(r)
36+
}

0 commit comments

Comments
 (0)
0