diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:41:43 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:41:43 +0800 |
commit | 1351bbae5610caa1cb6ac9b727ff5296b157273c (patch) | |
tree | 01b2c081816dc75e06d32231948fed3d8df104d1 | |
parent | Ensure server is ready before Run() (diff) | |
download | forge-1351bbae5610caa1cb6ac9b727ff5296b157273c.tar.gz forge-1351bbae5610caa1cb6ac9b727ff5296b157273c.tar.zst forge-1351bbae5610caa1cb6ac9b727ff5296b157273c.zip |
Slight refactor on NewServer
-rw-r--r-- | cmd/forge/main.go | 11 | ||||
-rw-r--r-- | config.go | 2 | ||||
-rw-r--r-- | 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() } @@ -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 @@ -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 { |