78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
_ "awesomeProject/internal/store"
|
|
"awesomeProject/server"
|
|
"awesomeProject/store/factory"
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
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)
|
|
// }
|
|
//}
|
|
|
|
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")
|
|
}
|