-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (60 loc) · 1.52 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/meox/meox.dev/contentwriter"
"github.com/meox/meox.dev/cv_parser"
log "github.com/sirupsen/logrus"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
mux := http.NewServeMux()
mux.HandleFunc("/", myCv)
log.Infof("server starting on port %s", port)
err := http.ListenAndServe(fmt.Sprintf(":%s", port), mux)
log.Info("server closed: %v", err)
}
func myCv(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
log.Warn("bath method: %s", req.Method)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// instantiate the right content-writer
var contentWriter contentwriter.ContentWriter
accept := req.Header.Get("Accept")
if strings.Contains(accept, "text/html") {
contentWriter = contentwriter.NewHtml(w)
} else {
contentWriter = contentwriter.NewPlain(w)
}
contentWriter.ContentType()
fd, err := cv_parser.Open("cv/meox.md")
if err != nil {
w.WriteHeader(http.StatusNotFound)
log.Errorf("error opening cv: %v", err)
return
}
err = contentWriter.Header()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorf("cannot write header: %v", err)
return
}
body, err := fd.Parse(contentWriter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorf("error parsing cv: %v", err)
return
}
_, err = w.Write([]byte(body))
if err != nil {
log.Warnf("writing resp to the server: %v", err)
}
}