Skip to content

Commit 17fc901

Browse files
committed
Refactor the gate.AutoRegister API and hide the portal pb.go files
This should probably be several commits but it's too much and I don't feel like splitting it up. 1. Hid the protos from the public go API. This will enable me to drop gRPC or maybe even protos entirely without changing the go API again. Another reason for this is there are now two different output styles protoc can do for go code and this will allow me to update as proto styles update without breaking my API. This also lets me have nice things like tls.Config in the structs and uint16 for port numbers (which protos don't support). So there's also wrapper functions for all the RPCs now. 2. gate.AutoRegister no longer returns an annoying results struct. Also client.AutoRegisterChan is now just client.AutoRegister and it takes a callback function instead of a channel to write to (it's a more flexible API, you don't have to have goroutines just to split channels etc). 3. tools.HTTPServer now takes a context not a quit channel 4. Dropped tools.AutoTLSConf for now because I'm still debating what the right API for it should be. I want to have something like that still but I'm going to keep it private until I like the API better. 5. Using crypto.PrivateKey instead of crypto.Signer now because it's more semantically correct Most of this I actually wrote months ago. I decided back then I want to try harder to have no dependencies and chose to move towards dropping gRPC and this was my solution. Back then I inevitably lost momentum and left myself with a difficult list of problems about how to import the private protos across binaries and what to do about using prototext as the public config file API for assimilate and spawn as well as the gRPC/proto public API of the portal server. I also keep going back and forth on the best API for AutoRegister (I don't love the one that spawns a background goroutine still). I suppose I'm moving towards using context instead of quit chans in all the public APIs. I do think it's more friendly and idiomatic. I used to kind of want to rebel against having ctx be the first arg of everything. Recently I only fixed up the compile errors and got it ready to actually commit.
1 parent f1b73b8 commit 17fc901

28 files changed

+1134
-1160
lines changed

assimilate/embedassimilate/assimilate.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
_ "ask.systems/daemon/portal/flags"
2020
_ "ask.systems/daemon/tools/flags"
2121

22+
"ask.systems/daemon/internal/portalpb"
2223
"ask.systems/daemon/portal/gate"
2324
"ask.systems/daemon/tools"
2425
"google.golang.org/protobuf/encoding/prototext"
@@ -59,7 +60,7 @@ func Run(ctx context.Context, flags *flag.FlagSet, args []string) {
5960
ctx, _ = tools.ContextWithQuitSignals(context.Background())
6061
errCount := 0
6162
for i, requestText := range flags.Args() {
62-
registration := &gate.RegisterRequest{}
63+
registration := &portalpb.RegisterRequest{}
6364
err := prototext.Unmarshal([]byte(requestText), registration)
6465
if err != nil {
6566
log.Printf("Failed to unmarshal RegisterRequest #%v %#v: %v",
@@ -70,7 +71,13 @@ func Run(ctx context.Context, flags *flag.FlagSet, args []string) {
7071
idx := i
7172
wg.Add(1)
7273
go func() {
73-
err := client.AutoRegisterChan(ctx, registration, nil)
74+
err := client.AutoRegister(ctx, &gate.RegisterRequest{
75+
Pattern: registration.Pattern,
76+
FixedPort: uint16(registration.FixedPort),
77+
Hostname: registration.Hostname,
78+
StripPattern: registration.StripPattern,
79+
AllowHttp: registration.AllowHttp,
80+
}, nil)
7481
wg.Done()
7582
if err != nil && !errors.Is(err, context.Cause(ctx)) {
7683
log.Printf("Error for registration #%v: %v", idx, err)

host/embedhost/host.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,16 @@ func Run(ctx context.Context, flags *flag.FlagSet, args []string) {
9696

9797
// Register the reverse proxy pattern with portal.
9898
// Only blocks until the initial registration is done, then keeps renewing.
99-
reg, waitForUnregister, err := gate.AutoRegister(ctx, &gate.RegisterRequest{
99+
port, tlsconf, wait, err := gate.AutoRegister(ctx, &gate.RegisterRequest{
100100
Pattern: *urlPath,
101101
})
102102
if err != nil {
103103
log.Print("Fatal error registering with portal:", err)
104104
return
105105
}
106106
// Start serving files (blocks until graceful stop is done)
107-
tools.HTTPServer(ctx.Done(), reg.Lease.Port, reg.TLSConfig, nil)
108-
// Wait for the AutoRegister background goroutine to gracefully stop
109-
<-waitForUnregister
107+
tools.HTTPServer(ctx, port, tlsconf, nil)
108+
<-wait // Wait for the AutoRegister background goroutine to gracefully stop
110109
log.Print("Goodbye.")
111110
}
112111

internal/portalpb/embed.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package portalpb
2+
3+
import _ "embed"
4+
5+
//go:embed service.proto
6+
var ServiceProto string

0 commit comments

Comments
 (0)