From 2aed21477f50afcf3cb5c375ff60cd477ede87b3 Mon Sep 17 00:00:00 2001 From: lulz1 Date: Wed, 9 Oct 2024 16:54:48 +0800 Subject: [PATCH] first commit --- .idea/.gitignore | 8 ++++++ .idea/awesomeProject.iml | 9 +++++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ cmd/bookstore/main.go | 43 +++++++++++++++++++++++++++++++++ go.mod | 1 + internal/store/memstore.go | 1 + server/middleware/middleware.go | 1 + server/server.go | 1 + store/factory/factory.go | 33 +++++++++++++++++++++++++ store/store.go | 16 ++++++++++++ 11 files changed, 127 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/awesomeProject.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 cmd/bookstore/main.go create mode 100644 go.mod create mode 100644 internal/store/memstore.go create mode 100644 server/middleware/middleware.go create mode 100644 server/server.go create mode 100644 store/factory/factory.go create mode 100644 store/store.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/awesomeProject.iml b/.idea/awesomeProject.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/awesomeProject.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..cc47053 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cmd/bookstore/main.go b/cmd/bookstore/main.go new file mode 100644 index 0000000..51a894f --- /dev/null +++ b/cmd/bookstore/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "awesomeProject/server" + "awesomeProject/store/factory" + "context" + "log" + "os" + "os/signal" + "syscall" + "time" +) + +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") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..41f7029 --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module awesomeProject diff --git a/internal/store/memstore.go b/internal/store/memstore.go new file mode 100644 index 0000000..72440ea --- /dev/null +++ b/internal/store/memstore.go @@ -0,0 +1 @@ +package store diff --git a/server/middleware/middleware.go b/server/middleware/middleware.go new file mode 100644 index 0000000..c870d7c --- /dev/null +++ b/server/middleware/middleware.go @@ -0,0 +1 @@ +package middleware diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..abb4e43 --- /dev/null +++ b/server/server.go @@ -0,0 +1 @@ +package server diff --git a/store/factory/factory.go b/store/factory/factory.go new file mode 100644 index 0000000..95f87f0 --- /dev/null +++ b/store/factory/factory.go @@ -0,0 +1,33 @@ +package factory + +import ( + "awesomeProject/store" + "fmt" + "sync" +) + +var ( + providersMu sync.RWMutex + providers = make(map[string]store.Store) +) + +func Register(name string, p store.Store) { + providersMu.Lock() + defer providersMu.Unlock() + if p == nil { + panic("store: Register provider is nil") + } + if _, dup := providers[name]; dup { + panic("store: Register called twice for provider " + name) + } + providers[name] = p +} +func New(providerName string) (store.Store, error) { + providersMu.RLock() + p, ok := providers[providerName] + providersMu.RUnlock() + if !ok { + return nil, fmt.Errorf("store: unknown provider %s", providerName) + } + return p, nil +} diff --git a/store/store.go b/store/store.go new file mode 100644 index 0000000..f7116f8 --- /dev/null +++ b/store/store.go @@ -0,0 +1,16 @@ +package store + +type Book struct { + Id string `json:"id"` // 图书ISBN ID + Name string `json:"name"` // 图书名称 + Authors []string `json:"authors"` // 图书作者 + Press string `json:"press"` // 出版社 +} + +type Store interface { + Create(*Book) error // 创建一个新图书条目 + Update(*Book) error // 更新某图书条目 + Get(string) (Book, error) // 获取某图书信息 + GetAll() ([]Book, error) // 获取所有图书信息 + Delete(string) error // 删除某图书条目 +}