/
main.go
64 lines (53 loc) · 1.87 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/apache/thrift/lib/go/thrift"
"github.com/rs/cors"
"github.com/Workiva/frugal/examples/go/gen-go/v1/music"
"github.com/Workiva/frugal/lib/go"
)
// Run an HTTP server
func main() {
// Set the protocol used for serialization.
// The protocol stack must match between client and server
fProtocolFactory := frugal.NewFProtocolFactory(thrift.NewTBinaryProtocolFactoryDefault())
// Create a handler. Each incoming request at the processor is sent to
// the handler. Responses from the handler are returned back to the
// client
handler := &StoreHandler{}
processor := music.NewFStoreProcessor(handler)
// Start the server using the configured processor, and protocol
mux := http.NewServeMux()
mux.HandleFunc("/frugal", frugal.NewFrugalHandlerFunc(processor, fProtocolFactory))
corsOptions := cors.Options{
AllowedHeaders: []string{"Content-Transfer-Encoding"},
}
httpHandler := cors.New(corsOptions).Handler(mux)
fmt.Println("Starting the http server...")
log.Fatal(http.ListenAndServe(":9090", httpHandler))
}
// StoreHandler handles all incoming requests to the server.
// The handler must satisfy the interface the server exposes.
type StoreHandler struct{}
// BuyAlbum always buys the same album
func (f *StoreHandler) BuyAlbum(ctx frugal.FContext, ASIN string, acct string) (r *music.Album, err error) {
album := &music.Album{
ASIN: "c54d385a-5024-4f3f-86ef-6314546a7e7f",
Duration: 1200,
Tracks: []*music.Track{{
Title: "Comme des enfants",
Artist: "Coeur de pirate",
Publisher: "Grosse Boîte",
Composer: "Béatrice Martin",
Duration: 169,
Pro: music.PerfRightsOrg_ASCAP,
}},
}
return album, nil
}
// EnterAlbumGiveaway always returns true
func (f *StoreHandler) EnterAlbumGiveaway(ctx frugal.FContext, email string, name string) (r bool, err error) {
return true, nil
}