2024-08-06 22:13:37 +07:00
|
|
|
package serve
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2024-08-07 10:41:00 +07:00
|
|
|
"os"
|
2024-08-06 22:13:37 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"connectrpc.com/connect"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/stephenafamo/bob"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/api"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/gen/proto/device/v1/v1connect"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
2024-08-07 10:41:00 +07:00
|
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/log"
|
2024-08-06 22:13:37 +07:00
|
|
|
"github.com/tigorlazuardi/bluemage/go/server"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Cmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-08-07 10:41:00 +07:00
|
|
|
db, err := bob.Open("sqlite3", "file:data.db")
|
2024-08-06 22:13:37 +07:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err, "failed to open database")
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:41:00 +07:00
|
|
|
logOutput := log.WrapOsFile(os.Stderr)
|
|
|
|
prettyHandler := log.NewPrettyHandler(logOutput, nil)
|
|
|
|
slog.SetDefault(slog.New(prettyHandler))
|
|
|
|
|
2024-08-06 22:13:37 +07:00
|
|
|
api := &api.API{
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := &server.Server{
|
|
|
|
DeviceHandler: server.DeviceHandler{
|
|
|
|
API: api,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle(v1connect.NewDeviceServiceHandler(handler, connect.WithInterceptors(server.LogInterceptor())))
|
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: h2c.NewHandler(server.WithCORS(mux), &http2.Server{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-cmd.Context().Done()
|
|
|
|
slog.Info("Exit signal received. Shutting down server")
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
|
|
|
_ = server.Shutdown(shutdownCtx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
slog.Info("ConnectRPC server started", "addr", server.Addr)
|
|
|
|
err = server.ListenAndServe()
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
return errs.Wrap(err, "failed to serve")
|
|
|
|
}
|
|
|
|
return errors.Join(db.Close())
|
|
|
|
},
|
|
|
|
SilenceUsage: true,
|
|
|
|
}
|