Compare commits
2 Commits
88f803692a
...
3ad49a082c
Author | SHA1 | Date | |
---|---|---|---|
3ad49a082c | |||
fbeb439bc0 |
22
cmd/main.go
22
cmd/main.go
@ -1,19 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"haberdasher-twirp/haberdasher"
|
||||
"haberdasher-twirp/internal/haberdasherserver"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var cwd, dbPath, dbDir string
|
||||
var err error
|
||||
|
||||
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 {
|
||||
bind = os.Args[1]
|
||||
}
|
||||
server := &haberdasherserver.Server{}
|
||||
|
||||
fmt.Printf("boltdb: %s\n", dbPath)
|
||||
|
||||
server := &haberdasherserver.Server{DBPath: dbPath}
|
||||
twirpHandler := haberdasher.NewHaberdasherServer(server)
|
||||
|
||||
http.ListenAndServe(bind, twirpHandler)
|
||||
|
@ -16,7 +16,9 @@ import (
|
||||
|
||||
const dbBucket = "hats"
|
||||
|
||||
type Server struct{}
|
||||
type Server struct {
|
||||
DBPath string
|
||||
}
|
||||
|
||||
type Hat struct {
|
||||
Inches int32 `json:"inchues"`
|
||||
@ -64,7 +66,7 @@ func HatQueryToHatQueryModel(q *pb.HatQuery) HatQuery {
|
||||
}
|
||||
|
||||
func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) {
|
||||
st, _ := NewStore("hat.db", 0600, nil)
|
||||
st, _ := NewStore(s.DBPath, 0600, nil)
|
||||
defer st.Close()
|
||||
|
||||
if size.Inches <= 0 {
|
||||
@ -85,22 +87,7 @@ 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) {
|
||||
/*
|
||||
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)
|
||||
st, _ := NewStore(s.DBPath, 0600, nil)
|
||||
defer st.Close()
|
||||
|
||||
hs, err := st.ListHats(HatQueryToHatQueryModel(q))
|
||||
|
Loading…
Reference in New Issue
Block a user