Skip to content

Commit a9d1a21

Browse files
committed
Store host port on station in db
1 parent d3c6d3c commit a9d1a21

6 files changed

Lines changed: 45 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
alter table stations add column host_port text not null default '';
2+
3+
---- create above / drop below ----
4+
5+
alter table stations drop column host_port;

gap/db/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gap/db/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ insert into stations(station_id, slug, user_id, active) values($1, $2, $3, $4) r
3737
-- name: SetStationCurrentTrack :exec
3838
update stations set current_track_id = $1 where station_id = $2;
3939

40+
-- name: SetStationHostPort :exec
41+
update stations set host_port = $1 where station_id = $2;
42+
4043
-- name: StationCurrentTrack :one
4144
select tracks.* from stations join tracks on stations.current_track_id = tracks.track_id where stations.station_id = $1;
4245

gap/db/query.sql.go

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gap/db/schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ CREATE TABLE public.stations (
263263
current_track_id text,
264264
background_image_url text DEFAULT ''::text NOT NULL,
265265
user_id text NOT NULL,
266-
is_public boolean DEFAULT false NOT NULL
266+
is_public boolean DEFAULT false NOT NULL,
267+
host_port text DEFAULT ''::text NOT NULL
267268
);
268269

269270

gap/internal/server/routes.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ func (s *Server) startLiq(w http.ResponseWriter, r *http.Request) {
427427
ctx := r.Context()
428428
slug := chi.URLParam(r, "slug")
429429

430+
station, err := s.db.Q().Station(ctx, slug)
431+
if err != nil {
432+
http.Error(w, err.Error(), http.StatusInternalServerError)
433+
return
434+
}
435+
430436
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
431437
if err != nil {
432438
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -507,12 +513,22 @@ func (s *Server) startLiq(w http.ResponseWriter, r *http.Request) {
507513

508514
resp, err := cli.ContainerInspect(ctx, response.ID)
509515
if err != nil {
510-
panic(err)
516+
http.Error(w, err.Error(), http.StatusInternalServerError)
517+
return
511518
}
512519
for containerPort, bindings := range resp.NetworkSettings.Ports {
513520
fmt.Println("containerPort", containerPort)
514-
for _, binding := range bindings {
515-
fmt.Printf("Container port %s is bound to host port %s\n", containerPort, binding.HostPort)
521+
if containerPort.Port() == "1234" {
522+
var hostPort string
523+
if len(bindings) == 1 {
524+
hostPort = bindings[0].HostPort
525+
}
526+
err := s.db.Q().SetStationHostPort(ctx, hostPort, station.StationID)
527+
if err != nil {
528+
http.Error(w, err.Error(), http.StatusInternalServerError)
529+
return
530+
}
531+
fmt.Printf("station %s host port=%s\n", station.Slug, hostPort)
516532
}
517533
}
518534

0 commit comments

Comments
 (0)