-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
45 lines (37 loc) · 1018 Bytes
/
server.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
package resume
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
)
// InitServer starts the resume server
func InitServer(flgPort, flgPath, flgTemplate, flgUser, flgPass string) {
colors := GetANSIColors()
tmpl, err := template.New(flgTemplate).Funcs(Funcs).ParseFiles(flgTemplate)
if err != nil {
log.Fatal(err)
}
http.HandleFunc(flgPath, func(w http.ResponseWriter, r *http.Request) {
// authenticate when user and pass flags are set
if flgUser != "" && flgPass != "" {
if auth := Authenticated(r, flgUser, flgPass); auth == false {
w.Header().Set("WWW-Authenticate", "password protected")
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
return
}
}
// create buffer to write results to
buf := new(bytes.Buffer)
// execute template with colors context
err = tmpl.Execute(buf, colors)
if err != nil {
log.Fatal(err)
}
// output results
fmt.Fprint(w, buf.String())
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", flgPort), nil))
}