-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
171 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2020-present Kuei-chun Chen. All rights reserved. | ||
|
||
package keyhole | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"mime/multipart" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// GenerateMaobiReport outputs an HTML from Maobi | ||
func GenerateMaobiReport(maobiURL string, data []byte, ofile string) error { | ||
var err error | ||
var murl *url.URL | ||
if maobiURL == "" { | ||
return nil | ||
} | ||
if murl, err = url.Parse(maobiURL); err != nil { | ||
return err | ||
} | ||
os.Mkdir(htmldir, 0755) | ||
i := strings.Index(ofile, ".bson.gz") | ||
filename := strings.Replace(ofile[:i]+".html", outdir, htmldir, 1) | ||
dial := net.Dialer{Timeout: 2 * time.Second} | ||
var c net.Conn | ||
to := murl.Hostname() | ||
if murl.Port() != "" { | ||
to += ":" + murl.Port() | ||
} | ||
if c, err = dial.Dial("tcp", to); err != nil { | ||
return err | ||
} | ||
c.Close() | ||
bodyBuf := &bytes.Buffer{} | ||
bodyWriter := multipart.NewWriter(bodyBuf) | ||
fileWriter, err := bodyWriter.CreateFormFile("file", ofile) | ||
fileWriter.Write(data) | ||
contentType := bodyWriter.FormDataContentType() | ||
bodyWriter.Close() | ||
resp, err := http.Post(maobiURL, contentType, bodyBuf) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
ioutil.WriteFile(filename, body, 0644) | ||
fmt.Println(fmt.Sprintf(`HTML report written to %v`, filename)) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.