-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (67 loc) · 2.13 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
package main
import (
"encoding/json"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"html/template"
"net/http"
)
type PageValue struct {
List []*Alias
}
type Alias struct {
Alias string
Url string
}
func main() {
var templates = template.Must(template.ParseFiles("./html/config.html"))
initConfig()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[1:]
if url := viper.GetString(path); url != "" {
http.Redirect(w, r, url, http.StatusFound)
} else {
http.Redirect(w, r, "/config", http.StatusFound)
}
})
// config html
http.HandleFunc("/config", func(writer http.ResponseWriter, r *http.Request) {
list := make([]*Alias, 0)
for _, key := range viper.AllKeys() {
if value := viper.GetString(key); value != "" {
list = append(list, &Alias{key, value})
}
}
templates.ExecuteTemplate(writer, "config.html", &PageValue{List: list})
})
// config json
http.HandleFunc("/json", func(writer http.ResponseWriter, r *http.Request) {
list := make([]*Alias, 0)
for _, key := range viper.AllKeys() {
if value := viper.GetString(key); value != "" {
list = append(list, &Alias{key, value})
}
}
writer.Header().Set("Content-Type", "application/json; charset=UTF-8")
writer.WriteHeader(http.StatusOK)
if err := json.NewEncoder(writer).Encode(list); err != nil {
panic(err)
}
})
http.ListenAndServe(":80", nil)
}
func initConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("$HOME/.url-alias") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
}