-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic IUT provider for ETOS API (#73)
- Loading branch information
Showing
32 changed files
with
1,177 additions
and
28 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
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,133 @@ | ||
// Copyright 2022 Axis Communications AB. | ||
// | ||
// For a full list of individual contributors, please see the commit history. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"runtime/debug" | ||
"syscall" | ||
"time" | ||
|
||
config "github.com/eiffel-community/etos-api/internal/configs/iut" | ||
"github.com/eiffel-community/etos-api/internal/logging" | ||
server "github.com/eiffel-community/etos-api/internal/server" | ||
"github.com/eiffel-community/etos-api/pkg/application" | ||
"github.com/eiffel-community/etos-api/pkg/iut/v1alpha1" | ||
"github.com/sirupsen/logrus" | ||
"github.com/snowzach/rotatefilehook" | ||
"go.elastic.co/ecslogrus" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
// main sets up logging and starts up the webserver. | ||
func main() { | ||
cfg := config.Get() | ||
ctx := context.Background() | ||
|
||
var hooks []logrus.Hook | ||
if fileHook := fileLogging(cfg); fileHook != nil { | ||
hooks = append(hooks, fileHook) | ||
} | ||
|
||
logger, err := logging.Setup(cfg.LogLevel(), hooks) | ||
if err != nil { | ||
logrus.Fatal(err.Error()) | ||
} | ||
|
||
hostname, err := os.Hostname() | ||
if err != nil { | ||
logrus.Fatal(err.Error()) | ||
} | ||
log := logger.WithFields(logrus.Fields{ | ||
"hostname": hostname, | ||
"application": "ETOS IUT Provider Service Mini", | ||
"version": vcsRevision(), | ||
"name": "ETOS IUT Provider Mini", | ||
"user_log": false, | ||
}) | ||
|
||
// Database connection test | ||
cli, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{cfg.DatabaseURI()}, | ||
DialTimeout: 5 * time.Second, | ||
}) | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to create etcd connection") | ||
} | ||
|
||
log.Info("Loading v1alpha1 routes") | ||
v1alpha1App := v1alpha1.New(cfg, log, ctx, cli) | ||
defer v1alpha1App.Close() | ||
router := application.New(v1alpha1App) | ||
|
||
srv := server.NewWebService(cfg, log, router) | ||
|
||
done := make(chan os.Signal, 1) | ||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
go func() { | ||
if err := srv.Start(); err != nil && err != http.ErrServerClosed { | ||
log.Errorf("Webserver shutdown: %+v", err) | ||
} | ||
}() | ||
|
||
<-done | ||
log.Info("SIGTERM received") | ||
|
||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancel() | ||
v1alpha1App.Close() | ||
|
||
if err := srv.Close(ctx); err != nil { | ||
log.Errorf("Webserver shutdown failed: %+v", err) | ||
} | ||
} | ||
|
||
// fileLogging adds a hook into a slice of hooks, if the filepath configuration is set | ||
func fileLogging(cfg config.Config) logrus.Hook { | ||
if filePath := cfg.LogFilePath(); filePath != "" { | ||
// TODO: Make these parameters configurable. | ||
// NewRotateFileHook cannot return an error which is why it's set to '_'. | ||
rotateFileHook, _ := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{ | ||
Filename: filePath, | ||
MaxSize: 10, // megabytes | ||
MaxBackups: 3, | ||
MaxAge: 0, // days | ||
Level: logrus.DebugLevel, | ||
Formatter: &ecslogrus.Formatter{ | ||
DataKey: "labels", | ||
}, | ||
}) | ||
return rotateFileHook | ||
} | ||
return nil | ||
} | ||
|
||
func vcsRevision() string { | ||
buildInfo, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return "(unknown)" | ||
} | ||
for _, val := range buildInfo.Settings { | ||
if val.Key == "vcs.revision" { | ||
return val.Value | ||
} | ||
} | ||
return "(unknown)" | ||
} |
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
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.21-alpine AS build | ||
WORKDIR /tmp/iut | ||
COPY . . | ||
RUN apk add --no-cache make=4.4.1-r2 git=2.45.2-r0 && make iut | ||
|
||
FROM alpine:3.17.3 | ||
ARG TZ | ||
ENV TZ=$TZ | ||
|
||
LABEL org.opencontainers.image.source=https://github.com/eiffel-community/etos-api | ||
LABEL org.opencontainers.image.authors=etos-maintainers@googlegroups.com | ||
LABEL org.opencontainers.image.licenses=Apache-2.0 | ||
|
||
RUN apk add --no-cache tzdata=2024a-r0 | ||
ENTRYPOINT ["/app/iut"] | ||
|
||
COPY --from=build /tmp/iut/bin/iut /app/iut |
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,8 @@ | ||
FROM golang:1.21 | ||
WORKDIR /app | ||
|
||
COPY ./go.mod ./go.sum ./ | ||
RUN go mod tidy | ||
COPY . . | ||
RUN git config --global --add safe.directory /app | ||
EXPOSE 8080 |
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,16 @@ | ||
version: "3.7" | ||
services: | ||
etos-iut: | ||
build: | ||
context: . | ||
dockerfile: ./deploy/etos-iut/Dockerfile.dev | ||
args: | ||
http_proxy: "${http_proxy}" | ||
https_proxy: "${https_proxy}" | ||
volumes: | ||
- ./:/app | ||
ports: | ||
- 8080:8080 | ||
env_file: | ||
- ./configs/development.env | ||
entrypoint: ["/app/bin/iut"] |
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
Oops, something went wrong.