aboutsummaryrefslogtreecommitdiff
path: root/cmd/xesite/api.go
blob: 6a79ad99702117194f4225257573faa97ce8b359 (plain)
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
package main

import (
	"context"
	"os"
	"os/exec"
	"runtime"
	"strings"

	"github.com/twitchtv/twirp"
	"google.golang.org/protobuf/types/known/emptypb"
	"google.golang.org/protobuf/types/known/timestamppb"
	"xeiaso.net/v4/internal/lume"
	"xeiaso.net/v4/pb"
	"xeiaso.net/v4/pb/external/protofeed"
)

var denoVersion string

func init() {
	cmd := exec.Command("deno", "--version")
	out, err := cmd.CombinedOutput()
	if err != nil {
		denoVersion = "unknown"
		return
	}
	denoVersion = strings.Split(strings.TrimSpace(string(out)), "\n")[0]
}

type MetaServer struct {
	fs *lume.FS
}

func (ms *MetaServer) Metadata(ctx context.Context, _ *emptypb.Empty) (*pb.BuildInfo, error) {
	commit, err := ms.fs.Commit()
	if err != nil {
		return nil, twirp.InternalErrorf("can't get commit hash: %w", err)
	}

	result := &pb.BuildInfo{
		Commit:        commit,
		GoVersion:     runtime.Version(),
		DenoVersion:   denoVersion,
		XesiteVersion: os.Args[0],
		BuildTime:     timestamppb.New(ms.fs.BuildTime()),
	}

	if *devel {
		result.XesiteVersion = "devel"
	}

	return result, nil
}

type FeedServer struct {
	fs *lume.FS
}

func (f *FeedServer) Get(ctx context.Context, _ *emptypb.Empty) (*protofeed.Feed, error) {
	return f.fs.LoadProtoFeed()
}