@@ -85,6 +85,11 @@ func (h APIHandler) registerInstance(w http.ResponseWriter, req *http.Request, p
85
85
err := h .DecodeJSON (req , obj )
86
86
if err != nil {
87
87
h .WriteError (w , err .Error (), http .StatusInternalServerError )
88
+ return
89
+ }
90
+ if obj .Endpoint == "" {
91
+ h .WriteError (w , "empty endpoint" , http .StatusInternalServerError )
92
+ return
88
93
}
89
94
90
95
oldInst := & model.Instance {}
@@ -95,8 +100,28 @@ func (h APIHandler) registerInstance(w http.ResponseWriter, req *http.Request, p
95
100
h .WriteError (w , errMsg , http .StatusInternalServerError )
96
101
return
97
102
}
98
-
99
- err = orm .Create (nil , obj )
103
+ err , result := orm .GetBy ("endpoint" , obj .Endpoint , oldInst )
104
+ if err != nil {
105
+ log .Error (err )
106
+ h .WriteError (w , err .Error (), http .StatusInternalServerError )
107
+ return
108
+ }
109
+ if len (result .Result ) > 0 {
110
+ buf := util .MustToJSONBytes (result .Result [0 ])
111
+ util .MustFromJSONBytes (buf , & oldInst )
112
+ if oldInst .ID != "" {
113
+ //keep old created time
114
+ obj .Created = oldInst .Created
115
+ log .Infof ("remove old instance [%s] with the same endpoint %s" , oldInst .ID , oldInst .Endpoint )
116
+ err = orm .Delete (nil , oldInst )
117
+ if err != nil {
118
+ log .Error (err )
119
+ h .WriteError (w , err .Error (), http .StatusInternalServerError )
120
+ return
121
+ }
122
+ }
123
+ }
124
+ err = orm .Save (nil , obj )
100
125
if err != nil {
101
126
h .WriteError (w , err .Error (), http .StatusInternalServerError )
102
127
return
0 commit comments