generated from telekom/reuse-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.
Merge remote-tracking branch 'origin/develop'
- Loading branch information
Showing
21 changed files
with
1,210 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
# SPDX-FileCopyrightText: 2023 Deutsche Telekom AG | ||
# | ||
# SPDX-License-Identifier: CC0-1.0 | ||
.idea | ||
.vscode | ||
.run | ||
.fleet | ||
*.yml | ||
*.exe | ||
*.cmd | ||
*.bat | ||
config.yml |
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,7 @@ | ||
include: | ||
- project: dhei/teams/pandora/gitlab-ci-templates | ||
file: products/gocomponents/GoComponent.Pipeline.gitlab-ci.yml | ||
ref: develop | ||
|
||
variables: | ||
TARGET_IMAGE_SUBGROUP: horizon |
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,17 @@ | ||
FROM golang:1.22-alpine AS build | ||
ARG GOPROXY | ||
ARG GONOSUMDB | ||
ENV GOPROXY=$GOPROXY | ||
ENV GONOSUMDB=$GONOSUMDB | ||
|
||
WORKDIR /build | ||
COPY . . | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s -extldflags=-static" -o golaris . | ||
|
||
FROM alpine:latest as certs | ||
RUN apk --update add ca-certificates | ||
|
||
FROM scratch | ||
COPY --from=build /build/golaris golaris | ||
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
ENTRYPOINT ["./golaris", "serve"] |
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,9 @@ | ||
package cmd | ||
|
||
import "github.com/rs/zerolog/log" | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.Panic().Err(err).Msg("Could not execute root command!") | ||
} | ||
} |
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,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"golaris/config" | ||
) | ||
|
||
var initCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "Initialize a local configuration for testing", | ||
Run: initializeConfig, | ||
} | ||
|
||
func initializeConfig(cmd *cobra.Command, args []string) { | ||
if err := config.InitConfig(); err != nil { | ||
handleConfigInitErr(err) | ||
return | ||
} | ||
log.Info().Msg("Configuration initialized") | ||
} | ||
|
||
func handleConfigInitErr(err error) { | ||
var configFileAlreadyExistsError viper.ConfigFileAlreadyExistsError | ||
if errors.As(err, &configFileAlreadyExistsError) { | ||
log.Error().Msg("Configuration file already exists") | ||
} | ||
} |
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,12 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func init() { | ||
rootCmd.AddCommand(initCmd, serveCmd) | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "Golaris", | ||
Short: "Our service for handling circuitBreakerMessages", | ||
} |
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 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"golaris/config" | ||
"golaris/service" | ||
) | ||
|
||
var serveCmd = &cobra.Command{ | ||
Use: "serve", | ||
Short: "Starts the actual service", | ||
Run: startGolarisService, | ||
} | ||
|
||
func startGolarisService(cmd *cobra.Command, args []string) { | ||
log.Info().Msg("Starting Golaris service") | ||
config.LoadConfiguration() | ||
|
||
service.InitializeService() | ||
listenApiPort() | ||
} | ||
|
||
func listenApiPort() { | ||
service.Listen(config.Current.Port) | ||
} |
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,42 @@ | ||
package config | ||
|
||
type Configuration struct { | ||
LogLevel string `mapstructure:"logLevel"` | ||
|
||
Port int `mapstructure:"port"` | ||
|
||
Security Security `mapstructure:"security"` | ||
Tracing Tracing `mapstructure:"tracing"` | ||
|
||
Hazelcast Hazelcast `mapstructure:"hazelcast"` | ||
Kafka Kafka `mapstructure:"kafka"` | ||
Mongo Mongo `mapstructure:"mongo"` | ||
} | ||
|
||
type Security struct { | ||
Enabled bool `mapstructure:"enabled"` | ||
TrustedIssuers []string `mapstructure:"trustedIssuers"` | ||
} | ||
|
||
type Tracing struct { | ||
Enabled bool `mapstructure:"enabled"` | ||
CollectorEndpoint string `mapstructure:"collectorEndpoint"` | ||
Https bool `mapstructure:"https"` | ||
DebugEnabled bool `mapstructure:"debugEnabled"` | ||
} | ||
|
||
type Hazelcast struct { | ||
ServiceDNS string `mapstructure:"serviceDNS"` | ||
} | ||
|
||
type Kafka struct { | ||
Brokers []string `mapstructure:"brokers"` | ||
Topics []string `mapstructure:"topics"` | ||
} | ||
|
||
type Mongo struct { | ||
Url string `mapstructure:"url"` | ||
Database string `mapstructure:"database"` | ||
Collection string `mapstructure:"collection"` | ||
BulkSize int `mapstructure:"bulkSize"` | ||
} |
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,97 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/viper" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var Current Configuration | ||
|
||
func LoadConfiguration() { | ||
configureViper() | ||
setDefaults() | ||
readConfiguration() | ||
|
||
if err := viper.Unmarshal(&Current); err != nil { | ||
log.Fatal().Err(err).Msg("Could not unmarshal current configuration!") | ||
} | ||
|
||
applyLogLevel(Current.LogLevel) | ||
} | ||
|
||
func InitConfig() error { | ||
configureViper() | ||
setDefaults() | ||
return viper.SafeWriteConfig() | ||
} | ||
|
||
func configureViper() { | ||
viper.SetConfigName("config") | ||
viper.SetConfigType("yml") | ||
viper.AddConfigPath(".") | ||
viper.SetEnvPrefix("golaris") | ||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
} | ||
|
||
func setDefaults() { | ||
viper.SetDefault("logLevel", "info") | ||
|
||
viper.SetDefault("port", 8080) | ||
|
||
// Security | ||
viper.SetDefault("security.enabled", true) | ||
viper.SetDefault("security.trustedIssuers", []string{"iris"}) | ||
|
||
// Tracing | ||
viper.SetDefault("tracing.enabled", true) | ||
viper.SetDefault("tracing.collectorEndpoint", "http://localhost:4318") | ||
viper.SetDefault("tracing.https", true) | ||
viper.SetDefault("tracing.debugEnabled", false) | ||
|
||
// Hazelcast | ||
viper.SetDefault("hazelcast.serviceDNS", "localhost:5701") | ||
|
||
// Kafka | ||
viper.SetDefault("kafka.brokers", "localhost:9092") | ||
viper.SetDefault("kafka.topics", []string{"status"}) | ||
|
||
// Mongo | ||
viper.SetDefault("mongo.url", "mongodb://localhost:27017") | ||
viper.SetDefault("mongo.database", "horizon") | ||
viper.SetDefault("mongo.collection", "status") | ||
viper.SetDefault("mongo.bulkSize", 50) | ||
} | ||
|
||
func readConfiguration() *Configuration { | ||
if err := viper.ReadInConfig(); err != nil { | ||
var configFileNotFoundError viper.ConfigFileNotFoundError | ||
if !errors.As(err, &configFileNotFoundError) { | ||
log.Info().Msg("Configuration file not found but environment variables will be taken into account!") | ||
} | ||
} | ||
viper.AutomaticEnv() | ||
|
||
var config Configuration | ||
if err := viper.Unmarshal(&config); err != nil { | ||
log.Fatal().Err(err).Msg("Could not unmarshal current configuration!") | ||
} | ||
|
||
return &config | ||
} | ||
|
||
func applyLogLevel(level string) { | ||
logLevel, err := zerolog.ParseLevel(level) | ||
if err != nil { | ||
logLevel = zerolog.InfoLevel | ||
log.Info().Msgf("Invalid log level %s. Info log level is used", logLevel) | ||
} | ||
|
||
log.Logger = zerolog.New(os.Stdout).Level(logLevel).With().Timestamp().Logger() | ||
if logLevel == zerolog.DebugLevel { | ||
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stdout}) | ||
} | ||
} |
Oops, something went wrong.