@@ -64,18 +64,21 @@ func (sm *ServerManager) requireServerStats(h func(http.ResponseWriter, *http.Re
6464 name := r .PathValue ("server" )
6565 server , ok := sm .serverByName [name ]
6666 if ! ok {
67- return restapi .EncodeResponse (w , http .StatusNotFound , StandardError { Message : "server not found" } )
67+ return restapi .EncodeResponse (w , http .StatusNotFound , & serverNotFoundJSON )
6868 }
6969 return h (w , r , server .StatsCollector )
7070 }
7171}
7272
73- var serverInfoJSON = []byte (`{"server":"shadowsocks-go ` + shadowsocks .Version + `","apiVersion":"v1"}` )
73+ var (
74+ serverInfoJSON = []byte (`{"server":"shadowsocks-go ` + shadowsocks .Version + `","apiVersion":"v1"}` )
75+ serverNotFoundJSON = []byte (`{"error":"server not found"}` )
76+ serverNoCredentialManagerJSON = []byte (`{"error":"The server does not support user management."}` )
77+ userNotFoundJSON = []byte (`{"error":"user not found"}` )
78+ )
7479
7580func handleGetServerInfo (w http.ResponseWriter , _ * http.Request , _ stats.Collector ) (int , error ) {
76- w .Header ()["Content-Type" ] = []string {"application/json" }
77- _ , err := w .Write (serverInfoJSON )
78- return http .StatusOK , err
81+ return restapi .EncodeResponse (w , http .StatusOK , & serverInfoJSON )
7982}
8083
8184func handleGetStats (w http.ResponseWriter , r * http.Request , sc stats.Collector ) (int , error ) {
@@ -93,10 +96,10 @@ func (sm *ServerManager) requireServerUsers(h func(http.ResponseWriter, *http.Re
9396 name := r .PathValue ("server" )
9497 server , ok := sm .serverByName [name ]
9598 if ! ok {
96- return restapi .EncodeResponse (w , http .StatusNotFound , StandardError { Message : "server not found" } )
99+ return restapi .EncodeResponse (w , http .StatusNotFound , & serverNotFoundJSON )
97100 }
98101 if server .CredentialManager == nil {
99- return restapi .EncodeResponse (w , http .StatusNotFound , StandardError { Message : "The server does not support user management." } )
102+ return restapi .EncodeResponse (w , http .StatusNotFound , & serverNoCredentialManagerJSON )
100103 }
101104 return h (w , r , server )
102105 }
@@ -131,7 +134,7 @@ func handleGetUser(w http.ResponseWriter, r *http.Request, ms Server) (int, erro
131134 username := r .PathValue ("username" )
132135 userCred , ok := ms .CredentialManager .GetCredential (username )
133136 if ! ok {
134- return restapi .EncodeResponse (w , http .StatusNotFound , StandardError { Message : "user not found" } )
137+ return restapi .EncodeResponse (w , http .StatusNotFound , & userNotFoundJSON )
135138 }
136139
137140 return restapi .EncodeResponse (w , http .StatusOK , response {userCred , ms .StatsCollector .Snapshot ().Traffic })
0 commit comments