Skip to content

Commit 4b78e84

Browse files
committed
Adding a way to get hostname from request
1 parent bbaeca8 commit 4b78e84

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

pkg/gofr/cmd/request.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"os"
56
"reflect"
67
"strconv"
78
"strings"
@@ -74,6 +75,13 @@ func (r *Request) PathParam(key string) string {
7475
func (r *Request) Context() context.Context {
7576
return context.Background()
7677
}
78+
func (r *Request) HostName() string {
79+
h, err := os.Hostname()
80+
if err != nil {
81+
return ""
82+
}
83+
return h
84+
}
7785

7886
func (r *Request) Bind(i interface{}) error {
7987
// pointer to struct - addressable

pkg/gofr/http/request.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io/ioutil"
89
"net/http"
910

@@ -45,6 +46,14 @@ func (r *Request) Bind(i interface{}) error {
4546
return json.Unmarshal(body, &i)
4647
}
4748

49+
func (r *Request) HostName() string {
50+
proto := r.req.Header.Get("X-forwarded-proto")
51+
if proto == "" {
52+
proto = "http"
53+
}
54+
return fmt.Sprintf("%s://%s", proto, r.req.Host)
55+
}
56+
4857
func (r *Request) body() ([]byte, error) {
4958
bodyBytes, err := ioutil.ReadAll(r.req.Body)
5059
if err != nil {

pkg/gofr/request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ type Request interface {
1212
Param(string) string
1313
PathParam(string) string
1414
Bind(interface{}) error
15+
HostName() string
1516
}

0 commit comments

Comments
 (0)