-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
157 lines (134 loc) · 3.52 KB
/
service.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
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
//go:generate go run scripts/ui.go
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/StevenLeRoux/goslash/golang/redirect"
"github.com/StevenLeRoux/goslash/golang/store"
"github.com/StevenLeRoux/goslash/golang/store/model"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
log "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
type goslash struct {
config *viper.Viper
store store.Store
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
if status == 0 {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(status)
}
fmt.Fprintf(w, "Error: "+strconv.Itoa(status))
}
func (g goslash) GoHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
raw := params["alias"]
args := strings.Split(raw, "/")
fmt.Println("Args: ")
fmt.Println(args)
alias := args[0]
value, ok := g.store.Get(alias)
fmt.Println("GoHandler status : ", ok)
if !ok {
errorHandler(w, r, http.StatusNotFound)
return
}
target := value.Target
fmt.Println("target : " + target)
//http.Redirect(w, r, target, http.StatusFound)
p := redirect.Apply(target, args)
log.INFO.Println("RedirectTo : ", p)
//if err != nil {
http.Redirect(w, r, p, http.StatusFound)
//} else {
// fmt.Println("error: " + err.Error())
// errorHandler(w, r, http.StatusNotFound)
//}
return
}
func (g goslash) PutHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
raw := params["alias"]
args := strings.Split(raw, "/")
fmt.Println("Args: ", args)
alias := args[0]
v := model.Values{
Alias: alias,
Target: "Target",
User: "User",
Created: "Created",
Modified: "Modified",
Description: "Description",
}
err := g.store.Put(v)
if err != nil {
log.ERROR.Println("Error while trying to Put() on store =>", err)
errorHandler(w, r, http.StatusNotModified)
return
}
return
}
func Get200(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "200 OK")
}
func GetAlias(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, alias)
}
func (g goslash) ReloadHandler(w http.ResponseWriter, r *http.Request) {
g.store.Reload()
w.WriteHeader(http.StatusAccepted)
}
func (g goslash) DumpHandler(w http.ResponseWriter, r *http.Request) {
values, ok := g.store.Dump()
if !ok {
errorHandler(w, r, http.StatusNotFound)
return
}
data, err := json.Marshal(values)
if err != nil {
errorHandler(w, r, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func runService(cmd *cobra.Command) {
config, err := InitializeConfig()
if err != nil {
log.FATAL.Fatalln(err)
}
var s *store.Store
s, err = store.New(config)
if err != nil {
log.ERROR.Panic(err)
}
g := &goslash{
config: config,
store: *s,
}
r := mux.NewRouter()
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/200", Get200).Methods("GET")
r.HandleFunc("/reload", g.ReloadHandler).Methods("GET")
r.HandleFunc("/dump", g.DumpHandler).Methods("GET")
r.HandleFunc("/set/{alias:.+}", g.PutHandler).Methods("PUT")
r.HandleFunc("/{alias:.+}", g.GoHandler).Methods("GET")
r.HandleFunc("/", GetAlias).Methods("GET")
http.Handle("/", r)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.FATAL.Fatal("ListenAndServe: ", err)
fmt.Printf("ListenAndServe:%s\n", err.Error())
}
}