diff --git a/README.md b/README.md index b629bae..bb2f460 100644 --- a/README.md +++ b/README.md @@ -3,22 +3,13 @@ ## Development ```bash shell -# First start in background the go backend on port :4000 -$ go run -v ./cmd/server - -# The start the frontend server on port :3000 -$ pnpm run dev +# Starts the backend on port :4000 and the frontend development server on port :3000 +$ go run -v ./cmd/devserver ``` ## Production ```bash shell -# scaffold the whole server without actually starting the server, this will generate a JSON file containing all routes mount-points for the ViteJS build -$ go run -v ./cmd/build - -# build all frontend pages and assets -$ pnpm run build - -# Now we have all the files and could also ship everything in a single binary -$ go build -o ./out/bin/server ./cmd/server +# Generates "routes.json", builds all frontend artifacts and finally the main binary +$ make build ``` diff --git a/cmd/build/main.go b/cmd/build/main.go index 60b45cd..7871c5a 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -18,7 +18,6 @@ func main() { sl.InjectValue(l, config.Slot, &config.Config{ Mode: "production", - Host: ":4000", }) sl.InjectValue(l, database.Slot, database.Database( diff --git a/cmd/devserver/main.go b/cmd/devserver/main.go new file mode 100644 index 0000000..1eccc28 --- /dev/null +++ b/cmd/devserver/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "bufio" + "io" + "log" + "os/exec" + "phc/website/model" + "phc/website/services/config" + "phc/website/services/database" + "phc/website/services/server" + "phc/website/sl" +) + +func main() { + l := sl.New() + + cfg := sl.InjectValue(l, config.Slot, &config.Config{ + Mode: "development", + Host: ":4000", + }) + + sl.InjectValue[database.Database](l, database.Slot, &database.Memory{ + Users: []model.User{ + { + Id: "claire", + FullName: "Claire Doe", + Nickname: "claire-doe", + AuthSources: map[string]model.AuthSource{}, + }, + { + Id: "john", + FullName: "John Smith", + Nickname: "john-smith", + AuthSources: map[string]model.AuthSource{}, + }, + }, + }) + + srv, err := server.Configure(l) + if err != nil { + log.Fatal(err) + } + + go func() { + log.Fatal(srv.Router.Listen(cfg.Host)) + }() + + r, w := io.Pipe() + + cmd := exec.Command("npm", "run", "dev") + cmd.Stdout = w + + go func() { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + log.Printf(`[ViteJS] %s`, scanner.Text()) + } + }() + + if err := cmd.Run(); err != nil { + log.Fatal(err) + } +} diff --git a/services/server/dev/dev.go b/services/server/dev/dev.go index 930551b..a2d4547 100644 --- a/services/server/dev/dev.go +++ b/services/server/dev/dev.go @@ -6,6 +6,7 @@ import ( "io" "log" "path" + "phc/website/services/config" "phc/website/services/server/routes" "phc/website/sl" @@ -84,6 +85,11 @@ func Configure(l *sl.ServiceLocator) (*devService, error) { return nil, err } + config, _ := sl.Use(l, config.Slot) + if config.Mode != "development" { + return d, nil + } + r.Get("/api/development/routes", func(c *fiber.Ctx) error { return c.JSON(map[string]any{ "static": d.staticRoutes,