// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"gateway/internal/config"
	"gateway/internal/handler"
	"gateway/internal/library/errors/code"
	"gateway/internal/response"
	"gateway/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/gateway.yaml", "the config file")

func main() {
	flag.Parse()

	response.RequestErrScope = code.Facade

	var c config.Config
	conf.MustLoad(*configFile, &c)

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	sc := svc.NewServiceContext(c)
	// Middlewares are now mounted per route group via .api `middleware:`
	// directives (AuthJWT / CasbinRBAC). See generate/api/README.md.
	handler.RegisterHandlers(server, sc)

	workerCtx, stopWorkers := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer stopWorkers()
	sc.StartWorkers(workerCtx)
	defer sc.StopWorkers()

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
