refactor: séparer executable et library
This commit is contained in:
parent
b3706080cd
commit
2dcf0ec867
11 changed files with 70 additions and 71 deletions
|
@ -4,15 +4,16 @@ LABEL author="vlbeaudoin"
|
||||||
|
|
||||||
WORKDIR /go/src/app
|
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 queries/ queries/
|
||||||
ADD ui/ ui/
|
ADD ui/ ui/
|
||||||
|
|
||||||
RUN CGO_ENABLED=0 go build \
|
RUN CGO_ENABLED=0 go build \
|
||||||
-a \
|
-a \
|
||||||
-o presences \
|
-o presences \
|
||||||
./
|
./cmd/presences/
|
||||||
|
|
||||||
# Alpine
|
# Alpine
|
||||||
|
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -17,7 +17,7 @@ help: ## Show this help
|
||||||
|
|
||||||
.PHONY: go-install
|
.PHONY: go-install
|
||||||
go-install: ## Use `go install` to build and link the executable
|
go-install: ## Use `go install` to build and link the executable
|
||||||
go install -a
|
go install -a ./cmd/presences/
|
||||||
|
|
||||||
.PHONY: docker-build
|
.PHONY: docker-build
|
||||||
docker-build: ## Build container image
|
docker-build: ## Build container image
|
||||||
|
|
50
cmd.go
50
cmd.go
|
@ -1,11 +1,15 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"codeberg.org/vlbeaudoin/voki/v3"
|
||||||
|
"git.agecem.com/bottin/bottin/v11"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
"github.com/spf13/cobra"
|
"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
11
cmd/presences/main.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.agecem.com/bottin/presences"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Entry
|
||||||
|
func main() {
|
||||||
|
// Start commandline parsing then call `run`
|
||||||
|
presences.Execute()
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
2
db.go
2
db.go
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
|
2
flag.go
2
flag.go
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
61
main.go
61
main.go
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package presences
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue