From eb93593d1944991ce13a24e89c5b3d56cb3fe21c Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Sun, 25 Jun 2023 23:29:06 +0700 Subject: [PATCH 01/15] Add arguments incrementally --- scripts/start_container.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index dc50c73..1d6d668 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -43,8 +43,15 @@ case ${HTTPS} in ;; esac +cmd="kjudge -file /data/kjudge.db" + if [ "${useHTTPS}" = true ]; then - kjudge -port 443 -file /data/kjudge.db -https /certs "$@" + cmd="$cmd -port 443 -https /certs" else - kjudge -port 80 -file /data/kjudge.db "$@" + cmd="$cmd -port 80" fi + +if [ -f "/data/favicon.ico" ]; then + cmd="$cmd -favicon /data/favicon.ico" + +"$cmd $@" From 53012149c4694c42484dea272fab95cbb9b7ea86 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 00:15:03 +0700 Subject: [PATCH 02/15] Allows serving favicon --- cmd/kjudge/main.go | 5 +++++ server/opts.go | 11 +++++++++++ server/server.go | 5 +++++ 3 files changed, 21 insertions(+) diff --git a/cmd/kjudge/main.go b/cmd/kjudge/main.go index 36fe110..cfa3a3b 100644 --- a/cmd/kjudge/main.go +++ b/cmd/kjudge/main.go @@ -16,6 +16,8 @@ import ( var ( dbfile = flag.String("file", "kjudge.db", "Path to the database file.") + faviconfile = flag.String("favicon", "", "Path to favicon file. Will not be served if not specified.") + sandboxImpl = flag.String("sandbox", "isolate", "The sandbox implementation to be used (isolate, raw). Defaults to isolate.") port = flag.Int("port", 8088, "The port for the server to listen on.") verbose = flag.Bool("verbose", false, "Log every http requests") @@ -41,6 +43,9 @@ func main() { if *verbose { opts = append(opts, server.Verbose()) } + if *faviconfile != "" { + opts = append(opts, server.Favicon(*faviconfile)) + } // Start the queue queue := worker.Queue{Sandbox: sandbox, DB: db} diff --git a/server/opts.go b/server/opts.go index 3bc0304..3bf8c8c 100644 --- a/server/opts.go +++ b/server/opts.go @@ -1,5 +1,7 @@ package server +import "os" + // Opt represents an option for the server. type Opt func(s *Server) @@ -9,3 +11,12 @@ func Verbose() Opt { s.verbose = true } } + +func Favicon(path string) Opt { + if _, err := os.Stat(path); err != nil { + panic(err) + } + return func(s *Server) { + s.faviconPath = path + } +} diff --git a/server/server.go b/server/server.go index c44eb08..63f8f17 100644 --- a/server/server.go +++ b/server/server.go @@ -30,6 +30,7 @@ type Server struct { echo *echo.Echo verbose bool + faviconPath string } // New creates a new server. @@ -37,6 +38,9 @@ func New(db *db.DB, opts ...Opt) (*Server, error) { s := &Server{ db: db, echo: echo.New(), + + verbose: false, + faviconPath: "", } for _, opt := range opts { @@ -85,6 +89,7 @@ func New(db *db.DB, opts ...Opt) (*Server, error) { return nil, err } s.echo.GET("", contests.ConestsGetNearestOngoingContest) + s.echo.Static("/favicon.ico", s.faviconPath) s.echo.Group("/static").GET("*", StaticFiles) s.echo.GET("*", StaticFiles) s.echo.POST("*", NotFoundHandler) From e49abfb11df066971ed045a494bd90dfd3ef96bf Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 00:17:49 +0700 Subject: [PATCH 03/15] gofmt --- server/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/server.go b/server/server.go index 63f8f17..8a36d4a 100644 --- a/server/server.go +++ b/server/server.go @@ -29,7 +29,7 @@ type Server struct { db *db.DB echo *echo.Echo - verbose bool + verbose bool faviconPath string } @@ -39,7 +39,7 @@ func New(db *db.DB, opts ...Opt) (*Server, error) { db: db, echo: echo.New(), - verbose: false, + verbose: false, faviconPath: "", } From 2b1e849aec850e87cd09841c7b999c50065faa53 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 00:28:16 +0700 Subject: [PATCH 04/15] Fix start_container.sh file --- scripts/start_container.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index 1d6d668..0c31c17 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -53,5 +53,6 @@ fi if [ -f "/data/favicon.ico" ]; then cmd="$cmd -favicon /data/favicon.ico" +fi -"$cmd $@" +$cmd $@ From b77d6b199a0d67c505a4589b0e716e1b462ed3e1 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:08:12 +0700 Subject: [PATCH 05/15] WIP --- cmd/kjudge/main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/kjudge/main.go b/cmd/kjudge/main.go index cfa3a3b..369e78c 100644 --- a/cmd/kjudge/main.go +++ b/cmd/kjudge/main.go @@ -43,8 +43,12 @@ func main() { if *verbose { opts = append(opts, server.Verbose()) } + if *faviconfile != "" { + log.Printf("Serving favicon from %s", *faviconfile) opts = append(opts, server.Favicon(*faviconfile)) + } else { + log.Printf("Not serving favicon") } // Start the queue From 38647a1e81b2a5b3af267b3e29f654b669a11881 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:23:12 +0700 Subject: [PATCH 06/15] Add favicon to echo static only if specified --- cmd/kjudge/main.go | 2 +- server/server.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/kjudge/main.go b/cmd/kjudge/main.go index 369e78c..cb94622 100644 --- a/cmd/kjudge/main.go +++ b/cmd/kjudge/main.go @@ -43,7 +43,7 @@ func main() { if *verbose { opts = append(opts, server.Verbose()) } - + if *faviconfile != "" { log.Printf("Serving favicon from %s", *faviconfile) opts = append(opts, server.Favicon(*faviconfile)) diff --git a/server/server.go b/server/server.go index 8a36d4a..0914799 100644 --- a/server/server.go +++ b/server/server.go @@ -74,6 +74,10 @@ func New(db *db.DB, opts ...Opt) (*Server, error) { s.SetupProfiling() + if s.faviconPath != "" { + s.echo.Static("/favicon.ico", s.faviconPath) + } + au, err := auth.NewAdmin() if err != nil { return nil, err @@ -89,7 +93,6 @@ func New(db *db.DB, opts ...Opt) (*Server, error) { return nil, err } s.echo.GET("", contests.ConestsGetNearestOngoingContest) - s.echo.Static("/favicon.ico", s.faviconPath) s.echo.Group("/static").GET("*", StaticFiles) s.echo.GET("*", StaticFiles) s.echo.POST("*", NotFoundHandler) From dc402ca09b550ab080e30677cb9ae7a2688379a2 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:26:29 +0700 Subject: [PATCH 07/15] Comments for favicon option --- server/opts.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/opts.go b/server/opts.go index 3bf8c8c..e5011fd 100644 --- a/server/opts.go +++ b/server/opts.go @@ -12,6 +12,7 @@ func Verbose() Opt { } } +// Favicon makes the server serves given file at /favicon.ico func Favicon(path string) Opt { if _, err := os.Stat(path); err != nil { panic(err) From a33075692527bc7ba3271e9035aaaa74c719352a Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:30:04 +0700 Subject: [PATCH 08/15] Run gofmt --- cmd/kjudge/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/kjudge/main.go b/cmd/kjudge/main.go index cb94622..369e78c 100644 --- a/cmd/kjudge/main.go +++ b/cmd/kjudge/main.go @@ -43,7 +43,7 @@ func main() { if *verbose { opts = append(opts, server.Verbose()) } - + if *faviconfile != "" { log.Printf("Serving favicon from %s", *faviconfile) opts = append(opts, server.Favicon(*faviconfile)) From 2713eacaa0fe43ca12bf49751dbff0260c7dd366 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:31:10 +0700 Subject: [PATCH 09/15] append $@ to cmd before running in start_container.sh --- scripts/start_container.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index 0c31c17..736c541 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -55,4 +55,6 @@ if [ -f "/data/favicon.ico" ]; then cmd="$cmd -favicon /data/favicon.ico" fi -$cmd $@ +cmd="$cmd $@" + +$cmd From d0ddb56c79226d461ee8e5b87a80553a272f501c Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:34:08 +0700 Subject: [PATCH 10/15] Change $@ to $* --- scripts/start_container.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index 736c541..fcbdffe 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -55,6 +55,6 @@ if [ -f "/data/favicon.ico" ]; then cmd="$cmd -favicon /data/favicon.ico" fi -cmd="$cmd $@" +cmd="$cmd $*" $cmd From 233ef34a4c26010548d160a876986c46e53bab11 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:43:39 +0700 Subject: [PATCH 11/15] Docker uses symlink for bind mounts so switching to echo#File --- server/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 0914799..82f63ab 100644 --- a/server/server.go +++ b/server/server.go @@ -75,7 +75,7 @@ func New(db *db.DB, opts ...Opt) (*Server, error) { s.SetupProfiling() if s.faviconPath != "" { - s.echo.Static("/favicon.ico", s.faviconPath) + s.echo.File("/favicon.ico", s.faviconPath) } au, err := auth.NewAdmin() From b20b92d2efba0764e1a3a05155c1c20cccd0b6b2 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:18:00 +0700 Subject: [PATCH 12/15] Fix start_container.sh --- scripts/start_container.sh | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index fcbdffe..e507224 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -19,7 +19,10 @@ showUsage() { - CERT_O [nki inc.] Certificate Organization Name - CERT_CN [kjudge] Certificate Common name - CERT_EMAIL [not@nkagami.me] Certificate Email address - - CERT_ALTNAMES [IP:127.0.0.1,DNS:localhost] A list of hosts that kjudge will be listening on, either by IP (as 'IP:1.2.3.4') or DNS (as 'DNS:google.com'), separated by ','" + - CERT_ALTNAMES [IP:127.0.0.1,DNS:localhost] A list of hosts that kjudge will be listening on, either by IP (as 'IP:1.2.3.4') or DNS (as 'DNS:google.com'), separated by ',' + + favicon usage: + If /data/favicon.ico, start kjudge with favicon support." } if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then @@ -43,18 +46,17 @@ case ${HTTPS} in ;; esac -cmd="kjudge -file /data/kjudge.db" +cmd="kjudge" +args="-file /data/kjudge.db" if [ "${useHTTPS}" = true ]; then - cmd="$cmd -port 443 -https /certs" + args="$args -port 443 -https /certs" else - cmd="$cmd -port 80" + args="$args -port 80" fi if [ -f "/data/favicon.ico" ]; then - cmd="$cmd -favicon /data/favicon.ico" + args="$args -favicon /data/favicon.ico" fi -cmd="$cmd $*" - -$cmd +$cmd $args $* From 5d73caf36f9cb0ff22e470c5f33d437e82d316f4 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:23:53 +0700 Subject: [PATCH 13/15] Favicon return err --- cmd/kjudge/main.go | 6 +++++- server/opts.go | 12 ++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/kjudge/main.go b/cmd/kjudge/main.go index 369e78c..e8ffc90 100644 --- a/cmd/kjudge/main.go +++ b/cmd/kjudge/main.go @@ -46,7 +46,11 @@ func main() { if *faviconfile != "" { log.Printf("Serving favicon from %s", *faviconfile) - opts = append(opts, server.Favicon(*faviconfile)) + opt, err := server.Favicon(*faviconfile) + if err != nil { + panic(err) + } + opts = append(opts, opt) } else { log.Printf("Not serving favicon") } diff --git a/server/opts.go b/server/opts.go index e5011fd..693f2d4 100644 --- a/server/opts.go +++ b/server/opts.go @@ -1,6 +1,10 @@ package server -import "os" +import ( + "os" + + "github.com/pkg/errors" +) // Opt represents an option for the server. type Opt func(s *Server) @@ -13,11 +17,11 @@ func Verbose() Opt { } // Favicon makes the server serves given file at /favicon.ico -func Favicon(path string) Opt { +func Favicon(path string) (Opt, error) { if _, err := os.Stat(path); err != nil { - panic(err) + return nil, errors.Wrap(err, "while searching for favicon") } return func(s *Server) { s.faviconPath = path - } + }, nil } From fd6d67a8d3b6c9583e73266c41fa53a1647833a0 Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:27:11 +0700 Subject: [PATCH 14/15] Double quote --- scripts/start_container.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index e507224..9fb20e9 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -59,4 +59,4 @@ if [ -f "/data/favicon.ico" ]; then args="$args -favicon /data/favicon.ico" fi -$cmd $args $* +"$cmd $args $@" From fea0c7b190b41d87f3a337d903c2900e4e07744d Mon Sep 17 00:00:00 2001 From: minhnhatnoe <86871862+minhnhatnoe@users.noreply.github.com> Date: Mon, 26 Jun 2023 22:45:36 +0700 Subject: [PATCH 15/15] shellcheck disable --- scripts/start_container.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/start_container.sh b/scripts/start_container.sh index 9fb20e9..9f7c8de 100755 --- a/scripts/start_container.sh +++ b/scripts/start_container.sh @@ -59,4 +59,5 @@ if [ -f "/data/favicon.ico" ]; then args="$args -favicon /data/favicon.ico" fi -"$cmd $args $@" +# shellcheck disable=SC2086 +$cmd $args "$@"