-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse-r-impl.go
77 lines (63 loc) · 1.52 KB
/
fuse-r-impl.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
/*
* Copyright (c) 2024.
* Created by Andy Pangaribuan <https://github.com/apangaribuan>.
*
* This product is protected by copyright and distributed under
* licenses restricting copying, distribution and decompilation.
* All Rights Reserved.
*/
package server
import (
"fmt"
"os"
"time"
"github.com/andypangaribuan/gmod/gm"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func (*stuServer) FuseR(restPort int, routes func(router RouterR)) {
if gm.Net.IsPortUsed(restPort) {
fmt.Printf("fuse server [restful]: port %v already in use\n", restPort)
os.Exit(100)
}
fuseFiberApp = fiber.New(fiber.Config{
JSONEncoder: gm.Json.Marshal,
JSONDecoder: gm.Json.Unmarshal,
DisableStartupMessage: true,
})
router := &stuFuseRRouter{
fiberApp: fuseFiberApp,
withAutoRecover: false,
printOnError: true,
}
routes(router)
if router.withAutoRecover {
fuseFiberApp.Use(recover.New())
}
isListenFailed := false
go func() {
tryCount := 0
maxTry := 30
time.Sleep(time.Millisecond * 100)
for {
if isListenFailed {
break
}
tryCount++
if tryCount > maxTry {
break
}
if gm.Net.IsPortUsed(restPort) {
fmt.Printf("fuse server [restful]: run at port %v\n", restPort)
break
}
time.Sleep(time.Millisecond * 100)
}
}()
err := fuseFiberApp.Listen(fmt.Sprintf(":%v", restPort))
if err != nil {
isListenFailed = true
fmt.Printf("fuse server [restful]: failed to listen on port %v\n", restPort)
os.Exit(100)
}
}