Bluemage/go/cmd/bluemage/serve/serve.go

79 lines
1.9 KiB
Go
Raw Normal View History

package serve
import (
"context"
2024-08-08 13:41:18 +07:00
"database/sql"
"errors"
"log/slog"
"net/http"
2024-08-07 10:41:00 +07:00
"os"
"time"
"connectrpc.com/connect"
2024-08-08 13:41:18 +07:00
sqldblogger "github.com/simukti/sqldb-logger"
"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"
"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-08 13:41:18 +07:00
sqldb, err := sql.Open("sqlite3", "file:data.db")
if err != nil {
2024-08-08 13:41:18 +07:00
return errs.Wrapw(err, "failed to open database", "file", "data.db")
}
2024-08-08 13:41:18 +07:00
sqldb = sqldblogger.OpenDriver(
"file:data.db",
sqldb.Driver(),
log.SQLLogger{},
sqldblogger.WithSQLQueryAsMessage(true),
)
db := bob.New(sqldb)
2024-08-07 10:41:00 +07:00
logOutput := log.WrapOsFile(os.Stderr)
prettyHandler := log.NewPrettyHandler(logOutput, nil)
slog.SetDefault(slog.New(prettyHandler))
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")
}
2024-08-08 13:41:18 +07:00
return errors.Join(sqldb.Close())
},
SilenceUsage: true,
}