Initial commit
This commit is contained in:
20
hw12_13_14_15_calendar/cmd/calendar/config.go
Normal file
20
hw12_13_14_15_calendar/cmd/calendar/config.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
// При желании конфигурацию можно вынести в internal/config.
|
||||
// Организация конфига в main принуждает нас сужать API компонентов, использовать
|
||||
// при их конструировании только необходимые параметры, а также уменьшает вероятность циклической зависимости.
|
||||
type Config struct {
|
||||
Logger LoggerConf
|
||||
// TODO
|
||||
}
|
||||
|
||||
type LoggerConf struct {
|
||||
Level string
|
||||
// TODO
|
||||
}
|
||||
|
||||
func NewConfig() Config {
|
||||
return Config{}
|
||||
}
|
||||
|
||||
// TODO
|
||||
61
hw12_13_14_15_calendar/cmd/calendar/main.go
Normal file
61
hw12_13_14_15_calendar/cmd/calendar/main.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/fixme_my_friend/hw12_13_14_15_calendar/internal/app"
|
||||
"github.com/fixme_my_friend/hw12_13_14_15_calendar/internal/logger"
|
||||
internalhttp "github.com/fixme_my_friend/hw12_13_14_15_calendar/internal/server/http"
|
||||
memorystorage "github.com/fixme_my_friend/hw12_13_14_15_calendar/internal/storage/memory"
|
||||
)
|
||||
|
||||
var configFile string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&configFile, "config", "/etc/calendar/config.toml", "Path to configuration file")
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if flag.Arg(0) == "version" {
|
||||
printVersion()
|
||||
return
|
||||
}
|
||||
|
||||
config := NewConfig()
|
||||
logg := logger.New(config.Logger.Level)
|
||||
|
||||
storage := memorystorage.New()
|
||||
calendar := app.New(logg, storage)
|
||||
|
||||
server := internalhttp.NewServer(logg, calendar)
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(),
|
||||
syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Stop(ctx); err != nil {
|
||||
logg.Error("failed to stop http server: " + err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
logg.Info("calendar is running...")
|
||||
|
||||
if err := server.Start(ctx); err != nil {
|
||||
logg.Error("failed to start http server: " + err.Error())
|
||||
cancel()
|
||||
os.Exit(1) //nolint:gocritic
|
||||
}
|
||||
}
|
||||
27
hw12_13_14_15_calendar/cmd/calendar/version.go
Normal file
27
hw12_13_14_15_calendar/cmd/calendar/version.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
release = "UNKNOWN"
|
||||
buildDate = "UNKNOWN"
|
||||
gitHash = "UNKNOWN"
|
||||
)
|
||||
|
||||
func printVersion() {
|
||||
if err := json.NewEncoder(os.Stdout).Encode(struct {
|
||||
Release string
|
||||
BuildDate string
|
||||
GitHash string
|
||||
}{
|
||||
Release: release,
|
||||
BuildDate: buildDate,
|
||||
GitHash: gitHash,
|
||||
}); err != nil {
|
||||
fmt.Printf("error while decode version info: %v\n", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user