chibisafe_netstorage_middleman : properly handle error output

This commit is contained in:
minoplhy 2024-04-10 21:58:07 +07:00
parent 5bb04ab856
commit 8aa71c95db
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
6 changed files with 96 additions and 18 deletions

View File

@ -9,3 +9,4 @@ services:
- .:/app
environment:
- CHIBISAFE_BASEPATH=
- MAX_UPLOAD_SIZE=

59
main.go

File diff suppressed because it is too large Load Diff

View File

@ -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

View File

@ -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)

View File

@ -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
View 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)
}