generated from greenbone/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic structure and containerize service
- Loading branch information
Showing
13 changed files
with
348 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
FROM golang:1.22.2-alpine AS builder | ||
RUN apk add --no-cache make | ||
|
||
# swagger docs generation will fail if cgo is used | ||
ENV CGO_ENABLED=0 | ||
|
||
WORKDIR /src/ | ||
|
||
# preinstall dependencies for better build caching | ||
COPY go.mod . | ||
COPY go.sum . | ||
RUN go mod download | ||
|
||
COPY Makefile . | ||
|
||
# preinstall code generation tools for better build caching | ||
RUN make install-code-generation-tools | ||
|
||
# copy api related source files and generate api docs | ||
COPY pkg/web pkg/web | ||
COPY pkg/models pkg/models | ||
RUN make api-docs | ||
|
||
# copy rest of the source files and build | ||
COPY cmd cmd | ||
COPY pkg pkg | ||
RUN make test | ||
RUN make build | ||
|
||
FROM busybox | ||
# service files | ||
COPY --from=builder /src/api /api | ||
COPY --from=builder /src/bin/notification-service /bin/ | ||
|
||
ENTRYPOINT ["./bin/notification-service"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
all: api-docs build test | ||
|
||
.PHONY: install-code-generation-tools api-docs generate-code build test | ||
.PHONY: install-code-generation-tools api-docs generate-code build test start-services | ||
|
||
SWAG = github.com/swaggo/swag/cmd/[email protected] | ||
|
||
|
@@ -16,3 +16,9 @@ build: | |
|
||
test: # run unit tests | ||
go test ./... -cover | ||
|
||
start-services: ## start service and dependencies with docker | ||
docker compose -f docker-compose.yml -f docker-compose.service.yml up --build --abort-on-container-exit | ||
|
||
cleanup-services: # delete service, dependencies and all persistent data | ||
docker compose -f docker-compose.yml -f docker-compose.service.yml down -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,96 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/greenbone/opensight-notification-service/pkg/config" | ||
"github.com/greenbone/opensight-notification-service/pkg/logging" | ||
"github.com/greenbone/opensight-notification-service/pkg/repository" | ||
"github.com/greenbone/opensight-notification-service/pkg/repository/notificationrepository" | ||
"github.com/greenbone/opensight-notification-service/pkg/services/healthservice" | ||
"github.com/greenbone/opensight-notification-service/pkg/services/notificationservice" | ||
"github.com/greenbone/opensight-notification-service/pkg/web" | ||
"github.com/greenbone/opensight-notification-service/pkg/web/healthcontroller" | ||
"github.com/greenbone/opensight-notification-service/pkg/web/notificationcontroller" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func main() { | ||
config, err := config.ReadConfig() | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("failed to read config") | ||
} | ||
|
||
err = logging.SetupLogger(config.LogLevel) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("failed to set up logger") | ||
} | ||
|
||
check(run(config)) | ||
} | ||
|
||
func run(config config.Config) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
ctx, stop := signal.NotifyContext(ctx, os.Interrupt) | ||
defer stop() | ||
|
||
pgClient, err := repository.NewClient(config.Database) | ||
if err != nil { | ||
return err | ||
} | ||
notificationRepository, err := notificationrepository.NewNotificationRepository(pgClient) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
notificationService := notificationservice.NewNotificationService(notificationRepository) | ||
healthService := healthservice.NewHealthService(pgClient) | ||
|
||
gin := web.NewWebEngine() | ||
rootRouter := gin.Group("/") | ||
notificationServiceRouter := gin.Group("/api/notification-service") | ||
docsRouter := gin.Group("/docs/notification-service") | ||
|
||
// rest api docs | ||
web.RegisterSwaggerDocsRoute(docsRouter) | ||
healthcontroller.RegisterSwaggerDocsRoute(docsRouter) | ||
|
||
//instantiate controllers | ||
notificationcontroller.NewNotificationController(notificationServiceRouter, notificationService) | ||
healthcontroller.NewHealthController(rootRouter, healthService) // for health probes (not a data source) | ||
|
||
srv := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", config.Http.Port), | ||
Handler: gin, | ||
ReadTimeout: config.Http.ReadTimeout, | ||
WriteTimeout: config.Http.WriteTimeout, | ||
IdleTimeout: config.Http.IdleTimeout, | ||
} | ||
|
||
go func() { | ||
if err := srv.ListenAndServe(); err != http.ErrServerClosed { | ||
check(err) | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
log.Info().Msg("Received signal. Shutting down") | ||
err = srv.Shutdown(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to shut down error: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func check(err error) { | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("critical error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# only to be used in combination with `docker-compose.yml` | ||
|
||
# version: '3' # uncomment for compatibility with old docker compose versions | ||
|
||
services: | ||
notification-service: | ||
build: . # replace this line with `image: ghcr.io/greenbone/notification-service:<desired vibd docker image>` if you want to use an already built image instead of building one from the active working directory | ||
environment: | ||
DB_HOST: postgres | ||
DB_PORT: 5432 | ||
DB_USERNAME: postgres | ||
DB_PASSWORD: $DB_PASSWORD | ||
DB_NAME: notification_service | ||
DB_SSL_MODE: disable | ||
LOG_LEVEL: debug | ||
ports: | ||
- 8085:8085 | ||
networks: | ||
- notification-service-net | ||
depends_on: | ||
postgres: | ||
condition: service_healthy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# version: '3' # uncomment for compatibility with old docker compose versions | ||
|
||
services: | ||
postgres: | ||
image: postgres:16 | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: $DB_PASSWORD | ||
POSTGRES_DB: notification_service | ||
volumes: | ||
- postgres-data:/var/lib/postgresql/data | ||
ports: | ||
- 5432:5432 | ||
networks: | ||
- notification-service-net | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready -U postgres"] | ||
interval: 1s | ||
timeout: 5s | ||
retries: 10 | ||
|
||
volumes: | ||
postgres-data: | ||
|
||
networks: | ||
notification-service-net: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.