forked from MaibornWolff/vbump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
154 lines (132 loc) · 4.57 KB
/
handler.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
package main
import (
"net/http"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
//Handler for handling http routes
type Handler struct {
version *Version
logger *log.Logger
}
//NewHandler constructs a new handler
func NewHandler(version *Version, logger *log.Logger) *Handler {
if logger == nil {
logger = log.New()
}
return &Handler{
version: version,
logger: logger,
}
}
//GetRouter configure all routes
func (handler *Handler) GetRouter() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/major/{project}", handler.OnMajor).Methods("POST")
r.HandleFunc("/minor/{project}", handler.OnMinor).Methods("POST")
r.HandleFunc("/patch/{project}", handler.OnPatch).Methods("POST")
r.HandleFunc("/minor/transient/{version}", handler.OnTransientMinor).Methods("POST")
r.HandleFunc("/patch/transient/{version}", handler.OnTransientPatch).Methods("POST")
r.HandleFunc("/version/{project}/{version}", handler.OnSetVersion).Methods("POST")
r.HandleFunc("/version/{project}", handler.OnGetVersion).Methods("GET")
r.HandleFunc("/", handler.OnHealth)
return r
}
//OnHealth is a handler for a health check
func (handler *Handler) OnHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello from vbump!"))
}
//OnMajor is a handler for bumping the major part for a given project
func (handler *Handler) OnMajor(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.BumpMajor(vars["project"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
handler.logger.Infof("bump major version to %v on project %v", version, vars["project"])
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
//OnMinor is a handler for bumping the minor part for a given project
func (handler *Handler) OnMinor(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.BumpMinor(vars["project"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
handler.logger.Infof("bump minor version to %v on project %v", version, vars["project"])
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
//OnPatch is a handler for bump the patch part for a given project
func (handler *Handler) OnPatch(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.BumpPatch(vars["project"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
handler.logger.Infof("bump patch version to %v on project %v", version, vars["project"])
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
//OnSetVersion is a handler for setting the version for a given project
func (handler *Handler) OnSetVersion(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.SetVersion(vars["project"], vars["version"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte(err.Error()))
return
}
handler.logger.Infof("set version explicitly to %v on project %v", version, vars["project"])
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
//OnGetVersion is a handler for getting the version for a given project
func (handler *Handler) OnGetVersion(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.GetVersion(vars["project"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(err.Error()))
return
}
handler.logger.Infof("get version from project %v", vars["project"])
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
//OnTransientPatch is a handler for a transient patch bumps
func (handler *Handler) OnTransientPatch(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.BumpTransientPatch(vars["version"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
handler.logger.Infof("bump transient patch version to %v", version)
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}
//OnTransientMinor is a handler for a transient minor bumps
func (handler *Handler) OnTransientMinor(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
version, err := handler.version.BumpTransientMinor(vars["version"])
if err != nil {
handler.logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
handler.logger.Infof("bump transient minor version to %v", version)
w.WriteHeader(http.StatusOK)
w.Write([]byte(version))
}