puerta/cmd/server/main.go

57 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-12-30 06:51:51 +00:00
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Roberto Hidalgo <nidito@un.rob.mx>
package server
import (
"fmt"
"net/http"
"os"
"git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/puerta/internal/server"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
2023-04-16 21:17:36 +00:00
var ServerCommand = &command.Command{
2022-12-30 06:51:51 +00:00
Path: []string{"server"},
Summary: "Runs the http server",
Description: "",
Options: command.Options{
"config": {
Type: "string",
Default: "./config.joao.yaml",
},
"db": {
Type: "string",
Default: "./puerta.db",
},
},
Action: func(cmd *command.Command) error {
config := cmd.Options["config"].ToValue().(string)
db := cmd.Options["db"].ToValue().(string)
data, err := os.ReadFile(config)
if err != nil {
return fmt.Errorf("could not read config file: %w", err)
}
cfg := server.ConfigDefaults(db)
if err := yaml.Unmarshal(data, &cfg); err != nil {
return fmt.Errorf("could not unserialize yaml at %s: %w", config, err)
}
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{DisableTimestamp: false})
router, err := server.Initialize(cfg)
if err != nil {
return err
}
2023-01-04 04:40:39 +00:00
logrus.Infof("Listening at %s", cfg.HTTP.Listen)
return http.ListenAndServe(cfg.HTTP.Listen, router)
2022-12-30 06:51:51 +00:00
},
}