|
| 1 | +// Copyright Axis Communications AB. |
| 2 | +// |
| 3 | +// For a full list of individual contributors, please see the commit history. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "net/http" |
| 21 | + "os" |
| 22 | + "os/signal" |
| 23 | + "runtime/debug" |
| 24 | + "syscall" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/eiffel-community/etos-api/internal/config" |
| 28 | + "github.com/eiffel-community/etos-api/internal/logging" |
| 29 | + "github.com/eiffel-community/etos-api/internal/server" |
| 30 | + "github.com/eiffel-community/etos-api/pkg/application" |
| 31 | + v1alpha "github.com/eiffel-community/etos-api/pkg/logarea/v1alpha" |
| 32 | + "github.com/sirupsen/logrus" |
| 33 | + "github.com/snowzach/rotatefilehook" |
| 34 | + "go.elastic.co/ecslogrus" |
| 35 | +) |
| 36 | + |
| 37 | +// main sets up logging and starts up the logarea webservice. |
| 38 | +func main() { |
| 39 | + cfg := config.Get() |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + var hooks []logrus.Hook |
| 43 | + if fileHook := fileLogging(cfg); fileHook != nil { |
| 44 | + hooks = append(hooks, fileHook) |
| 45 | + } |
| 46 | + logger, err := logging.Setup(cfg.LogLevel(), hooks) |
| 47 | + if err != nil { |
| 48 | + logrus.Fatal(err.Error()) |
| 49 | + } |
| 50 | + |
| 51 | + hostname, err := os.Hostname() |
| 52 | + if err != nil { |
| 53 | + logrus.Fatal(err.Error()) |
| 54 | + } |
| 55 | + log := logger.WithFields(logrus.Fields{ |
| 56 | + "hostname": hostname, |
| 57 | + "application": "ETOS API LogArea Server", |
| 58 | + "version": vcsRevision(), |
| 59 | + "name": "ETOS API", |
| 60 | + }) |
| 61 | + |
| 62 | + log.Info("Loading logarea routes") |
| 63 | + v1AlphaLogArea := v1alpha.New(cfg, log) |
| 64 | + defer v1AlphaLogArea.Close() |
| 65 | + |
| 66 | + app := application.New(v1AlphaLogArea) |
| 67 | + srv := server.NewWebService(cfg, log, app) |
| 68 | + |
| 69 | + done := make(chan os.Signal, 1) |
| 70 | + signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) |
| 71 | + |
| 72 | + go func() { |
| 73 | + if err := srv.Start(); err != nil && err != http.ErrServerClosed { |
| 74 | + log.Errorf("Webserver shutdown: %+v", err) |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + sig := <-done |
| 79 | + log.Infof("%s received", sig.String()) |
| 80 | + |
| 81 | + ctx, cancel := context.WithTimeout(ctx, 1*time.Minute) |
| 82 | + defer cancel() |
| 83 | + |
| 84 | + if err := srv.Close(ctx); err != nil { |
| 85 | + log.Errorf("Webserver shutdown failed: %+v", err) |
| 86 | + } |
| 87 | + log.Info("Wait for shutdown to complete") |
| 88 | +} |
| 89 | + |
| 90 | +// fileLogging adds a hook into a slice of hooks, if the filepath configuration is set |
| 91 | +func fileLogging(cfg config.Config) logrus.Hook { |
| 92 | + if filePath := cfg.LogFilePath(); filePath != "" { |
| 93 | + // TODO: Make these parameters configurable. |
| 94 | + // NewRotateFileHook cannot return an error which is why it's set to '_'. |
| 95 | + rotateFileHook, _ := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{ |
| 96 | + Filename: filePath, |
| 97 | + MaxSize: 10, // megabytes |
| 98 | + MaxBackups: 3, |
| 99 | + MaxAge: 0, // days |
| 100 | + Level: logrus.DebugLevel, |
| 101 | + Formatter: &ecslogrus.Formatter{ |
| 102 | + DataKey: "labels", |
| 103 | + }, |
| 104 | + }) |
| 105 | + return rotateFileHook |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// vcsRevision returns vcs revision from build info, if any. Otherwise '(unknown)'. |
| 111 | +func vcsRevision() string { |
| 112 | + buildInfo, ok := debug.ReadBuildInfo() |
| 113 | + if !ok { |
| 114 | + return "(unknown)" |
| 115 | + } |
| 116 | + for _, val := range buildInfo.Settings { |
| 117 | + if val.Key == "vcs.revision" { |
| 118 | + return val.Value |
| 119 | + } |
| 120 | + } |
| 121 | + return "(unknown)" |
| 122 | +} |
0 commit comments