Browse Source

fix: only init on standard start

master
OhYee 9 months ago
parent
commit
84d8feeee1
  1. 9
      api/pkg/menu/main.go
  2. 3
      api/pkg/post/jieba.go
  3. 3
      api/pkg/post/update.go
  4. 4
      main.go
  5. 3
      mongo/conn.go
  6. 15
      utils/initial/initial.go

9
api/pkg/menu/main.go

@ -3,6 +3,7 @@ package menu @@ -3,6 +3,7 @@ package menu
import (
"github.com/OhYee/blotter/mongo"
"github.com/OhYee/blotter/output"
"github.com/OhYee/blotter/utils/initial"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
@ -10,9 +11,11 @@ import ( @@ -10,9 +11,11 @@ import (
const DatabaseName = "pages"
func init() {
if exists, _ := mongo.CollectionExists("blotter", DatabaseName); exists == false {
initMenus()
}
initial.Register(func() {
if exists, _ := mongo.CollectionExists("blotter", DatabaseName); exists == false {
initMenus()
}
})
}
func initMenus() {

3
api/pkg/post/jieba.go

@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
"sync"
"github.com/OhYee/blotter/output"
"github.com/OhYee/blotter/utils/initial"
"github.com/yanyiwu/gojieba"
)
@ -31,5 +32,5 @@ func getJieba() *gojieba.Jieba { @@ -31,5 +32,5 @@ func getJieba() *gojieba.Jieba {
}
func init() {
go initJieba()
initial.Register(initJieba)
}

3
api/pkg/post/update.go

@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/OhYee/blotter/mongo"
"github.com/OhYee/blotter/utils/initial"
"go.mongodb.org/mongo-driver/bson"
)
@ -35,5 +36,5 @@ func updatePublishedToStatus() { @@ -35,5 +36,5 @@ func updatePublishedToStatus() {
}
func init() {
go updatePublishedToStatus()
initial.Register(updatePublishedToStatus)
}

4
main.go

@ -11,6 +11,7 @@ import ( @@ -11,6 +11,7 @@ import (
"github.com/OhYee/blotter/http"
"github.com/OhYee/blotter/output"
"github.com/OhYee/blotter/register"
"github.com/OhYee/blotter/utils/initial"
)
var (
@ -55,13 +56,14 @@ func main() { @@ -55,13 +56,14 @@ func main() {
}
f()
os.Exit(0)
return
}
cron.Start()
defer cron.Stop()
api.Register()
initial.Run()
// queue.Register().Register("extensions/queue")
// register.DebugApiMap()
output.Log("Server will start at http://%s", addr)

3
mongo/conn.go

@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
"os"
"sync"
"github.com/OhYee/blotter/utils/initial"
"go.mongodb.org/mongo-driver/mongo/options"
)
@ -32,5 +33,5 @@ func getClientOptions() *options.ClientOptions { @@ -32,5 +33,5 @@ func getClientOptions() *options.ClientOptions {
}
func init() {
go initClientOptions()
initial.Register(initClientOptions)
}

15
utils/initial/initial.go

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
package initial
var (
initialFunction = make([]func(), 0)
)
func Register(f func()) {
initialFunction = append(initialFunction, f)
}
func Run() {
for _, f := range initialFunction {
go f()
}
}
Loading…
Cancel
Save