qixiaoning
2025-08-21 e38654fe9eff4562da4f18f8f018aed7879d493c
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
package routers
 
import (
    "basic.com/valib/logger.git"
    "github.com/gin-gonic/gin"
    "net/http"
    "sync"
    "vamicro/update-service/controller"
)
 
type (
    router struct {
        methods   []string
        path      string
        handles   gin.HandlersChain
    }
)
 
var (
    routines []router
)
 
func init() {
    ver := router{
        methods: []string{http.MethodGet},
        path:    "/version",
        handles: []gin.HandlerFunc{controller.GetAppVersion},
    }
 
    upload := router{
        methods: []string{http.MethodPost},
        path:    "/upload",
        handles: []gin.HandlerFunc{controller.UploadApps},
    }
 
    //files := router{
    //    methods: []string{http.MethodPost},
    //    path:    "/files",
    //    handles: []gin.HandlerFunc{controller.UploadFiles},
    //}
 
    download := router{
        methods: []string{http.MethodGet},
        path:    "/download",
        handles: []gin.HandlerFunc{controller.Download},
    }
 
    routines = append(routines,
        ver,
        upload,
        //files,
        download,
    )
}
 
func InitRouter(wg *sync.WaitGroup) *http.Server {
    r := gin.Default()
 
    for _, v := range routines {
        if len(v.methods) > 0 && len(v.handles) > 0 {
            for _, m := range v.methods {
                r.Handle(m, v.path, v.handles...)
            }
        }
    }
 
    server := &http.Server{
        Addr:              ":8080",
        Handler:           r,
    }
 
    wg.Add(1)
    go func() {
        if err := server.ListenAndServe(); nil != err {
            if err != http.ErrServerClosed {
                logger.Fatal("InitRouter failed with error:", err)
            } else {
                logger.Info("InitRouter exited by user")
            }
        }
 
        wg.Done()
    }()
 
    return server
}