Skip to content

Commit

Permalink
perfect docker to http server
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestjgq committed Dec 1, 2020
1 parent f498f22 commit 9e0920b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
42 changes: 40 additions & 2 deletions gmi/gmi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package gmi defines Marker to represent each variable by interface.
package gmi

import "net/textproto"

// Marker is an interface to provide variable marking.
type Marker interface {
// Mark a number, the number definition is bound to marker itself.
Expand All @@ -18,8 +20,8 @@ const (

type Request struct {
Router Route
Headers map[string]string
Params map[string]string
headers map[string]string
}

func (r *Request) GetParam(key string) string {
Expand All @@ -38,9 +40,45 @@ func (r *Request) HasParam(key string) bool {
_, ok := r.Params[key]
return ok
}
func (r *Request) GetHeader(key string) string {
if r.headers == nil {
return ""
}
if v, ok := r.headers[textproto.CanonicalMIMEHeaderKey(key)]; ok {
return v
}
return ""
}
func (r *Request) SetHeader(key, value string) {
if r.headers == nil {
r.headers = make(map[string]string)
}
r.headers[textproto.CanonicalMIMEHeaderKey(key)] = value
}

type Response struct {
Status int
Headers map[string]string
headers map[string]string
Body []byte
}
func (r *Response) SetHeader(key, value string) {
if r.headers == nil {
r.headers = make(map[string]string)
}
r.headers[textproto.CanonicalMIMEHeaderKey(key)] = value
}
func (r *Response) GetHeaders() map[string]string {
if r.headers == nil {
r.headers = make(map[string]string)
}
return r.headers
}
func (r *Response) GetHeader(key string) string {
if r.headers == nil {
return ""
}
if v, ok := r.headers[textproto.CanonicalMIMEHeaderKey(key)]; ok {
return v
}
return ""
}
54 changes: 18 additions & 36 deletions internal/httpsrv/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ type httpServer struct {
}

var server *httpServer
var headers = []string{
"If-Modified-Since",
"console",
"user-agent",
}

func Start(port int) {
var err error
Expand Down Expand Up @@ -62,8 +57,6 @@ func RequestHTTP(req *gmi.Request) *gmi.Response {
default:
return &gmi.Response{
Status: 404,
Headers: nil,
Body: nil,
}
}
}
Expand All @@ -74,8 +67,6 @@ func procDebug(w http.ResponseWriter, r *http.Request) {
func serveDebug(req *gmi.Request) (rsp *gmi.Response) {
rsp = &gmi.Response{
Status: 200,
Headers: make(map[string]string),
Body: nil,
}
p := req.GetParam("perf")
if p == "1" {
Expand All @@ -99,18 +90,17 @@ func procJs(w http.ResponseWriter, r *http.Request) {
func serveJs(req *gmi.Request) (rsp *gmi.Response) {
rsp = &gmi.Response{
Status: 200,
Headers: map[string]string{
"content-type": "application/javascript",
},
Body: nil,
}

rsp.SetHeader("content-type", "application/javascript")

if v, ok := req.Params["script"]; ok {
if m, exist := req.Headers["If-Modified-Since"]; exist && m == lastModified {
if req.GetHeader("If-Modified-Since") == lastModified {
rsp.Status = 304
return
}
rsp.Headers["Last-Modified"] = lastModified
rsp.SetHeader("Last-Modified", lastModified)
if v == "jquery_min" {
rsp.Body = []byte(jqueryMinJs)
} else if v == "flot_min" {
Expand All @@ -124,16 +114,14 @@ func serveJs(req *gmi.Request) (rsp *gmi.Response) {
return
}

func useHtml(h map[string]string) bool {
if h == nil {
return true
}

if v, ok := h["console"]; ok && len(v) > 0 {
func useHtml(req *gmi.Request) bool {
v := req.GetHeader("console")
if len(v) > 0 {
return v == "0"
}

if v, ok := h["user-agent"]; !ok || len(v) == 0 {
v = req.GetHeader("user-agent")
if len(v) == 0 {
return false
} else if strings.Index(v, "curl/") < 0 {
return true
Expand Down Expand Up @@ -194,7 +182,6 @@ func procVar(w http.ResponseWriter, r *http.Request) {
func proc(route gmi.Route, w http.ResponseWriter, r *http.Request) {
req := &gmi.Request{
Params: make(map[string]string),
Headers: make(map[string]string),
}
vars := mux.Vars(r)
for k, v := range vars {
Expand All @@ -213,11 +200,7 @@ func proc(route gmi.Route, w http.ResponseWriter, r *http.Request) {
}

for k := range r.Header {
req.Headers[k] = r.Header.Get(k)
}

for _, k := range headers {
req.Headers[k] = r.Header.Get(k)
req.SetHeader(k, r.Header.Get(k))
}

var rsp *gmi.Response
Expand All @@ -228,12 +211,13 @@ func proc(route gmi.Route, w http.ResponseWriter, r *http.Request) {
rsp = serveDebug(req)
case gmi.RouteJs:
rsp = serveJs(req)
default:
w.WriteHeader(404)
return
}
w.WriteHeader(rsp.Status)
if len(rsp.Headers) > 0 {
for k, v := range rsp.Headers {
w.Header().Add(k, v)
}
for k, v := range rsp.GetHeaders() {
w.Header().Add(k, v)
}
if len(rsp.Body) > 0 {
_, _ = w.Write(rsp.Body)
Expand All @@ -242,8 +226,6 @@ func proc(route gmi.Route, w http.ResponseWriter, r *http.Request) {
func serveVar(req *gmi.Request) (rsp *gmi.Response) {
rsp = &gmi.Response{
Status: 200,
Headers: make(map[string]string),
Body: nil,
}

buf := &bytes.Buffer{}
Expand All @@ -266,15 +248,15 @@ func serveVar(req *gmi.Request) (rsp *gmi.Response) {
return
}

html := useHtml(req.Headers)
html := useHtml(req)
tabs := false
if html && !req.HasParam("dataonly") {
tabs = true
}
if html {
rsp.Headers["content-type"] = "text/html"
rsp.SetHeader("content-type", "text/html")
} else {
rsp.Headers["content-type"] = "text/plain"
rsp.SetHeader("content-type", "text/plain")
}

dumper := &dumpImpl{html: html}
Expand Down
2 changes: 2 additions & 0 deletions mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func StartHTTPServer(port int) {
}

// Request will docker gomark to your own http server
// See http_server.go for detailed information, there Start() will start an http
// server based on mux, and it will call the same API as this calls.
func Request(req *gmi.Request) *gmi.Response {
return httpsrv.RequestHTTP(req)
}
Expand Down

0 comments on commit 9e0920b

Please sign in to comment.