-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (57 loc) · 1.56 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
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"log"
"math/rand"
"net"
pb "hoshimachi/proto"
"google.golang.org/grpc"
)
const (
sui_port string = ":22318"
)
type SuiQuote struct {
Quote string `json:"quote"`
Type uint32 `json:"type"`
Source string `json:"source"`
}
type HoshimachiQuoteGenServer struct {
pb.UnimplementedHoshimachiQuoteGenServer
}
func HoshimachiQuoteFromJSON(path string, arr *[]SuiQuote) error {
json_data, json_err := os.ReadFile(path)
if json_err != nil {
return fmt.Errorf("failed to read file: %v", json_err)
}
parse_err := json.Unmarshal(json_data, &arr)
if parse_err != nil {
return fmt.Errorf("failed to parse: %v", parse_err)
}
return nil
}
var suisei_quotes []SuiQuote
func (s *HoshimachiQuoteGenServer) GetRandomQuote(ctx context.Context, req *pb.SuiQuoteReq) (*pb.SuiQuoteResp, error) {
rand_index := rand.Intn(len(suisei_quotes))
selected_quote := suisei_quotes[rand_index]
return &pb.SuiQuoteResp{
Quote: selected_quote.Quote,
Type: selected_quote.Type,
Source: selected_quote.Source,
}, nil
}
func main() {
HoshimachiQuoteFromJSON("./quotes/Sui_EN.json", &suisei_quotes)
lis, lis_err := net.Listen("tcp", sui_port)
if lis_err != nil {
log.Fatal("❌🌠 Hoshimachi failed to listen:", lis_err)
}
sui_grpc := grpc.NewServer()
pb.RegisterHoshimachiQuoteGenServer(sui_grpc, &HoshimachiQuoteGenServer{})
fmt.Println("🌠 Hoshimachi's running on port", sui_port)
if serve_err := sui_grpc.Serve(lis); serve_err != nil {
log.Fatal("❌🌠 Hoshimachi's failed to serve:", serve_err)
}
}