chibisafe_netstorage_middleman : properly handle error output
This commit is contained in:
parent
5bb04ab856
commit
8aa71c95db
@ -9,3 +9,4 @@ services:
|
||||
- .:/app
|
||||
environment:
|
||||
- CHIBISAFE_BASEPATH=
|
||||
- MAX_UPLOAD_SIZE=
|
||||
|
@ -8,6 +8,25 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func Check_API_Key(Basepath string, accessKey string) bool {
|
||||
URL := Basepath + "/api/user/me"
|
||||
headers := map[string]string{
|
||||
"X-Api-Key": accessKey,
|
||||
}
|
||||
GETStruct := URLRequest{
|
||||
URL: URL,
|
||||
Header: headers,
|
||||
Method: "GET",
|
||||
}
|
||||
resp, err := HTTPNoData(GETStruct)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return resp.StatusCode == http.StatusOK
|
||||
}
|
||||
|
||||
func UploadPost(BasePath string, PostData UploadPostMeta, accessKey string) ([]byte, error) {
|
||||
URL := BasePath + "/api/upload"
|
||||
// Convert PostData to JSON
|
||||
|
@ -51,6 +51,24 @@ func HTTPOSFile(RequestStruct URLRequest, RequestData *os.File) (*http.Response,
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// HTTP With no Data sent
|
||||
func HTTPNoData(RequestStruct URLRequest) (*http.Response, error) {
|
||||
req, err := http.NewRequest(RequestStruct.Method, RequestStruct.URL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for key, value := range RequestStruct.Header {
|
||||
req.Header.Set(key, value)
|
||||
}
|
||||
|
||||
resp, err := HTTPClientDo(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func HTTPClientDo(Request *http.Request) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
response, err := client.Do(Request)
|
||||
|
@ -31,3 +31,8 @@ type URLRequest struct {
|
||||
ContentLength *int
|
||||
Header map[string]string
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
StatusCode int64
|
||||
Message string
|
||||
}
|
||||
|
12
src/handler/templates.go
Normal file
12
src/handler/templates.go
Normal file
@ -0,0 +1,12 @@
|
||||
package handler
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func ErrorResponseBuild(StatusCode int64, Message string) string {
|
||||
ErrorResponse := ErrorResponse{
|
||||
StatusCode: StatusCode,
|
||||
Message: Message,
|
||||
}
|
||||
Response, _ := json.Marshal(ErrorResponse)
|
||||
return string(Response)
|
||||
}
|
Loading…
Reference in New Issue
Block a user