-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
75 lines (57 loc) · 1.39 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/mervick/aes-everywhere/go/aes256"
)
var (
configPath string
encryptPassword = "somehardpw"
// BasicAuth functionality
BasicAuth bool
BAUsername string
BAPassword string
// server utility
bind string
)
// getConfig : handler for getting config data
func getConfig(w http.ResponseWriter, r *http.Request) {
if BasicAuth == true {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
username, password, authOK := r.BasicAuth()
if authOK == false {
http.Error(w, "Not authorized", 401)
return
}
if username != BAUsername && password != BAPassword {
http.Error(w, "Not authorized", 401)
return
}
}
// filename of project id
configFile := r.URL.Query().Get("id")
// path already end with slash
path := configPath + "/" + configFile
if fileExists(path) {
b, err := ioutil.ReadFile(path)
if err != nil {
http.Error(w, "Failed to read file", 500)
return
}
encrypted := aes256.Encrypt(string(b), encryptPassword)
// return json content from file as string
w.Write([]byte(encrypted))
return
}
http.Error(w, "File doesn't exist!", 404)
}
func main() {
flag.StringVar(&bind, "bind", ":8080", "-bind 0.0.0.0:3000")
flag.Parse()
http.HandleFunc("/config", getConfig)
fmt.Println("starting to listen on", bind)
log.Fatal(http.ListenAndServe(bind, nil))
}