diff --git a/Makefile b/Makefile index 75241486..2b80de11 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,11 @@ build: build_base build_web build_glue build_judge ## Builds all services build_base: ## builds njudge-base docker build -t $(PROJECT_NAME)-base . build_web: ## builds njudge-web - docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-web -f internal/web/Dockerfile . + docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-web -f web.Dockerfile . build_glue: ## builds njudge-glue - docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-glue -f internal/glue/Dockerfile . + docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-glue -f glue.Dockerfile . build_judge: ## builds njudge-judge - docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-judge -f internal/judge/Dockerfile . + docker build --build-arg="PROJECT_NAME=$(PROJECT_NAME)" -t $(PROJECT_NAME)-judge -f judge.Dockerfile . up: build ## builds an runs docker-compose up COMPOSE_PROJECT_NAME="$(PROJECT_NAME)" docker-compose up diff --git a/cmd/glue.go b/cmd/glue.go index bf9ebac3..c7357c08 100644 --- a/cmd/glue.go +++ b/cmd/glue.go @@ -11,7 +11,6 @@ import ( "golang.org/x/net/context" "io/fs" "log/slog" - "strings" "time" ) @@ -26,7 +25,7 @@ var DefaultGlueConfig = GlueConfig{ Host: "db", Name: "postgres", Port: 5432, - SSLMode: true, + SSLMode: false, }, } @@ -36,6 +35,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command { Use: "glue", Short: "start glue service", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + v.SetConfigType("yaml") v.SetConfigFile("glue.yaml") v.AddConfigPath(".") @@ -44,7 +44,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command { v.SetDefault("db.host", DefaultGlueConfig.Database.Host) v.SetDefault("db.name", DefaultGlueConfig.Database.Name) v.SetDefault("db.port", DefaultGlueConfig.Database.Port) - v.SetDefault("db.sslmode", DefaultGlueConfig.Database.SSLMode) + v.SetDefault("db.ssl_mode", DefaultGlueConfig.Database.SSLMode) v.AutomaticEnv() v.SetEnvPrefix("njudge") @@ -57,7 +57,7 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command { } cmd.Flags().VisitAll(func(flag *pflag.Flag) { - configName := strings.ReplaceAll(flag.Name, "-", "") + configName := flag.Name if !flag.Changed && v.IsSet(configName) { val := v.Get(configName) _ = cmd.Flags().Set(flag.Name, fmt.Sprintf("%v", val)) @@ -68,10 +68,21 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command { }, RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println(cfg) conn, err := cfg.Database.Connect() if err != nil { return err } + for { + slog.Info("Trying to ping database...") + if err := conn.Ping(); err == nil { + slog.Info("OK, connected to database") + break + } else { + slog.Error("Failed to connect to database", "error", err) + } + time.Sleep(5 * time.Second) + } judges := glue.NewJudges(conn, slog.Default()) go func() { for { @@ -94,10 +105,10 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command { cmd.Flags().StringVar(&cfg.Database.User, "db.user", DefaultGlueConfig.Database.User, "database user") cmd.Flags().StringVar(&cfg.Database.Password, "db.password", DefaultGlueConfig.Database.Password, "database password") - cmd.Flags().StringVar(&cfg.Database.Host, "db.host", DefaultGlueConfig.Database.Password, "database host") + cmd.Flags().StringVar(&cfg.Database.Host, "db.host", DefaultGlueConfig.Database.Host, "database host") cmd.Flags().StringVar(&cfg.Database.Name, "db.name", DefaultGlueConfig.Database.Name, "database name") cmd.Flags().IntVar(&cfg.Database.Port, "db.port", DefaultGlueConfig.Database.Port, "database port") - cmd.Flags().BoolVar(&cfg.Database.SSLMode, "db.sslmode", DefaultGlueConfig.Database.SSLMode, "database sslmode") + cmd.Flags().BoolVar(&cfg.Database.SSLMode, "db.ssl_mode", DefaultGlueConfig.Database.SSLMode, "database sslmode") return cmd } diff --git a/cmd/judge.go b/cmd/judge.go index f4c6003e..742c3030 100644 --- a/cmd/judge.go +++ b/cmd/judge.go @@ -13,20 +13,19 @@ import ( "io/fs" "log/slog" "runtime" - "strings" "time" ) type JudgeConfig struct { - Port int - ProblemsDir string + Port int `mapstructure:"port" yaml:"port"` + ProblemsDir string `mapstructure:"problems_dir" yaml:"problems_dir"` - Isolate bool - IsolateSandboxRange []int + Isolate bool `mapstructure:"isolate" yaml:"isolate"` + IsolateSandboxRange []int `mapstructure:"isolate_sandbox_range" yaml:"isolate_sandbox_range"` - UpdateStatusLimitEvery time.Duration + UpdateStatusLimitEvery time.Duration `mapstructure:"update_status_limit_every" yaml:"update_status_limit_every"` - Concurrency int + Concurrency int `mapstructure:"concurrency" yaml:"concurrency"` } var DefaultJudgeConfig = JudgeConfig{ @@ -48,10 +47,10 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command { v.AddConfigPath(".") v.SetDefault("port", DefaultJudgeConfig.Port) - v.SetDefault("problemsDir", DefaultJudgeConfig.ProblemsDir) + v.SetDefault("problems_dir", DefaultJudgeConfig.ProblemsDir) v.SetDefault("isolate", DefaultJudgeConfig.Isolate) - v.SetDefault("isolateSandboxRange", DefaultJudgeConfig.IsolateSandboxRange) - v.SetDefault("updateStatusLimitEvery", DefaultJudgeConfig.UpdateStatusLimitEvery) + v.SetDefault("isolate_sandbox_range", DefaultJudgeConfig.IsolateSandboxRange) + v.SetDefault("update_status_limit_every", DefaultJudgeConfig.UpdateStatusLimitEvery) v.AutomaticEnv() v.SetEnvPrefix("njudge") @@ -64,7 +63,7 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command { } cmd.Flags().VisitAll(func(flag *pflag.Flag) { - configName := strings.ReplaceAll(flag.Name, "-", "") + configName := flag.Name if !flag.Changed && v.IsSet(configName) { val := v.Get(configName) _ = cmd.Flags().Set(flag.Name, fmt.Sprintf("%v", val)) @@ -131,10 +130,10 @@ func NewJudgeCmd(v *viper.Viper) *cobra.Command { } cmd.Flags().IntVar(&cfg.Port, "port", DefaultJudgeConfig.Port, "port to listen on") - cmd.Flags().StringVar(&cfg.ProblemsDir, "problems-dir", DefaultJudgeConfig.ProblemsDir, "directory of the problems") + cmd.Flags().StringVar(&cfg.ProblemsDir, "problems_dir", DefaultJudgeConfig.ProblemsDir, "directory of the problems") cmd.Flags().BoolVar(&cfg.Isolate, "isolate", DefaultJudgeConfig.Isolate, "use isolate (otherwise dummy sandboxes are used which are NOT secure)") - cmd.Flags().IntSliceVar(&cfg.IsolateSandboxRange, "isolate-sandbox-range", DefaultJudgeConfig.IsolateSandboxRange, "inclusive interval of isolate sandbox IDs") - cmd.Flags().DurationVar(&cfg.UpdateStatusLimitEvery, "updateStatus-limit-every", DefaultJudgeConfig.UpdateStatusLimitEvery, "the rate of status updates for the clients") + cmd.Flags().IntSliceVar(&cfg.IsolateSandboxRange, "isolate_sandbox_range", DefaultJudgeConfig.IsolateSandboxRange, "inclusive interval of isolate sandbox IDs") + cmd.Flags().DurationVar(&cfg.UpdateStatusLimitEvery, "updateStatus_limit_every", DefaultJudgeConfig.UpdateStatusLimitEvery, "the rate of status updates for the clients") cmd.Flags().IntVar(&cfg.Concurrency, "concurrency", DefaultJudgeConfig.Concurrency, "the maximum number of concurrently executed testcase") return cmd diff --git a/configs/docker/glue.yaml b/configs/docker/glue.yaml new file mode 100644 index 00000000..e69de29b diff --git a/configs/docker/glue_docker.json b/configs/docker/glue_docker.json deleted file mode 100644 index fa8e3f3a..00000000 --- a/configs/docker/glue_docker.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "port": "3000", - "DBName": "postgres", - "DBAccount": "postgres", - "DBPassword": "postgres", - "DBHost": "db" -} diff --git a/configs/docker/judge.yaml b/configs/docker/judge.yaml new file mode 100644 index 00000000..6c7b148e --- /dev/null +++ b/configs/docker/judge.yaml @@ -0,0 +1 @@ +problems_dir: /njudge_problems diff --git a/configs/docker/judge_docker.json b/configs/docker/judge_docker.json deleted file mode 100644 index abd466be..00000000 --- a/configs/docker/judge_docker.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "mode": "development", - "id": "default", - "host": "judge", - "port": "8888", - "problems_dir": "/njudge_problems", - "public_key": "", - "log_dir":"/njudge_logs", - "sandbox_ids": "100-200", - "worker_count": 2 -} \ No newline at end of file diff --git a/configs/docker/web_docker.json b/configs/docker/web_docker.json index bcd7e3f8..8ad8c179 100644 --- a/configs/docker/web_docker.json +++ b/configs/docker/web_docker.json @@ -5,10 +5,14 @@ "url": "http://localhost:5555", "problemsDir": "/njudge_problems", "templatesDir": "./internal/web/templates", - "DBName": "postgres", - "DBAccount": "postgres", - "DBPassword": "postgres", - "DBHost": "db", + "database": { + "user": "postgres", + "password": "postgres", + "host": "db", + "name": "postgres", + "port": 5432, + "ssl_mode": false + }, "cookieSecret": "secret_for_c00kies", "customHead": "", diff --git a/internal/glue/Dockerfile b/glue.Dockerfile similarity index 58% rename from internal/glue/Dockerfile rename to glue.Dockerfile index b8f508ac..a79578ba 100644 --- a/internal/glue/Dockerfile +++ b/glue.Dockerfile @@ -1,6 +1,6 @@ ARG PROJECT_NAME FROM ${PROJECT_NAME}-base -COPY configs/docker/glue_docker.json ./glue.json +COPY configs/docker/glue.yaml ./glue.yaml CMD ["./njudge", "glue"] \ No newline at end of file diff --git a/internal/web/helpers/config/config.go b/internal/web/helpers/config/config.go index 1b5df72b..7be23e8c 100644 --- a/internal/web/helpers/config/config.go +++ b/internal/web/helpers/config/config.go @@ -11,12 +11,12 @@ import ( ) type Database struct { - User string `json:"user,omitempty"` - Password string `json:"password,omitempty"` - Host string `json:"host,omitempty"` - Name string `json:"name,omitempty"` - Port int `json:"port,omitempty"` - SSLMode bool `json:"ssl_mode,omitempty"` + User string `mapstructure:"user" yaml:"user" json:"user"` + Password string `mapstructure:"password" yaml:"password" json:"password"` + Host string `mapstructure:"host" yaml:"host" json:"host"` + Name string `mapstructure:"name" yaml:"name" json:"name"` + Port int `mapstructure:"port" yaml:"port" json:"port"` + SSLMode bool `mapstructure:"ssl_mode" yaml:"ssl_mode" json:"ssl_mode"` } func (db Database) Connect() (*sql.DB, error) { diff --git a/internal/judge/Dockerfile b/judge.Dockerfile similarity index 92% rename from internal/judge/Dockerfile rename to judge.Dockerfile index 9ae8afff..3a4ae649 100644 --- a/internal/judge/Dockerfile +++ b/judge.Dockerfile @@ -18,6 +18,6 @@ COPY --from=nimlang/nim:1.6.18 /usr/bin/nim /usr/bin/nim RUN ln -s /usr/local/julia/bin/julia /usr/local/bin/julia WORKDIR /app -COPY configs/docker/judge_docker.json ./judge.json +COPY configs/docker/judge.yaml ./judge.yaml CMD ["./njudge", "judge"] \ No newline at end of file diff --git a/internal/web/Dockerfile b/web.Dockerfile similarity index 100% rename from internal/web/Dockerfile rename to web.Dockerfile