mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2026-03-13 10:12:40 +08:00
chore (maintenance): refactoring of the starter plugin
This commit is contained in:
15
cmd/main.go
15
cmd/main.go
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/mickael-kerjean/filestash"
|
||||
@@ -11,9 +10,9 @@ import (
|
||||
"github.com/mickael-kerjean/filestash/server/model"
|
||||
|
||||
. "github.com/mickael-kerjean/filestash/server/common"
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin"
|
||||
_ "github.com/mickael-kerjean/filestash/server/pkg"
|
||||
"github.com/mickael-kerjean/filestash/server/pkg/workflow"
|
||||
_ "github.com/mickael-kerjean/filestash/server/plugin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -26,7 +25,7 @@ func Run(router *mux.Router) {
|
||||
check(workflow.Init(), "Worklow Initialisation failure. err=%s")
|
||||
check(model.PluginDiscovery(), "Plugin Discovery failed. err=%s")
|
||||
check(ctrl.InitPluginList(embed.EmbedPluginList, model.PLUGINS), "Plugin Initialisation failed. err=%s")
|
||||
if len(Hooks.Get.Starter()) == 0 {
|
||||
if Hooks.Get.Starter() == nil {
|
||||
check(ErrNotFound, "Missing starter plugin. err=%s")
|
||||
}
|
||||
for _, fn := range Hooks.Get.Onload() {
|
||||
@@ -41,15 +40,7 @@ func Run(router *mux.Router) {
|
||||
server.DebugRoutes(router)
|
||||
}
|
||||
server.CatchAll(router)
|
||||
var wg sync.WaitGroup
|
||||
for _, obj := range Hooks.Get.Starter() {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
obj(router)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
Hooks.Get.Starter()(router)
|
||||
}
|
||||
|
||||
func check(err error, msg string) {
|
||||
|
||||
@@ -104,12 +104,12 @@ func (this Register) Static(www fs.FS, chroot string) {
|
||||
* - plg_started_http2 to create an HTTP2 server
|
||||
* - ...
|
||||
*/
|
||||
var starter_process []func(*mux.Router)
|
||||
var starter_process func(*mux.Router)
|
||||
|
||||
func (this Register) Starter(fn func(*mux.Router)) {
|
||||
starter_process = append(starter_process, fn)
|
||||
starter_process = fn
|
||||
}
|
||||
func (this Get) Starter() []func(*mux.Router) {
|
||||
func (this Get) Starter() func(*mux.Router) {
|
||||
return starter_process
|
||||
}
|
||||
|
||||
|
||||
@@ -11,27 +11,28 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
Hooks.Register.Starter(func(r *mux.Router) {
|
||||
Log.Info("[http] starting ...")
|
||||
port := Config.Get("general.port").Int()
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: r,
|
||||
}
|
||||
go ensureAppHasBooted(
|
||||
fmt.Sprintf("http://127.0.0.1:%d%s", port, WithBase("/about")),
|
||||
fmt.Sprintf("[http] listening on :%d", port),
|
||||
)
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
Log.Error("error: %v", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
Hooks.Register.Starter(Start)
|
||||
}
|
||||
|
||||
func Start(r *mux.Router) {
|
||||
Log.Info("[http] starting ...")
|
||||
port := Config.Get("general.port").Int()
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: r,
|
||||
}
|
||||
go ensureAppHasBooted(
|
||||
fmt.Sprintf("http://127.0.0.1:%d%s", port, WithBase("/about")),
|
||||
fmt.Sprintf("[http] listening on :%d", port),
|
||||
)
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
Log.Error("error: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ensureAppHasBooted(address string, message string) {
|
||||
i := 0
|
||||
for {
|
||||
for i := 0; i > 10; i++ {
|
||||
if i > 10 {
|
||||
Log.Warning("[http] didn't boot")
|
||||
break
|
||||
@@ -39,12 +40,10 @@ func ensureAppHasBooted(address string, message string) {
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
res, err := http.Get(address)
|
||||
if err != nil {
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNotFound {
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
Log.Info(message)
|
||||
|
||||
Reference in New Issue
Block a user