DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: 6363d38d035b8b64a35fd40586ab563f07d46601 emerwen-web/internal/client/client.go -rw-r--r-- 1.1 KiB
6363d38dJonni Liljamo feat: gRPC client and fetch basic worker details 3 days ago
                                                                                
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
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

// Package client provides a type wrapper for the emerwen gRPC WebToMasterClient.
package client

import (
	"context"
	"fmt"
	"log/slog"

	"git.src.quest/~liljamo/emerwen-proto/go/proto"
	"git.src.quest/~liljamo/emerwen-web/internal/config"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/metadata"
)

// Client wraps the emerwen gRPC WebToMasterClient, along with a Context.
type Client struct {
	Client proto.WebToMasterClient
	Ctx    context.Context
}

// NewClient constructs a new Client.
func NewClient(c *config.Config) *Client {
	conn, err := grpc.NewClient(fmt.Sprintf("unix://%s", c.SocketPath), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		slog.Error("yay")
	}

	client := proto.NewWebToMasterClient(conn)

	// TODO: config read auth_token from file
	ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", "Bearer avain_perkele")

	return &Client{Client: client, Ctx: ctx}
}