refactor: séparer executable et library

This commit is contained in:
Victor Lacasse-Beaudoin 2025-05-13 13:48:34 -04:00
parent b3706080cd
commit 2dcf0ec867
11 changed files with 70 additions and 71 deletions

View file

@ -4,15 +4,16 @@ LABEL author="vlbeaudoin"
WORKDIR /go/src/app
COPY LICENSE cmd.go config.go db.go entity.go flag.go go.mod go.sum handler.go main.go server.go ./
COPY LICENSE cmd.go config.go db.go entity.go flag.go go.mod go.sum handler.go server.go ./
ADD cmd/ cmd/
ADD queries/ queries/
ADD ui/ ui/
RUN CGO_ENABLED=0 go build \
-a \
-o presences \
./
./cmd/presences/
# Alpine

View file

@ -17,7 +17,7 @@ help: ## Show this help
.PHONY: go-install
go-install: ## Use `go install` to build and link the executable
go install -a
go install -a ./cmd/presences/
.PHONY: docker-build
docker-build: ## Build container image

50
cmd.go
View file

@ -1,11 +1,15 @@
package main
package presences
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
"git.agecem.com/bottin/bottin/v11"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/spf13/cobra"
)
@ -49,3 +53,47 @@ var rootCmd = &cobra.Command{
}
},
}
func run(ctx context.Context, cfg Config) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
bottinClient := bottin.APIClient{Caller: voki.New(
http.DefaultClient,
cfg.Bottin.Host,
cfg.Bottin.Key,
cfg.Bottin.Port,
func() string {
if cfg.Bottin.TLS.Enabled {
return "https"
} else {
return "http"
}
}(),
)}
// connect to db
dbPool, err := pgxpool.New(ctx,
fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
cfg.DB.Username,
cfg.DB.Password,
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.Database,
cfg.DB.SSLMode,
))
if err != nil {
return err
}
defer dbPool.Close()
dbClient := DBClient{Pool: dbPool}
if err := RunUIServer(ctx, cfg, &bottinClient, &dbClient); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
}

11
cmd/presences/main.go Normal file
View file

@ -0,0 +1,11 @@
package main
import (
"git.agecem.com/bottin/presences"
)
// Entry
func main() {
// Start commandline parsing then call `run`
presences.Execute()
}

View file

@ -1,4 +1,4 @@
package main
package presences
import (
"fmt"

2
db.go
View file

@ -1,4 +1,4 @@
package main
package presences
import (
"context"

View file

@ -1,4 +1,4 @@
package main
package presences
import "time"

View file

@ -1,4 +1,4 @@
package main
package presences
import (
"github.com/spf13/pflag"

View file

@ -1,4 +1,4 @@
package main
package presences
import (
"context"

61
main.go
View file

@ -1,61 +0,0 @@
package main
import (
"context"
"fmt"
"net/http"
"codeberg.org/vlbeaudoin/voki/v3"
"git.agecem.com/bottin/bottin/v11"
"github.com/jackc/pgx/v5/pgxpool"
)
// Entry
func main() {
// Start commandline parsing then call `run`
Execute()
}
func run(ctx context.Context, cfg Config) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
bottinClient := bottin.APIClient{Caller: voki.New(
http.DefaultClient,
cfg.Bottin.Host,
cfg.Bottin.Key,
cfg.Bottin.Port,
func() string {
if cfg.Bottin.TLS.Enabled {
return "https"
} else {
return "http"
}
}(),
)}
// connect to db
dbPool, err := pgxpool.New(ctx,
fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
cfg.DB.Username,
cfg.DB.Password,
cfg.DB.Host,
cfg.DB.Port,
cfg.DB.Database,
cfg.DB.SSLMode,
))
if err != nil {
return err
}
defer dbPool.Close()
dbClient := DBClient{Pool: dbPool}
if err := RunUIServer(ctx, cfg, &bottinClient, &dbClient); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
}

View file

@ -1,4 +1,4 @@
package main
package presences
import (
"context"