feat: initial commit
This commit is contained in:
28
internal/http/context/base_url.go
Normal file
28
internal/http/context/base_url.go
Normal file
@ -0,0 +1,28 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const keyBaseURL = "baseURL"
|
||||
|
||||
func BaseURL(ctx context.Context) *url.URL {
|
||||
rawBaseURL, ok := ctx.Value(keyBaseURL).(string)
|
||||
if !ok {
|
||||
panic(errors.New("no base url in context"))
|
||||
}
|
||||
|
||||
baseURL, err := url.Parse(rawBaseURL)
|
||||
if err != nil {
|
||||
panic(errors.WithStack(err))
|
||||
}
|
||||
|
||||
return baseURL
|
||||
}
|
||||
|
||||
func SetBaseURL(ctx context.Context, baseURL string) context.Context {
|
||||
return context.WithValue(ctx, keyBaseURL, baseURL)
|
||||
}
|
3
internal/http/context/context.go
Normal file
3
internal/http/context/context.go
Normal file
@ -0,0 +1,3 @@
|
||||
package context
|
||||
|
||||
type key string
|
23
internal/http/context/current_url.go
Normal file
23
internal/http/context/current_url.go
Normal file
@ -0,0 +1,23 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const keyCurrentURL = "currentURL"
|
||||
|
||||
func CurrentURL(ctx context.Context) *url.URL {
|
||||
currentURL, ok := ctx.Value(keyCurrentURL).(*url.URL)
|
||||
if !ok {
|
||||
panic(errors.New("no current url in context"))
|
||||
}
|
||||
|
||||
return currentURL
|
||||
}
|
||||
|
||||
func SetCurrentURL(ctx context.Context, u *url.URL) context.Context {
|
||||
return context.WithValue(ctx, keyCurrentURL, u)
|
||||
}
|
23
internal/http/context/user.go
Normal file
23
internal/http/context/user.go
Normal file
@ -0,0 +1,23 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/markbates/goth"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const keyUser = "user"
|
||||
|
||||
func User(ctx context.Context) *goth.User {
|
||||
user, ok := ctx.Value(keyUser).(*goth.User)
|
||||
if !ok {
|
||||
panic(errors.New("no user in context"))
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
func SetUser(ctx context.Context, user *goth.User) context.Context {
|
||||
return context.WithValue(ctx, keyUser, user)
|
||||
}
|
Reference in New Issue
Block a user