-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (40 loc) · 1.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io"
"log"
"net/http"
"time"
)
func main(){
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/tl-hasher/sha256", processRequest).Methods("POST")
log.Println("Listening on port 8080")
log.Println("Application Started")
log.Fatal(http.ListenAndServe(":8080", myRouter))
}
type Response struct {
Hash string `json:"hash"`
TimeTakenMS int64 `json:"time_taken_ms"`
}
func processRequest(writer http.ResponseWriter, request *http.Request) {
startTime:=time.Now().UnixNano() / int64(time.Millisecond)
defer request.Body.Close()
hash:=sha256.New()
file,_,_:=request.FormFile("file")
if _, err := io.Copy(hash, file); err != nil {
log.Fatal(err)
}
sum := hash.Sum(nil)
finishTime:=time.Now().UnixNano() / int64(time.Millisecond)
response:= Response{
Hash: fmt.Sprintf("%x", sum),
TimeTakenMS: finishTime - startTime,
}
writer.Header().Add("Content-type", "application/json")
json,_:=json.Marshal(response)
writer.Write(json)
}