-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (70 loc) · 1.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
)
type BupwConfig struct {
Httpserver struct {
Port string `yaml:"port"`
Query string `yaml:"query"`
}
Files []string
}
type BupwResponse struct {
Status bool `json:"status"`
}
var bupwConfig *BupwConfig
var words map[string]bool
func main() {
config, err := getProjectConfig()
bupwConfig = config
if err != nil {
log.Fatalln(err.Error())
}
words = readFiles(config.Files)
log.Printf("Imported words: %d \n", len(words))
if err != nil {
log.Fatalln(err.Error())
}
http.HandleFunc("/", handleRequest)
log.Printf("Start server: %s \n", ":"+config.Httpserver.Port)
http.ListenAndServe(":"+config.Httpserver.Port, nil)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
passwordQuery := r.URL.Query().Get(bupwConfig.Httpserver.Query)
log.Printf("Incoming password: %s \n", passwordQuery)
status := false
if words[passwordQuery] {
status = true
}
// json resposne
js, err := json.Marshal(BupwResponse{status})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// send reponse
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(js)
}
func getProjectConfig() (*BupwConfig, error) {
config := &BupwConfig{}
if err := YamlUnmarshal("./config.yml", config); err != nil {
return nil, err
}
return config, nil
}
func YamlUnmarshal(path string, out interface{}) error {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if err := yaml.Unmarshal(bytes, out); err != nil {
return err
}
return nil
}