diff --git a/api/api.go b/api/api.go index a234ec0a..c12117dd 100644 --- a/api/api.go +++ b/api/api.go @@ -15,6 +15,7 @@ import ( "github.com/Gleipnir-Technology/nidus-sync/platform" "github.com/Gleipnir-Technology/nidus-sync/platform/types" "github.com/Gleipnir-Technology/nidus-sync/resource" + "github.com/Gleipnir-Technology/nidus-sync/version" //"github.com/gorilla/mux" "github.com/rs/zerolog/log" ) @@ -273,10 +274,10 @@ func parseTime(x string) (*time.Time, error) { } type about struct { - Environment string `json:"environment"` - SentryDSN string `json:"sentry_dsn"` - Tegola tegolaURLs `json:"tegola"` - Version string `json:"version"` + Environment string `json:"environment"` + SentryDSN string `json:"sentry_dsn"` + Tegola tegolaURLs `json:"tegola"` + Version version.VersionInfo `json:"version"` } type tegolaURLs struct { Nidus string `json:"nidus"` @@ -284,6 +285,7 @@ type tegolaURLs struct { } func getRoot(ctx context.Context, r *http.Request, q resource.QueryParams) (*about, *nhttp.ErrorWithStatus) { + v := version.Get() return &about{ Environment: config.Environment, SentryDSN: config.SentryDSNFrontend, @@ -291,6 +293,6 @@ func getRoot(ctx context.Context, r *http.Request, q resource.QueryParams) (*abo Nidus: config.MakeURLTegola("/maps/nidus/{z}/{x}/{y}?id={organization_id}"), RMO: config.MakeURLTegola("/maps/rmo/{z}/{x}/{y}"), }, - Version: version, + Version: v, }, nil } diff --git a/api/event.go b/api/event.go index 374dd5f6..e17e2a4f 100644 --- a/api/event.go +++ b/api/event.go @@ -7,6 +7,7 @@ import ( "time" "github.com/Gleipnir-Technology/nidus-sync/platform" + "github.com/Gleipnir-Technology/nidus-sync/version" "github.com/google/uuid" "github.com/rs/zerolog/log" ) @@ -27,6 +28,13 @@ type Message struct { URI string `json:"uri"` } +type Status struct { + BuildTime time.Time `json:"build_time"` + IsModified bool `json:"is_modified"` + Revision string `json:"revision"` + Status string `json:"status"` +} + func (c *ConnectionSSE) SendEvent(w http.ResponseWriter, m platform.Event) error { return send(w, Message{ Resource: m.Resource, @@ -62,11 +70,6 @@ func SetEventChannel(chan_envelopes <-chan platform.Envelope) { }() } -var version string = "unknown" - -func SetVersion(v string) { - version = v -} func send[T any](w http.ResponseWriter, msg T) error { jsonData, err := json.Marshal(msg) if err != nil { @@ -91,6 +94,8 @@ func streamEvents(w http.ResponseWriter, r *http.Request, u platform.User) { uid, err := uuid.NewUUID() if err != nil { log.Error().Err(err).Msg("failed to create uuid") + http.Error(w, "failed to create uuid", http.StatusInternalServerError) + return } connection := ConnectionSSE{ chanEvent: make(chan platform.Event), @@ -102,7 +107,21 @@ func streamEvents(w http.ResponseWriter, r *http.Request, u platform.User) { log.Debug().Int32("org", u.Organization.ID).Int("user", u.ID).Str("id", uid.String()).Msg("connected SSE client") // Send an initial connected event - fmt.Fprintf(w, "event: connected\ndata: {\"status\": \"connected\", \"version\": \"%s\", \"time\": \"%s\"}\n\n", version, time.Now().Format(time.RFC3339)) + v := version.Get() + status := Status{ + BuildTime: v.BuildTime, + IsModified: v.IsModified, + Revision: v.Revision, + Status: "connected", + } + body, err := json.Marshal(status) + if err != nil { + log.Error().Err(err).Msg("failed to marshal connect status") + http.Error(w, "failed to marshal connect status", http.StatusInternalServerError) + return + } + + w.Write(body) w.(http.Flusher).Flush() // Keep the connection open with a ticker sending periodic events diff --git a/main.go b/main.go index 5b290686..fb84a993 100644 --- a/main.go +++ b/main.go @@ -19,9 +19,9 @@ import ( "github.com/Gleipnir-Technology/nidus-sync/llm" "github.com/Gleipnir-Technology/nidus-sync/middleware" "github.com/Gleipnir-Technology/nidus-sync/platform" - "github.com/Gleipnir-Technology/nidus-sync/resource" "github.com/Gleipnir-Technology/nidus-sync/rmo" nidussync "github.com/Gleipnir-Technology/nidus-sync/sync" + "github.com/Gleipnir-Technology/nidus-sync/version" "github.com/getsentry/sentry-go" sentryhttp "github.com/getsentry/sentry-go/http" "github.com/getsentry/sentry-go/zerolog" @@ -43,7 +43,8 @@ func main() { log.Warn().Msg("Forcing production mode for testing templates") config.Environment = "PRODUCTION" } - log.Info().Str("environment", config.Environment).Bool("is-prod", config.IsProductionEnvironment()).Str("version", Version).Str("commit", Commit).Msg("Starting") + v := version.Get() + log.Info().Str("environment", config.Environment).Bool("is-prod", config.IsProductionEnvironment()).Str("revision", v.Revision).Str("build_time", v.BuildTime.String()).Bool("is modified", v.IsModified).Msg("Starting") err = sentry.Init(sentry.ClientOptions{ Debug: false, //!config.IsProductionEnvironment(), Dsn: config.SentryDSN, @@ -157,8 +158,6 @@ func main() { }() chan_envelope := make(chan platform.Envelope, 10) - api.SetVersion(Version) - resource.SetVersion(Version) platform.SetEventChannel(chan_envelope) api.SetEventChannel(chan_envelope) diff --git a/resource/session.go b/resource/session.go index 0ca987ae..61400318 100644 --- a/resource/session.go +++ b/resource/session.go @@ -116,9 +116,3 @@ func (res *sessionR) Get(ctx context.Context, r *http.Request, user platform.Use }, }, nil } - -var version string = "unknown" - -func SetVersion(v string) { - version = v -} diff --git a/version.go b/version/version.go similarity index 100% rename from version.go rename to version/version.go