summaryrefslogtreecommitdiff
path: root/shrine/utils/urls/path.go
blob: 3a552ae18d3bad3b5acb6f5c13b5d27a79c540b7 (plain)
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
package urls

import (
	"shrine/enums"
	"strings"

	"github.com/gofiber/fiber/v2"
)

func Path(method enums.HTTPMethod, path string, handler fiber.Handler, name string) {
	registry.mutex.Lock()
	defer registry.mutex.Unlock()

	namespace := registry.currentNamespace
	fullName := name
	fullPath := path

	if namespace != "" {
		if !strings.HasPrefix(path, "/") {
			path = "/" + path
		}

		fullName = namespace + "." + name
		fullPath = "/" + namespace + path
	} else {
		if !strings.HasPrefix(fullPath, "/") {
			fullPath = "/" + fullPath
		}
	}

	registry.routes[fullName] = registeredRoute{
		method:    method,
		path:      path,
		handler:   handler,
		namespace: namespace,
		name:      name,
		fullPath:  fullPath,
	}
}