-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
144 lines (121 loc) · 3.55 KB
/
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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gerald1248/httpscerts"
"io/ioutil"
"log"
"net/http"
"time"
)
type PostStruct struct {
Buffer string
}
func serve(certificate, key, hostname string, port int) {
virtual_fs := &assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo}
//set up custom mux
mux := http.NewServeMux()
mux.Handle("/static/", http.FileServer(virtual_fs))
mux.HandleFunc("/openshift-linter/report", guiHandler)
mux.HandleFunc("/openshift-linter", handler)
// exception: no TLS req'd
if port%2 == 0 {
fmt.Print(listening(hostname, port, false))
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", hostname, port), mux))
return
}
err := httpscerts.Check(certificate, key)
if err != nil {
cert, key, err := httpscerts.GenerateArrays(fmt.Sprintf("%s:%d", hostname, port))
if err != nil {
log.Fatal("Can't create https certs")
}
keyPair, err := tls.X509KeyPair(cert, key)
if err != nil {
log.Fatal("Can't create key pair")
}
//supply root certs in case code is run in from-scratch image
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(rootCerts)
var certificates []tls.Certificate
certificates = append(certificates, keyPair)
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
Certificates: certificates,
RootCAs: pool,
}
s := &http.Server{
Addr: fmt.Sprintf("%s:%d", hostname, port),
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
TLSConfig: cfg,
}
fmt.Print(listening(hostname, port, true))
log.Fatal(s.ListenAndServeTLS("", ""))
}
fmt.Print(listening(hostname, port, false))
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", hostname, port), certificate, key, mux))
}
func listening(hostname string, port int, selfCert bool) string {
var selfCertMsg string
if selfCert {
selfCertMsg = " (self-certified)"
}
protocol := "https"
if port%2 == 0 {
protocol = "http"
}
// for Docker, mustn't specify localhost explicitly
if hostname == "" {
hostname = "0.0.0.0"
}
return fmt.Sprintf("Listening on port %d%s\n"+
"POST JSON sources to %s://%s:%d/openshift-linter\n"+
"Generate report at %s://%s:%d/openshift-linter/report\n", port, selfCertMsg, protocol, hostname, port, protocol, hostname, port)
}
func guiHandler(w http.ResponseWriter, r *http.Request) {
//show GUI from static resources
bytes, _ := Asset("static/index.html")
fmt.Fprintf(w, "%s\n", string(bytes))
}
func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
handlePost(&w, r)
case "GET":
handleGet(&w, r)
}
}
func handleGet(w *http.ResponseWriter, r *http.Request) {
//fetch configuration list
fmt.Fprintf(*w, "GET request\nRequest struct = %v\n", r)
}
func handlePost(w *http.ResponseWriter, r *http.Request) {
//process configuration list
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(*w, "Can't read POST request body: %s", err)
return
}
any := ".*" //for POST requests, patterns are specified within config body
combinedResultMap, err := processBytes(body, LinterParams{any, any, any, any, "", any, any, "json"})
if err != nil {
fmt.Fprintf(*w, "Can't process POST request body: %s\n%s\n", err, body)
return
}
json, err := json.Marshal(combinedResultMap)
if err != nil {
fmt.Fprintf(*w, "Can't encode result: %s", err)
return
}
fmt.Fprintf(*w, string(json))
}