Skip to content

Commit 04f8b9e

Browse files
authored
Merge pull request #126 from carryall/release/2.5.0
Release 2.5.0
2 parents eca950b + 888deb7 commit 04f8b9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1829
-115
lines changed

bootstrap/router.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import (
77
"github.com/gin-gonic/gin"
88
)
99

10-
func SetupRouter() *gin.Engine {
11-
r := gin.Default()
10+
func SetupRouter(engine *gin.Engine) *gin.Engine {
11+
apiv1router.ComebineRoutes(engine)
12+
webrouter.ComebineRoutes(engine)
1213

13-
apiv1router.ComebineRoutes(r)
14-
webrouter.ComebineRoutes(r)
15-
16-
return r
14+
return engine
1715
}

bootstrap/session.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package bootstrap
2+
3+
import (
4+
"github.com/gin-contrib/sessions"
5+
"github.com/gin-contrib/sessions/cookie"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
const sessionMaxAge = 60 * 60 * 24 * 3 // 3 days
10+
11+
func SetupSession(engine *gin.Engine) *gin.Engine {
12+
store := cookie.NewStore([]byte("secret"))
13+
store.Options(sessions.Options{MaxAge: sessionMaxAge})
14+
engine.Use(sessions.Sessions("google_scraper_session", store))
15+
16+
return engine
17+
}

cmd/api/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ func main() {
1818

1919
database.InitDatabase(database.GetDatabaseURL())
2020

21-
r := bootstrap.SetupRouter()
21+
engine := gin.Default()
22+
engine = bootstrap.SetupSession(engine)
23+
engine = bootstrap.SetupRouter(engine)
2224

2325
oauth.SetUpOauth()
24-
2526
bootstrap.InitCron()
2627
view.SetupView()
2728

28-
err := r.Run(getAppPort())
29+
err := engine.Run(getAppPort())
2930
if err != nil {
3031
log.Fatal("Failed to start server: ", err)
3132
}

go.mod

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ go 1.18
55
require (
66
github.com/bxcodec/faker/v3 v3.8.0
77
github.com/dnaeon/go-vcr v1.2.0
8-
github.com/gin-gonic/gin v1.6.3
8+
github.com/foolin/goview v0.3.0
9+
github.com/gin-contrib/sessions v0.0.5
10+
github.com/gin-gonic/gin v1.7.4
911
github.com/go-co-op/gocron v1.13.0
10-
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
1112
github.com/gocolly/colly v1.2.0
1213
github.com/google/jsonapi v1.0.0
1314
github.com/google/uuid v1.3.0
1415
github.com/jackc/pgx/v4 v4.16.1
15-
github.com/michelloworld/ez-gin-template v0.0.0-20171028145326-7d77b0197797
1616
github.com/onsi/ginkgo v1.16.5
1717
github.com/onsi/gomega v1.18.1
1818
github.com/sirupsen/logrus v1.8.1
@@ -34,15 +34,18 @@ require (
3434
github.com/antchfx/xpath v1.2.1 // indirect
3535
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
3636
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
37-
github.com/foolin/goview v0.3.0 // indirect
3837
github.com/fsnotify/fsnotify v1.4.9 // indirect
3938
github.com/gin-contrib/sse v0.1.0 // indirect
39+
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
4040
github.com/go-playground/locales v0.13.0 // indirect
4141
github.com/go-playground/universal-translator v0.17.0 // indirect
42-
github.com/go-playground/validator/v10 v10.2.0 // indirect
42+
github.com/go-playground/validator/v10 v10.4.1 // indirect
4343
github.com/gobwas/glob v0.2.3 // indirect
4444
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
4545
github.com/golang/protobuf v1.5.2 // indirect
46+
github.com/gorilla/context v1.1.1 // indirect
47+
github.com/gorilla/securecookie v1.1.1
48+
github.com/gorilla/sessions v1.2.1 // indirect
4649
github.com/hashicorp/hcl v1.0.0 // indirect
4750
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
4851
github.com/jackc/pgconn v1.12.1 // indirect
@@ -79,7 +82,7 @@ require (
7982
github.com/vgarvardt/pgx-helpers/v4 v4.0.0-20200225100150-876aee3d1a22 // indirect
8083
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
8184
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
82-
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
85+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
8386
google.golang.org/appengine v1.6.1 // indirect
8487
google.golang.org/protobuf v1.26.0 // indirect
8588
gopkg.in/ini.v1 v1.51.0 // indirect

go.sum

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ github.com/antchfx/xpath v1.2.1/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwq
4343
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
4444
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
4545
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
46+
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
4647
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
4748
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
4849
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
@@ -96,12 +97,14 @@ github.com/gavv/httpexpect v0.0.0-20180803094507-bdde30871313/go.mod h1:x+9tiU1Y
9697
github.com/gavv/monotime v0.0.0-20171021193802-6f8212e8d10d h1:oYXrtNhqNKL1dVtKdv8XUq5zqdGVFNQ0/4tvccXZOLM=
9798
github.com/gavv/monotime v0.0.0-20171021193802-6f8212e8d10d/go.mod h1:vmp8DIyckQMXOPl0AQVHt+7n5h7Gb7hS6CUydiV8QeA=
9899
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
100+
github.com/gin-contrib/sessions v0.0.5 h1:CATtfHmLMQrMNpJRgzjWXD7worTh7g7ritsQfmF+0jE=
101+
github.com/gin-contrib/sessions v0.0.5/go.mod h1:vYAuaUPqie3WUSsft6HUlCjlwwoJQs97miaG2+7neKY=
99102
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
100103
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
101104
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
102105
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
103-
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
104-
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
106+
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
107+
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
105108
github.com/go-co-op/gocron v1.13.0 h1:BjkuNImPy5NuIPEifhWItFG7pYyr27cyjS6BN9w/D4c=
106109
github.com/go-co-op/gocron v1.13.0/go.mod h1:GD5EIEly1YNW+LovFVx5dzbYVcIc8544K99D8UVRpGo=
107110
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -110,16 +113,16 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
110113
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
111114
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
112115
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
113-
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
114-
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
116+
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
117+
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
115118
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
116119
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
117120
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
118121
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
119122
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
120123
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
121-
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
122-
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
124+
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
125+
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
123126
github.com/go-session/session v3.1.2+incompatible/go.mod h1:8B3iivBQjrz/JtC68Np2T1yBBLxTan3mn/3OM0CyRt0=
124127
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
125128
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -178,6 +181,12 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
178181
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
179182
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
180183
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
184+
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
185+
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
186+
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
187+
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
188+
github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
189+
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
181190
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
182191
github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY=
183192
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
@@ -304,9 +313,8 @@ github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8Nz
304313
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
305314
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
306315
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
307-
github.com/klauspost/compress v1.4.0 h1:8nsMz3tWa9SWWPL60G1V6CUsf4lLjWLTNEtibhe8gh8=
308316
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
309-
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e h1:+lIPJOWl+jSiJOc70QXJ07+2eg2Jy2EC7Mi11BWujeM=
317+
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
310318
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
311319
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
312320
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -347,8 +355,6 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
347355
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
348356
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
349357
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
350-
github.com/michelloworld/ez-gin-template v0.0.0-20171028145326-7d77b0197797 h1:B9nbOxQgHYxJhf5Ak7qmZaBP6EYfAi1MxMTiZytnIAg=
351-
github.com/michelloworld/ez-gin-template v0.0.0-20171028145326-7d77b0197797/go.mod h1:RJe+KA66iVMhkNpflNSFTcNpbsMxvtuvN9rAdfi9aGw=
352358
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
353359
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
354360
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
@@ -655,8 +661,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
655661
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
656662
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
657663
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
658-
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
659-
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
664+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw=
665+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
660666
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
661667
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
662668
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

helpers/authentication.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ package helpers
22

33
import (
44
"encoding/json"
5+
56
"go-google-scraper-challenge/helpers/log"
6-
"go-google-scraper-challenge/lib/api/v1/serializers"
77

88
"golang.org/x/crypto/bcrypt"
99
)
1010

11+
type AuthenticationToken struct {
12+
AccessToken string `json:"access_token"`
13+
RefreshToken string `json:"refresh_token"`
14+
ExpiresIn int64 `json:"expires_in"`
15+
TokenType string `json:"token_type"`
16+
}
17+
1118
// HashPassword hash given password, returns hashed password and error
1219
func HashPassword(password string) (string, error) {
1320
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
@@ -32,8 +39,8 @@ func CompareHashWithPassword(hashedPassword string, password string) bool {
3239
return true
3340
}
3441

35-
func GetTokenInfo(tokenData map[string]interface{}) (tokenInfo *serializers.AuthenticationToken, err error) {
36-
tokenInfo = &serializers.AuthenticationToken{}
42+
func GetTokenInfo(tokenData map[string]interface{}) (tokenInfo *AuthenticationToken, err error) {
43+
tokenInfo = &AuthenticationToken{}
3744
tokenDataString, err := json.Marshal(tokenData)
3845
if err != nil {
3946
return nil, err

helpers/authentication_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package helpers_test
22

33
import (
4+
"go-google-scraper-challenge/helpers"
5+
46
"github.com/bxcodec/faker/v3"
57
. "github.com/onsi/ginkgo"
68
. "github.com/onsi/gomega"
79
"golang.org/x/crypto/bcrypt"
8-
9-
"go-google-scraper-challenge/helpers"
1010
)
1111

1212
var _ = Describe("Authentication", func() {

helpers/string.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ func ToSnakeCase(str string) string {
4545

4646
return strings.ToLower(snake)
4747
}
48+
49+
func ToTitleCase(str string) string {
50+
return strings.ToTitle(str)
51+
}

helpers/string_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,24 @@ var _ = Describe("String", func() {
6767
})
6868
})
6969
})
70+
71+
Describe("#ToTitleCase", func() {
72+
Context("given a camel case string", func() {
73+
It("returns a title case string", func() {
74+
Expect(helpers.ToTitleCase("CamelCaseString")).To(Equal("CAMELCASESTRING"))
75+
})
76+
})
77+
78+
Context("given a kebab case string", func() {
79+
It("returns a title case string", func() {
80+
Expect(helpers.ToTitleCase("kebab-case-string")).To(Equal("KEBAB-CASE-STRING"))
81+
})
82+
})
83+
84+
Context("given a sentence case string", func() {
85+
It("returns a title case string", func() {
86+
Expect(helpers.ToTitleCase("Sentence case string")).To(Equal("SENTENCE CASE STRING"))
87+
})
88+
})
89+
})
7090
})

helpers/web/response.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package helpers
2+
3+
import (
4+
"net/http"
5+
6+
"go-google-scraper-challenge/constants"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
var ActionsWithGetMethod = []string{"index", "new", "show", "edit", "delete", "cache"}
12+
13+
func HandleUnauthorizedRequest(ctx *gin.Context, actionName string) {
14+
isGetMethod := false
15+
for _, action := range ActionsWithGetMethod {
16+
if action == actionName {
17+
isGetMethod = true
18+
}
19+
}
20+
21+
if isGetMethod {
22+
signInPath := constants.WebRoutes["sessions"]["new"]
23+
ctx.Redirect(http.StatusFound, signInPath)
24+
} else {
25+
ctx.AbortWithStatus(http.StatusMethodNotAllowed)
26+
}
27+
}
28+
29+
func RedirectToDashboard(ctx *gin.Context) {
30+
dashboardPath := constants.WebRoutes["results"]["index"]
31+
ctx.Redirect(http.StatusFound, dashboardPath)
32+
}

0 commit comments

Comments
 (0)