add: X-Real-IP header

This commit is contained in:
minoplhy 2024-04-11 18:38:54 +07:00
parent 449b455d24
commit 897e0497d8
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
3 changed files with 17 additions and 15 deletions

View File

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
FROM golang:latest FROM golang:1.22
# Set destination for COPY # Set destination for COPY
WORKDIR /app WORKDIR /app

16
main.go
View File

@ -85,7 +85,13 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
FileSize: fileHeader.Size, FileSize: fileHeader.Size,
} }
chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, API_Key, PostData) UploadHeaders := map[string]string{
"X-Api-Key": API_Key,
"Content-Type": "application/json",
"X-Real-IP": r.RemoteAddr,
}
chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, UploadHeaders, PostData)
if err != nil { if err != nil {
http.Error(w, handler.ErrorResponseBuild(http.StatusBadRequest, "Something went wrong!"), http.StatusBadRequest) http.Error(w, handler.ErrorResponseBuild(http.StatusBadRequest, "Something went wrong!"), http.StatusBadRequest)
handler.ErrorLogBuilder([]string{r.RemoteAddr, tempfilepath}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr, tempfilepath}, err.Error())
@ -120,7 +126,13 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
Identifier: chibisafe_Response_Metadata.Identifier, Identifier: chibisafe_Response_Metadata.Identifier,
} }
PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, API_Key, PostProcessData) ProcessHeaders := map[string]string{
"X-Api-Key": API_Key,
"Content-Type": "application/json",
"X-Real-IP": r.RemoteAddr,
}
PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, ProcessHeaders, PostProcessData)
if err != nil { if err != nil {
http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError)
handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error()) handler.ErrorLogBuilder([]string{r.RemoteAddr, chibisafe_Response_Metadata.Identifier, tempfilepath}, err.Error())

View File

@ -31,7 +31,7 @@ func Check_API_Key(Basepath string, accessKey string) bool {
return resp.StatusCode == http.StatusOK return resp.StatusCode == http.StatusOK
} }
func UploadPost(BasePath string, accessKey string, PostData UploadPostMeta) ([]byte, error) { func UploadPost(BasePath string, headers map[string]string, PostData UploadPostMeta) ([]byte, error) {
URL := BasePath + "/api/upload" URL := BasePath + "/api/upload"
// Convert PostData to JSON // Convert PostData to JSON
PostDataJson, err := json.Marshal(PostData) PostDataJson, err := json.Marshal(PostData)
@ -39,11 +39,6 @@ func UploadPost(BasePath string, accessKey string, PostData UploadPostMeta) ([]b
return nil, err return nil, err
} }
headers := map[string]string{
"X-Api-Key": accessKey,
"Content-Type": "application/json",
}
POSTStruct := URLRequest{ POSTStruct := URLRequest{
URL: URL, URL: URL,
ContentType: PostData.ContentType, ContentType: PostData.ContentType,
@ -114,7 +109,7 @@ func NetworkStoragePut(URL string, ContentType string, filepath string) ([]byte,
} }
} }
func UploadProcessPost(BasePath string, accessKey string, PostData UploadProcessMeta) ([]byte, error) { func UploadProcessPost(BasePath string, headers map[string]string, PostData UploadProcessMeta) ([]byte, error) {
URL := BasePath + "/api/upload/process" URL := BasePath + "/api/upload/process"
// Convert PostData to JSON // Convert PostData to JSON
PostDataJson, err := json.Marshal(PostData) PostDataJson, err := json.Marshal(PostData)
@ -122,11 +117,6 @@ func UploadProcessPost(BasePath string, accessKey string, PostData UploadProcess
return nil, err return nil, err
} }
headers := map[string]string{
"X-Api-Key": accessKey,
"Content-Type": "application/json",
}
POSTStruct := URLRequest{ POSTStruct := URLRequest{
URL: URL, URL: URL,
Method: "POST", Method: "POST",