-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
48 lines (37 loc) · 907 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func initViper() {
pflag.String("keepass-file", "", "KeepassDB file path")
pflag.Int("http-port", 8080, "Port to listen on")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}
func init() {
initViper()
}
func main() {
router := httprouter.New()
router.GET("/search", SearchHandler)
router.POST("/reload", ReloadHandler)
addr := fmt.Sprintf(":%d", viper.GetInt("http-port"))
log.Fatal(http.ListenAndServe(addr, router))
}
func respondWithError(w http.ResponseWriter, err error, status int) {
response := errorResponse{Error: err.Error()}
json, err := json.Marshal(&response)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}