commit 2aed21477f50afcf3cb5c375ff60cd477ede87b3 Author: lulz1 Date: Wed Oct 9 16:54:48 2024 +0800 first commit 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 // 删除某图书条目 +}