Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
7140d1c1
Commit
7140d1c1
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Split middleware and helpers into separate files
parent
8b2a17e6
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
helpers.go
+64
-0
64 additions, 0 deletions
helpers.go
main.go
+0
-79
0 additions, 79 deletions
main.go
middleware.go
+34
-0
34 additions, 0 deletions
middleware.go
with
98 additions
and
79 deletions
helpers.go
0 → 100644
+
64
−
0
View file @
7140d1c1
package
main
import
(
"encoding/json"
"log"
"net/http"
"net/url"
)
func
getOrigin
(
r
*
http
.
Request
)
string
{
origin
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
)
if
origin
==
""
{
origin
=
r
.
RemoteAddr
}
return
origin
}
func
getURL
(
r
*
http
.
Request
)
string
{
scheme
:=
r
.
Header
.
Get
(
"X-Forwarded-Proto"
)
if
scheme
==
""
{
scheme
=
r
.
Header
.
Get
(
"X-Forwarded-Protocol"
)
}
if
scheme
==
""
&&
r
.
Header
.
Get
(
"X-Forwarded-Ssl"
)
==
"on"
{
scheme
=
"https"
}
if
scheme
==
""
{
scheme
=
"http"
}
host
:=
r
.
URL
.
Host
if
host
==
""
{
host
=
r
.
Host
}
u
:=
&
url
.
URL
{
Scheme
:
scheme
,
Opaque
:
r
.
URL
.
Opaque
,
User
:
r
.
URL
.
User
,
Host
:
host
,
Path
:
r
.
URL
.
Path
,
RawPath
:
r
.
URL
.
RawPath
,
ForceQuery
:
r
.
URL
.
ForceQuery
,
RawQuery
:
r
.
URL
.
RawQuery
,
Fragment
:
r
.
URL
.
Fragment
,
}
return
u
.
String
()
}
func
writeResponse
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
Resp
)
{
resp
.
Origin
=
getOrigin
(
r
)
resp
.
URL
=
getURL
(
r
)
body
,
err
:=
json
.
Marshal
(
resp
)
if
err
!=
nil
{
log
.
Printf
(
"error marshalling %v as JSON: %s"
,
resp
,
err
)
}
writeJSON
(
w
,
body
)
}
func
writeJSON
(
w
http
.
ResponseWriter
,
body
[]
byte
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; encoding=utf-8"
)
w
.
Write
(
body
)
}
This diff is collapsed.
Click to expand it.
main.go
+
0
−
79
View file @
7140d1c1
package
main
package
main
import
(
import
(
"encoding/json"
"fmt"
"fmt"
"html/template"
"html/template"
"log"
"log"
...
@@ -22,84 +21,6 @@ type Resp struct {
...
@@ -22,84 +21,6 @@ type Resp struct {
JSON
map
[
string
][]
string
`json:"json,omitempty"`
JSON
map
[
string
][]
string
`json:"json,omitempty"`
}
}
func
getOrigin
(
r
*
http
.
Request
)
string
{
origin
:=
r
.
Header
.
Get
(
"X-Forwarded-For"
)
if
origin
==
""
{
origin
=
r
.
RemoteAddr
}
return
origin
}
func
getURL
(
r
*
http
.
Request
)
string
{
scheme
:=
r
.
Header
.
Get
(
"X-Forwarded-Proto"
)
if
scheme
==
""
{
scheme
=
r
.
Header
.
Get
(
"X-Forwarded-Protocol"
)
}
if
scheme
==
""
&&
r
.
Header
.
Get
(
"X-Forwarded-Ssl"
)
==
"on"
{
scheme
=
"https"
}
if
scheme
==
""
{
scheme
=
"http"
}
host
:=
r
.
URL
.
Host
if
host
==
""
{
host
=
r
.
Host
}
u
:=
&
url
.
URL
{
Scheme
:
scheme
,
Opaque
:
r
.
URL
.
Opaque
,
User
:
r
.
URL
.
User
,
Host
:
host
,
Path
:
r
.
URL
.
Path
,
RawPath
:
r
.
URL
.
RawPath
,
ForceQuery
:
r
.
URL
.
ForceQuery
,
RawQuery
:
r
.
URL
.
RawQuery
,
Fragment
:
r
.
URL
.
Fragment
,
}
return
u
.
String
()
}
func
writeResponse
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
resp
*
Resp
)
{
resp
.
Origin
=
getOrigin
(
r
)
resp
.
URL
=
getURL
(
r
)
body
,
err
:=
json
.
Marshal
(
resp
)
if
err
!=
nil
{
log
.
Printf
(
"error marshalling %v as JSON: %s"
,
resp
,
err
)
}
w
.
Write
(
body
)
}
func
logger
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
h
.
ServeHTTP
(
w
,
r
)
log
.
Printf
(
"%s %s %s"
,
r
.
RemoteAddr
,
r
.
Method
,
r
.
URL
)
})
}
func
cors
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
origin
:=
r
.
Header
.
Get
(
"Origin"
)
if
origin
==
""
{
origin
=
"*"
}
respHeader
:=
w
.
Header
()
respHeader
.
Set
(
"Access-Control-Allow-Origin"
,
origin
)
respHeader
.
Set
(
"Access-Control-Allow-Credentials"
,
"true"
)
if
r
.
Method
==
"OPTIONS"
{
respHeader
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
)
respHeader
.
Set
(
"Access-Control-Max-Age"
,
"3600"
)
if
r
.
Header
.
Get
(
"Access-Control-Request-Headers"
)
!=
""
{
respHeader
.
Set
(
"Access-Control-Allow-Headers"
,
r
.
Header
.
Get
(
"Access-Control-Request-Headers"
))
}
}
h
.
ServeHTTP
(
w
,
r
)
})
}
func
index
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
index
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
t
,
err
:=
template
.
ParseGlob
(
"templates/*.html"
)
t
,
err
:=
template
.
ParseGlob
(
"templates/*.html"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
middleware.go
0 → 100644
+
34
−
0
View file @
7140d1c1
package
main
import
(
"log"
"net/http"
)
func
logger
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
h
.
ServeHTTP
(
w
,
r
)
log
.
Printf
(
"%s %s %s"
,
r
.
RemoteAddr
,
r
.
Method
,
r
.
URL
)
})
}
func
cors
(
h
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
origin
:=
r
.
Header
.
Get
(
"Origin"
)
if
origin
==
""
{
origin
=
"*"
}
respHeader
:=
w
.
Header
()
respHeader
.
Set
(
"Access-Control-Allow-Origin"
,
origin
)
respHeader
.
Set
(
"Access-Control-Allow-Credentials"
,
"true"
)
if
r
.
Method
==
"OPTIONS"
{
respHeader
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
)
respHeader
.
Set
(
"Access-Control-Max-Age"
,
"3600"
)
if
r
.
Header
.
Get
(
"Access-Control-Request-Headers"
)
!=
""
{
respHeader
.
Set
(
"Access-Control-Allow-Headers"
,
r
.
Header
.
Get
(
"Access-Control-Request-Headers"
))
}
}
h
.
ServeHTTP
(
w
,
r
)
})
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment