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 }