2024-10-09 16:54:48 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-10 16:20:08 +08:00
|
|
|
_ "awesomeProject/internal/store"
|
2024-10-09 16:54:48 +08:00
|
|
|
"awesomeProject/server"
|
|
|
|
"awesomeProject/store/factory"
|
|
|
|
"context"
|
2024-10-10 16:20:08 +08:00
|
|
|
"fmt"
|
2024-10-09 16:54:48 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-10-10 16:20:08 +08:00
|
|
|
func sendOddNum(cn chan<- int) {
|
|
|
|
number := 1
|
|
|
|
for {
|
|
|
|
cn <- number
|
|
|
|
number += 2 // 生成下一个奇数
|
|
|
|
fmt.Println("inside:", number)
|
|
|
|
time.Sleep(1 * time.Second) // 每 1 秒发送一次
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendEvenNum(cn chan<- int) {
|
|
|
|
number := 2
|
|
|
|
for {
|
|
|
|
cn <- number
|
|
|
|
number += 2
|
|
|
|
fmt.Println("inside:", number)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//func main() {
|
|
|
|
// cn := make(chan int, 10)
|
|
|
|
// go sendOddNum(cn)
|
|
|
|
// go sendEvenNum(cn)
|
|
|
|
//
|
|
|
|
// for {
|
|
|
|
// num := <-cn
|
|
|
|
// time.Sleep(5 * time.Second)
|
|
|
|
// fmt.Println("Received:", num)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2024-10-09 16:54:48 +08:00
|
|
|
func main() {
|
|
|
|
s, err := factory.New("mem") // 创建图书数据存储模块实例
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
srv := server.NewBookStoreServer(":8080", s) // 创建http服务实例
|
|
|
|
errChan, err := srv.ListenAndServe() // 运行http服务
|
|
|
|
if err != nil {
|
|
|
|
log.Println("web server start failed:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("web server start ok")
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
select { // 监视来自errChan以及c的事件
|
|
|
|
case err = <-errChan:
|
|
|
|
log.Println("web server run failed:", err)
|
|
|
|
return
|
|
|
|
case <-c:
|
|
|
|
log.Println("bookstore program is exiting...")
|
|
|
|
ctx, cf := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cf()
|
|
|
|
err = srv.Shutdown(ctx) // 优雅关闭http服务实例
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Println("bookstore program exit error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("bookstore program exit ok")
|
|
|
|
}
|