DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: 14e0729381328bc2af9840dce46025c829b2aa2c emerwen-web/internal/client/client.go -rw-r--r-- 1.1 KiB
14e07293Jonni Liljamo fix(components): better base layout a day 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}
}