feat: added a single development cli command

next
Antonio De Lucreziis 2 years ago
parent d33a4f25ff
commit d185e81ab2

@ -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
```

@ -18,7 +18,6 @@ func main() {
sl.InjectValue(l, config.Slot, &config.Config{
Mode: "production",
Host: ":4000",
})
sl.InjectValue(l, database.Slot, database.Database(

@ -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)
}
}

@ -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,

Loading…
Cancel
Save