Compare commits
7 Commits
v1.4.1
...
b475da66da
Author | SHA1 | Date | |
---|---|---|---|
b475da66da | |||
d0085ab2c3 | |||
58514f4c5f | |||
2a32a1b3ce | |||
6da7d408f9 | |||
e993bcf317 | |||
9b231a73d6 |
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
@ -15,6 +15,7 @@ fine with getting your hands dirty, but I plan on having it ready to go for more
|
||||
- CSRF protection
|
||||
- Minimal user login/registration + sessions
|
||||
- Config file handling
|
||||
- Scheduled tasks
|
||||
- Entire website compiles into a single binary (~10mb) (excluding env.json)
|
||||
- Minimal dependencies (just standard library, postgres driver, and experimental package for bcrypt)
|
||||
|
||||
|
4
go.mod
4
go.mod
@ -3,6 +3,6 @@ module GoWeb
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/lib/pq v1.10.7
|
||||
golang.org/x/crypto v0.7.0
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/crypto v0.11.0
|
||||
)
|
||||
|
8
go.sum
8
go.sum
@ -1,4 +1,4 @@
|
||||
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
||||
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
||||
|
57
restclient/client.go
Normal file
57
restclient/client.go
Normal file
@ -0,0 +1,57 @@
|
||||
package restclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SendRequest sends an HTTP request to a URL and includes the specified headers and body.
|
||||
// A body can be nil for GET requests, a map[string]string for multipart/form-data requests,
|
||||
// or a struct for JSON requests
|
||||
func SendRequest(url string, method string, headers map[string]string, body interface{}) (http.Response, error) {
|
||||
var reqBody *bytes.Buffer
|
||||
var contentType string
|
||||
|
||||
switch v := body.(type) {
|
||||
case nil:
|
||||
reqBody = bytes.NewBuffer([]byte(""))
|
||||
case map[string]string:
|
||||
reqBody = &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(reqBody)
|
||||
for key, value := range v {
|
||||
writer.WriteField(key, value)
|
||||
}
|
||||
writer.Close()
|
||||
contentType = writer.FormDataContentType()
|
||||
default:
|
||||
jsonBody, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
reqBody = bytes.NewBuffer(jsonBody)
|
||||
contentType = "application/json"
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, url, reqBody)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
for key, value := range headers {
|
||||
req.Header.Add(key, value)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return http.Response{}, err
|
||||
}
|
||||
|
||||
return *resp, nil
|
||||
}
|
Reference in New Issue
Block a user