From 1351bbae5610caa1cb6ac9b727ff5296b157273c Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 6 Apr 2025 01:41:43 +0800 Subject: Slight refactor on NewServer --- cmd/forge/main.go | 11 +---------- config.go | 2 +- server.go | 10 ++++++++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cmd/forge/main.go b/cmd/forge/main.go index 102f4da..07a9bfd 100644 --- a/cmd/forge/main.go +++ b/cmd/forge/main.go @@ -5,8 +5,6 @@ package main import ( "flag" - "log/slog" - "os" "go.lindenii.runxiyu.org/forge" ) @@ -19,14 +17,7 @@ func main() { ) flag.Parse() - s := forge.Server{} - - s.Setup() - - if err := s.LoadConfig(*configPath); err != nil { - slog.Error("loading configuration", "error", err) - os.Exit(1) - } + s := forge.NewServer(*configPath) s.Run() } diff --git a/config.go b/config.go index d845ce8..8ed6eb7 100644 --- a/config.go +++ b/config.go @@ -61,7 +61,7 @@ type Config struct { // it to the global [config] struct. This may race with concurrent reads from // [config]; additional synchronization is necessary if the configuration is to // be made reloadable. -func (s *Server) LoadConfig(path string) (err error) { +func (s *Server) loadConfig(path string) (err error) { var configFile *os.File if configFile, err = os.Open(path); err != nil { return err diff --git a/server.go b/server.go index fdedf3c..3a90e1c 100644 --- a/server.go +++ b/server.go @@ -49,14 +49,18 @@ type Server struct { ready bool } -func (s *Server) Setup() { +func (s *Server) NewServer(configPath string) error { + if err := s.loadConfig(configPath); err != nil { + return err + } + s.sourceHandler = http.StripPrefix( "/-/source/", http.FileServer(http.FS(embeddedSourceFS)), ) staticFS, err := fs.Sub(embeddedResourcesFS, "static") if err != nil { - panic(err) + return err } s.staticHandler = http.StripPrefix("/-/static/", http.FileServer(http.FS(staticFS))) s.globalData = map[string]any{ @@ -72,6 +76,8 @@ func (s *Server) Setup() { misc.NoneOrPanic(os.Chmod(filepath.Join(s.config.Hooks.Execs, "pre-receive"), 0o755)) s.ready = true + + return nil } func (s *Server) Run() error { -- cgit v1.2.3