Skip to content

Commit d79bc2d

Browse files
AndresCidoncharaphink
authored andcommitted
refactor: Rename basePath-related functions
* Now it's more readable * Also add test for SetBasePath method * Fix dependent tests
1 parent 4fd47b4 commit d79bc2d

File tree

4 files changed

+58
-43
lines changed

4 files changed

+58
-43
lines changed

api/api.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ListStateStats(w http.ResponseWriter, r *http.Request, d *db.Database) {
7575
// GetState provides information on a State
7676
func GetState(w http.ResponseWriter, r *http.Request, d *db.Database) {
7777
w.Header().Set("Access-Control-Allow-Origin", "*")
78-
st := util.TrimBase(r, "api/state/")
78+
st := util.TrimBasePath(r, "api/state/")
7979
versionID := r.URL.Query().Get("versionid")
8080
var err error
8181
if versionID == "" {
@@ -98,7 +98,7 @@ func GetState(w http.ResponseWriter, r *http.Request, d *db.Database) {
9898
// GetStateActivity returns the activity (version history) of a State
9999
func GetStateActivity(w http.ResponseWriter, r *http.Request, d *db.Database) {
100100
w.Header().Set("Access-Control-Allow-Origin", "*")
101-
st := util.TrimBase(r, "api/state/activity/")
101+
st := util.TrimBasePath(r, "api/state/activity/")
102102
activity := d.GetStateActivity(st)
103103

104104
jActivity, err := json.Marshal(activity)
@@ -112,7 +112,7 @@ func GetStateActivity(w http.ResponseWriter, r *http.Request, d *db.Database) {
112112
// StateCompare compares two versions ('from' and 'to') of a State
113113
func StateCompare(w http.ResponseWriter, r *http.Request, d *db.Database) {
114114
w.Header().Set("Access-Control-Allow-Origin", "*")
115-
st := util.TrimBase(r, "api/state/compare/")
115+
st := util.TrimBasePath(r, "api/state/compare/")
116116
query := r.URL.Query()
117117
fromVersion := query.Get("from")
118118
toVersion := query.Get("to")

main.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func idx(w http.ResponseWriter, r *http.Request) {
2828
// TODO: Return error page
2929
}
3030
idxStr := string(idx)
31-
idxStr = util.ReplaceBase(idxStr, "base href=\"/\"", "base href=\"%s\"")
31+
idxStr = util.ReplaceBasePath(idxStr, "base href=\"/\"", "base href=\"%s\"")
3232
io.WriteString(w, idxStr)
3333
}
3434

@@ -129,7 +129,7 @@ func getVersion(w http.ResponseWriter, r *http.Request) {
129129
func main() {
130130
c := config.LoadConfig(version)
131131

132-
util.UpdateBase(c.Web.BaseURL)
132+
util.SetBasePath(c.Web.BaseURL)
133133

134134
log.Infof("Terraboard v%s is starting...", version)
135135

@@ -157,28 +157,28 @@ func main() {
157157
defer database.Close()
158158

159159
// Index is a wildcard for all paths
160-
http.HandleFunc(util.AddBase(""), idx)
160+
http.HandleFunc(util.GetFullPath(""), idx)
161161

162162
// Serve static files (CSS, JS, images) from dir
163163
staticFs := http.FileServer(http.Dir("static"))
164-
http.Handle(util.AddBase("static/"), http.StripPrefix(util.AddBase("static"), staticFs))
164+
http.Handle(util.GetFullPath("static/"), http.StripPrefix(util.GetFullPath("static"), staticFs))
165165

166166
// Handle API points
167-
http.HandleFunc(util.AddBase("api/version"), getVersion)
168-
http.HandleFunc(util.AddBase("api/user"), api.GetUser)
169-
http.HandleFunc(util.AddBase("api/states"), handleWithDB(api.ListStates, database))
170-
http.HandleFunc(util.AddBase("api/states/stats"), handleWithDB(api.ListStateStats, database))
171-
http.HandleFunc(util.AddBase("api/states/tfversion/count"), handleWithDB(api.ListTerraformVersionsWithCount, database))
172-
http.HandleFunc(util.AddBase("api/state/"), handleWithDB(api.GetState, database))
173-
http.HandleFunc(util.AddBase("api/state/activity/"), handleWithDB(api.GetStateActivity, database))
174-
http.HandleFunc(util.AddBase("api/state/compare/"), handleWithDB(api.StateCompare, database))
175-
http.HandleFunc(util.AddBase("api/locks"), api.GetLocks)
176-
http.HandleFunc(util.AddBase("api/search/attribute"), handleWithDB(api.SearchAttribute, database))
177-
http.HandleFunc(util.AddBase("api/resource/types"), handleWithDB(api.ListResourceTypes, database))
178-
http.HandleFunc(util.AddBase("api/resource/types/count"), handleWithDB(api.ListResourceTypesWithCount, database))
179-
http.HandleFunc(util.AddBase("api/resource/names"), handleWithDB(api.ListResourceNames, database))
180-
http.HandleFunc(util.AddBase("api/attribute/keys"), handleWithDB(api.ListAttributeKeys, database))
181-
http.HandleFunc(util.AddBase("api/tf_versions"), handleWithDB(api.ListTfVersions, database))
167+
http.HandleFunc(util.GetFullPath("api/version"), getVersion)
168+
http.HandleFunc(util.GetFullPath("api/user"), api.GetUser)
169+
http.HandleFunc(util.GetFullPath("api/states"), handleWithDB(api.ListStates, database))
170+
http.HandleFunc(util.GetFullPath("api/states/stats"), handleWithDB(api.ListStateStats, database))
171+
http.HandleFunc(util.GetFullPath("api/states/tfversion/count"), handleWithDB(api.ListTerraformVersionsWithCount, database))
172+
http.HandleFunc(util.GetFullPath("api/state/"), handleWithDB(api.GetState, database))
173+
http.HandleFunc(util.GetFullPath("api/state/activity/"), handleWithDB(api.GetStateActivity, database))
174+
http.HandleFunc(util.GetFullPath("api/state/compare/"), handleWithDB(api.StateCompare, database))
175+
http.HandleFunc(util.GetFullPath("api/locks"), api.GetLocks)
176+
http.HandleFunc(util.GetFullPath("api/search/attribute"), handleWithDB(api.SearchAttribute, database))
177+
http.HandleFunc(util.GetFullPath("api/resource/types"), handleWithDB(api.ListResourceTypes, database))
178+
http.HandleFunc(util.GetFullPath("api/resource/types/count"), handleWithDB(api.ListResourceTypesWithCount, database))
179+
http.HandleFunc(util.GetFullPath("api/resource/names"), handleWithDB(api.ListResourceNames, database))
180+
http.HandleFunc(util.GetFullPath("api/attribute/keys"), handleWithDB(api.ListAttributeKeys, database))
181+
http.HandleFunc(util.GetFullPath("api/tf_versions"), handleWithDB(api.ListTfVersions, database))
182182

183183
// Start server
184184
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", c.Port), nil))

util/util.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import (
66
"strings"
77
)
88

9-
var baseURL string
9+
var basePath string
1010

11-
// UpdateBase replaces baseURL with a new one
12-
func UpdateBase(new string) {
13-
baseURL = new
11+
// SetBasePath replaces basePath with a new one
12+
func SetBasePath(new string) {
13+
basePath = new
1414
}
1515

16-
// ReplaceBase replaces a pattern in a string, injecting baseURL into it
17-
func ReplaceBase(str, old, new string) string {
18-
return strings.Replace(str, old, fmt.Sprintf(new, baseURL), 1)
16+
// ReplaceBasePath replaces a pattern in a string, injecting basePath into it
17+
func ReplaceBasePath(str, old, new string) string {
18+
return strings.Replace(str, old, fmt.Sprintf(new, basePath), 1)
1919
}
2020

21-
// AddBase preprends baseURL to a string
22-
func AddBase(path string) string {
23-
return fmt.Sprintf("%s%s", baseURL, path)
21+
// GetFullPath preprends basePath to a string
22+
func GetFullPath(path string) string {
23+
return fmt.Sprintf("%s%s", basePath, path)
2424
}
2525

26-
// TrimBase removes baseURL from the beginning of a string
27-
func TrimBase(r *http.Request, prefix string) string {
28-
return strings.TrimPrefix(r.URL.Path, AddBase(prefix))
26+
// TrimBasePath removes basePath from the beginning of a string
27+
func TrimBasePath(r *http.Request, prefix string) string {
28+
return strings.TrimPrefix(r.URL.Path, GetFullPath(prefix))
2929
}

util/util_test.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,52 @@
11
package util
22

33
import (
4-
"testing"
54
"net/http"
5+
"testing"
66
)
77

8-
func TestAddBase(t *testing.T) {
8+
func TestSetBasePath(t *testing.T) {
9+
newBase := "/test/"
10+
expectedStr := newBase
11+
12+
SetBasePath(newBase)
13+
14+
result := GetFullPath("")
15+
16+
if result != expectedStr {
17+
t.Fatalf("Expected %s, got %s", expectedStr, result)
18+
}
19+
}
20+
21+
func TestGetFullPath(t *testing.T) {
922
expectedStr := "/mypath/"
1023

11-
result := AddBase("mypath/")
24+
SetBasePath("/")
25+
result := GetFullPath("mypath/")
1226

1327
if result != expectedStr {
1428
t.Fatalf("Expected %s, got %s", expectedStr, result)
1529
}
1630
}
1731

18-
func TestReplaceBase(t *testing.T) {
32+
func TestReplaceBasePath(t *testing.T) {
1933
expectedStr := "<a href=\"/\">root</a>"
2034
str := "<a href=\"/fakebase/\">root</a>"
2135

22-
result := ReplaceBase(str, "href=\"/fakebase/\"", "href=\"%s\"")
36+
SetBasePath("/")
37+
result := ReplaceBasePath(str, "href=\"/fakebase/\"", "href=\"%s\"")
2338

2439
if result != expectedStr {
2540
t.Fatalf("Expected %s, got %s", expectedStr, result)
2641
}
2742
}
2843

29-
30-
func TestTrimBase(t *testing.T) {
44+
func TestTrimBasePath(t *testing.T) {
3145
expectedStr := ""
3246

47+
SetBasePath("/")
3348
req, _ := http.NewRequest("GET", "/api/state", nil)
34-
result := TrimBase(req, "api/state")
49+
result := TrimBasePath(req, "api/state")
3550

3651
if result != expectedStr {
3752
t.Fatalf("Expected %s, got %s", expectedStr, result)

0 commit comments

Comments
 (0)