summaryrefslogtreecommitdiff
path: root/utils/urls/path.go
blob: e6d4ed7428d1e8aa29449f0691c518af71678e28 (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
41
42
43
44
45
46
47
48
49
50
51
package urls

import (
	"lain/types"
	"strings"

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

func Path(method types.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,
	}
}

func GetFullPath(routeName string) (string, bool) {
	registry.mutex.Lock()
	defer registry.mutex.Unlock()

	route, ok := registry.routes[routeName]
	if !ok {
		return "", false
	}

	return route.fullPath, true
}