Files
nikita-tokarch-uk/cmd/renderer/main.go
T

91 lines
1.9 KiB
Go
Raw Normal View History

2021-12-03 02:15:05 +01:00
package main
import (
2021-12-03 02:15:47 +01:00
"fmt"
2021-12-03 02:26:58 +01:00
"net"
2021-12-03 04:01:50 +01:00
"time"
2021-12-03 02:15:47 +01:00
2021-12-03 02:15:05 +01:00
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
2021-12-03 02:26:58 +01:00
"github.com/valyala/fasthttp"
2021-12-03 02:17:27 +01:00
2021-12-16 21:57:05 +01:00
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/config"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/ghost/v4api/httpclient"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/routes"
"code.tokarch.uk/mainnika/nikita-tokarch-uk/pkg/templates"
2021-12-03 02:15:05 +01:00
)
2021-12-03 02:15:47 +01:00
var Version = "nightly"
2021-12-03 02:19:47 +01:00
const frontendServerIdentity = "Microsoft-IIS/6.0"
2021-12-03 02:15:05 +01:00
func main() {
2021-12-03 02:15:47 +01:00
if viper.GetBool("version") {
fmt.Print(Version)
return
}
2021-12-03 02:15:05 +01:00
viper.SetConfigFile(viper.GetString("config"))
if err := viper.ReadInConfig(); err != nil {
logrus.Debugf("Skip invalid config file %s, %v", viper.ConfigFileUsed(), err)
}
2021-12-03 02:15:47 +01:00
2021-12-03 02:17:27 +01:00
config := &config.Config{}
if err := viper.Unmarshal(config); err != nil {
logrus.Warnf("Cannot unmarshal config, %v", err)
}
2021-12-03 02:26:58 +01:00
var netw, addr string
switch {
case config.Addr != "":
netw = "tcp"
addr = config.Addr
case config.Unix != "":
netw = "unix"
addr = config.Unix
default:
panic("no address given")
}
netListener, err := net.Listen(netw, addr)
if err != nil {
logrus.Fatal(err)
}
templateFuncs := &templates.Funcs{Version: Version}
err = templates.Load(templateFuncs)
if err != nil {
logrus.Fatal(err)
}
2021-12-16 21:57:05 +01:00
ghostClient := &httpclient.HTTPClient{
2021-12-11 02:39:28 +01:00
Addr: config.Content.Backend.Addr,
Secured: config.Content.Backend.Secured,
Headers: config.Content.Backend.Headers,
2021-12-03 04:01:50 +01:00
ContentKey: config.Content.Key,
QueryTimeout: time.Second,
}
2021-12-16 21:57:05 +01:00
apiRoutes := &routes.Routes{
2021-12-03 04:01:50 +01:00
GhostClient: ghostClient,
2021-12-03 04:03:51 +01:00
ContentConfig: config.Content,
2021-12-03 02:58:37 +01:00
Base: config.Base,
}
2021-12-03 02:26:58 +01:00
httpServer := fasthttp.Server{
Logger: logrus.StandardLogger(),
2021-12-16 21:57:05 +01:00
Handler: apiRoutes.Handler,
2021-12-03 02:19:47 +01:00
Name: frontendServerIdentity,
2021-12-03 02:26:58 +01:00
GetOnly: true,
}
2021-12-03 02:15:47 +01:00
logrus.Infof("Version: %s", Version)
2021-12-03 02:26:58 +01:00
logrus.Debugf("Addr: %s+%s", netw, addr)
2021-12-03 02:17:27 +01:00
logrus.Debugf("Conf: %#v", config)
2021-12-03 02:26:58 +01:00
err = httpServer.Serve(netListener)
if err != nil {
logrus.Fatal(err)
}
2021-12-03 02:15:05 +01:00
}