MCPcopy
hub / github.com/GopeedLab/gopeed / BuildServer

Function BuildServer

pkg/rest/server.go:82–261  ·  view source on GitHub ↗
(startCfg *model.StartConfig)

Source from the content-addressed store, hash-verified

80}
81
82func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error) {
83 if startCfg == nil {
84 startCfg = &model.StartConfig{}
85 }
86 startCfg.Init()
87
88 downloadCfg := &download.DownloaderConfig{
89 ProductionMode: startCfg.ProductionMode,
90 RefreshInterval: startCfg.RefreshInterval,
91 WhiteDownloadDirs: startCfg.WhiteDownloadDirs,
92 }
93 if startCfg.Storage == model.StorageBolt {
94 downloadCfg.Storage = download.NewBoltStorage(startCfg.StorageDir)
95 } else {
96 downloadCfg.Storage = download.NewMemStorage()
97 }
98 downloadCfg.StorageDir = startCfg.StorageDir
99 downloadCfg.Init()
100 Downloader = download.NewDownloader(downloadCfg)
101 if err := Downloader.Setup(); err != nil {
102 return nil, nil, err
103 }
104
105 if startCfg.Network == "unix" {
106 util.SafeRemove(startCfg.Address)
107 }
108
109 if startCfg.WebEnable {
110 aesKey = make([]byte, 32)
111 if _, err := rand.Read(aesKey); err != nil {
112 return nil, nil, errors.Wrap(err, "generate aes key failed")
113 }
114 }
115
116 listener, err := net.Listen(startCfg.Network, startCfg.Address)
117 if err != nil {
118 return nil, nil, err
119 }
120
121 var r = mux.NewRouter()
122 r.Methods(http.MethodGet).Path("/api/v1/info").HandlerFunc(Info)
123 r.Methods(http.MethodPost).Path("/api/v1/resolve").HandlerFunc(Resolve)
124 r.Methods(http.MethodPost).Path("/api/v1/tasks").HandlerFunc(CreateTask)
125 r.Methods(http.MethodPost).Path("/api/v1/tasks/batch").HandlerFunc(CreateTaskBatch)
126 r.Methods(http.MethodPatch).Path("/api/v1/tasks/{id}").HandlerFunc(PatchTask)
127 r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/pause").HandlerFunc(PauseTask)
128 r.Methods(http.MethodPut).Path("/api/v1/tasks/pause").HandlerFunc(PauseTasks)
129 r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/continue").HandlerFunc(ContinueTask)
130 r.Methods(http.MethodPut).Path("/api/v1/tasks/continue").HandlerFunc(ContinueTasks)
131 r.Methods(http.MethodDelete).Path("/api/v1/tasks/{id}").HandlerFunc(DeleteTask)
132 r.Methods(http.MethodDelete).Path("/api/v1/tasks").HandlerFunc(DeleteTasks)
133 r.Methods(http.MethodGet).Path("/api/v1/tasks/{id}").HandlerFunc(GetTask)
134 r.Methods(http.MethodGet).Path("/api/v1/tasks").HandlerFunc(GetTasks)
135 r.Methods(http.MethodGet).Path("/api/v1/tasks/{id}/stats").HandlerFunc(GetStats)
136 r.Methods(http.MethodGet).Path("/api/v1/config").HandlerFunc(GetConfig)
137 r.Methods(http.MethodPut).Path("/api/v1/config").HandlerFunc(PutConfig)
138 r.Methods(http.MethodPost).Path("/api/v1/extensions").HandlerFunc(InstallExtension)
139 r.Methods(http.MethodGet).Path("/api/v1/extensions").HandlerFunc(GetExtensions)

Callers 2

StartFunction · 0.92
StartFunction · 0.85

Calls 15

InitMethod · 0.95
NewBoltStorageFunction · 0.92
NewMemStorageFunction · 0.92
NewDownloaderFunction · 0.92
SafeRemoveFunction · 0.92
NewErrorResultFunction · 0.92
NewOkResultFunction · 0.92
ReadJsonFunction · 0.85
aesEncryptFunction · 0.85
WriteJsonFunction · 0.85
WriteStatusJsonFunction · 0.85
gzipMiddlewareFunction · 0.85

Tested by

no test coverage detected