Compare commits

..

No commits in common. "3ad49a082c1e4380fba68e369c826a2810096e7c" and "88f803692a2245b44f52a1fe5c506938b9189e6b" have entirely different histories.

2 changed files with 19 additions and 26 deletions

View File

@ -1,39 +1,19 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
"path"
"haberdasher-twirp/haberdasher" "haberdasher-twirp/haberdasher"
"haberdasher-twirp/internal/haberdasherserver" "haberdasher-twirp/internal/haberdasherserver"
) )
func main() { func main() {
var cwd, dbPath, dbDir string
var err error
var bind string = ":8080" var bind string = ":8080"
cwd, err = os.Getwd()
dbPath = path.Join(cwd, "var/hat.db")
dbDir = path.Dir(dbPath)
if _, err = os.Stat(dbDir); os.IsNotExist(err) {
err = os.Mkdir(dbDir, 0700)
if err != nil {
fmt.Printf("boltdb: %s\n", err)
}
}
if len(os.Args) > 1 { if len(os.Args) > 1 {
bind = os.Args[1] bind = os.Args[1]
} }
server := &haberdasherserver.Server{}
fmt.Printf("boltdb: %s\n", dbPath)
server := &haberdasherserver.Server{DBPath: dbPath}
twirpHandler := haberdasher.NewHaberdasherServer(server) twirpHandler := haberdasher.NewHaberdasherServer(server)
http.ListenAndServe(bind, twirpHandler) http.ListenAndServe(bind, twirpHandler)

View File

@ -16,9 +16,7 @@ import (
const dbBucket = "hats" const dbBucket = "hats"
type Server struct { type Server struct{}
DBPath string
}
type Hat struct { type Hat struct {
Inches int32 `json:"inchues"` Inches int32 `json:"inchues"`
@ -66,7 +64,7 @@ func HatQueryToHatQueryModel(q *pb.HatQuery) HatQuery {
} }
func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) { func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) {
st, _ := NewStore(s.DBPath, 0600, nil) st, _ := NewStore("hat.db", 0600, nil)
defer st.Close() defer st.Close()
if size.Inches <= 0 { if size.Inches <= 0 {
@ -87,7 +85,22 @@ func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err e
} }
func (s *Server) ListHats(ctx context.Context, q *pb.HatQuery) (hats *pb.Hats, err error) { func (s *Server) ListHats(ctx context.Context, q *pb.HatQuery) (hats *pb.Hats, err error) {
st, _ := NewStore(s.DBPath, 0600, nil) /*
var hat *pb.Hat
hats = &pb.Hats{}
for i := 0; i < 10; i++ {
hat = &pb.Hat{
Inches: int32(rand.Intn(12)),
Color: []string{"white", "black", "red", "blue"}[rand.Intn(4)],
Name: []string{"bowler", "baseball cap", "top hat", "derby"}[rand.Intn(3)],
}
hats.Hats = append(hats.Hats, hat)
}
*/
st, _ := NewStore("hat.db", 0600, nil)
defer st.Close() defer st.Close()
hs, err := st.ListHats(HatQueryToHatQueryModel(q)) hs, err := st.ListHats(HatQueryToHatQueryModel(q))