forked from ellemouton/aperture-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
167 lines (133 loc) · 3.63 KB
/
content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package content
import (
"context"
"fmt"
"log"
"net"
"net/http"
"strconv"
pb "github.com/ellemouton/aperture-demo/contentrpc"
"github.com/ellemouton/aperture-demo/db"
"github.com/gorilla/mux"
pricespb "github.com/lightninglabs/aperture/pricesrpc"
"google.golang.org/grpc"
)
type Server struct {
DB *db.DB
*pb.UnimplementedContentServer
contentServer *grpc.Server
pricesServer *grpc.Server
}
func NewServer() (*Server, error) {
db, err := db.NewDB()
if err != nil {
return nil, err
}
return &Server{
DB: db,
}, nil
}
func (s *Server) Start() error {
// Start the Content gRPC server.
s.contentServer = grpc.NewServer()
pb.RegisterContentServer(s.contentServer, s)
lis, err := net.Listen("tcp", "localhost:8080")
if err != nil {
return err
}
log.Printf("Content Server serving at %s", "localhost:8080")
go func() {
if err := s.contentServer.Serve(lis); err != nil {
fmt.Printf("error starting content server: %v\n", err)
}
}()
// Start the Content gRPC server.
s.pricesServer = grpc.NewServer()
pricespb.RegisterPricesServer(s.pricesServer, s)
lis2, err := net.Listen("tcp", "localhost:8083")
if err != nil {
return err
}
log.Printf("Prices Server serving at %s", "localhost:8083")
go func() {
if err := s.pricesServer.Serve(lis2); err != nil {
fmt.Printf("error starting content server: %v\n", err)
}
}()
// Start the http server that listens for content requests.
r := mux.NewRouter()
r.HandleFunc("/test", freebeeHandler).Methods("GET")
r.HandleFunc("/article/{id}", s.articleHandler).Methods("GET")
r.HandleFunc("/quote/{id}", s.quoteHandler).Methods("GET")
log.Printf("Serving HTTP server on port %s", "localhost:9000")
go func() {
if err := http.ListenAndServe("localhost:9000", r); err != nil {
fmt.Printf("error starting http server: %v\n", err)
}
}()
return nil
}
func (s *Server) Stop() error {
s.contentServer.Stop()
return s.DB.Close()
}
var _ pb.ContentServer = (*Server)(nil)
func (s *Server) AddArticle(_ context.Context,
req *pb.AddArticleRequest) (*pb.AddArticleResponse, error) {
id, err := s.DB.AddArticle(&db.Article{
Title: req.Title,
Author: req.Author,
Content: req.Content,
})
if err != nil {
return nil, err
}
return &pb.AddArticleResponse{Id: int64(id)}, nil
}
func (s *Server) AddQuote(_ context.Context,
req *pb.AddQuoteRequest) (*pb.AddQuoteResponse, error) {
id, err := s.DB.AddQuote(&db.Quote{
Author: req.Author,
Content: req.Content,
Price: req.Price,
})
if err != nil {
return nil, err
}
return &pb.AddQuoteResponse{Id: int64(id)}, nil
}
func freebeeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Freebee endpoint test")
}
func (s *Server) articleHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
article, err := s.DB.GetArticle(int(id))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp := fmt.Sprintf("Title: %s\nAuthor: %s\nContent: %s\n",
article.Title, article.Author, article.Content)
fmt.Fprintln(w, resp)
}
func (s *Server) quoteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
quote, err := s.DB.GetQuote(int(id))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp := fmt.Sprintf("Quote Author: %s\nContent: %s\nPrice: %d\n",
quote.Author, quote.Content, quote.Price)
fmt.Fprintln(w, resp)
}