Skip to content

Commit

Permalink
refactoring/renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Aug 25, 2024
1 parent 22feb04 commit ae2fd7b
Show file tree
Hide file tree
Showing 35 changed files with 210 additions and 317 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/webtor-io/web-ui-v2

go 1.22.0
go 1.23

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
Expand Down Expand Up @@ -56,7 +56,7 @@ require (
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-pg/migrations/v8 v8.1.0 // indirect
github.com/go-pg/pg/v10 v10.13.0 // indirect
github.com/go-pg/pg/v10 v10.13.0
github.com/go-pg/zerochecker v0.2.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand All @@ -69,7 +69,6 @@ require (
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.2.2 // indirect
Expand Down Expand Up @@ -127,7 +126,6 @@ require (
golang.org/x/term v0.20.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
Expand Down
153 changes: 17 additions & 136 deletions go.sum

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#-------------------------------------------------------------------------------#
# Qodana analysis is configured by qodana.yaml file #
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
#-------------------------------------------------------------------------------#
version: "1.0"

#Specify inspection profile for code analysis
profile:
name: qodana.starter

#Enable inspections
#include:
# - name: <SomeEnabledInspectionId>

#Disable inspections
#exclude:
# - name: <SomeDisabledInspectionId>
# paths:
# - <path/where/not/run/inspection>

#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
#bootstrap: sh ./prepare-qodana.sh

#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
#plugins:
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)

#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
linter: jetbrains/qodana-go:latest
24 changes: 12 additions & 12 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func serve(c *cli.Context) error {
// redis := cs.NewRedisClient(c)
// defer redis.Close()

servers := []cs.Servable{}
var servers []cs.Servable
// Setting Probe
probe := cs.NewProbe(c)
servers = append(servers, probe)
Expand Down Expand Up @@ -110,7 +110,7 @@ func serve(c *cli.Context) error {
cl := http.DefaultClient

// Setting Api
api := api.New(c, cl)
sapi := api.New(c, cl)

// Setting Helper
helper := w.NewHelper(c)
Expand All @@ -125,7 +125,7 @@ func serve(c *cli.Context) error {
queues := job.NewQueues(job.NewStorage(redis, gin.Mode()))

// Setting JobHandler
jobs := wj.New(queues, tm, api)
jobs := wj.New(queues, tm, sapi)

jobs.RegisterHandler(r)

Expand All @@ -138,7 +138,7 @@ func serve(c *cli.Context) error {
return err
}
a.RegisterHandler(r)
wau.RegisterHandler(c, r, tm)
wau.RegisterHandler(r, tm)
}

// Setting Claims Client
Expand All @@ -149,32 +149,32 @@ func serve(c *cli.Context) error {
uc := claims.New(c, cpCl)
if uc != nil {
// Setting UserClaimsHandler
uc.RegisterHandler(c, r)
uc.RegisterHandler(r)
}

// Setting DomainSettings
ds := embed.NewDomainSettings(pg, uc)

// Setting ApiClaimsHandler
api.RegisterHandler(c, r)
sapi.RegisterHandler(r)

// Setting ResourceHandler
wr.RegisterHandler(c, r, tm, api, jobs)
wr.RegisterHandler(r, tm, sapi, jobs)

// Setting IndexHandler
wi.RegisterHandler(c, r, tm)
wi.RegisterHandler(r, tm)

// Setting ActionHandler
wa.RegisterHandler(c, r, tm, jobs)
wa.RegisterHandler(r, tm, jobs)

// Setting ProfileHandler
p.RegisterHandler(c, r, tm)
p.RegisterHandler(r, tm)

// Setting EmbedExamplesHandler
wee.RegisterHandler(c, r, tm)
wee.RegisterHandler(r, tm)

// Setting EmbedHandler
we.RegisterHandler(c, cl, r, tm, jobs, ds)
we.RegisterHandler(cl, r, tm, jobs, ds)

// Render templates
err = tm.Init()
Expand Down
29 changes: 16 additions & 13 deletions services/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ func New(c *cli.Context, cl *http.Client) *Api {
}
}
log.Infof("api endpoint %v", u)
url, _ := url.Parse(c.String(services.DomainFlag))
apiURL, _ := url.Parse(c.String(services.DomainFlag))
return &Api{
url: u,
cl: cl,
prepareRequest: prepareRequest,
domain: url.Host,
domain: apiURL.Host,
}
}

Expand Down Expand Up @@ -343,6 +343,10 @@ func (s *Api) Download(ctx context.Context, u string) (io.ReadCloser, error) {

func (s *Api) DownloadWithRange(ctx context.Context, u string, start int, end int) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil {
log.WithError(err).Error("failed to make new request")
return nil, err
}
if start != 0 || end != -1 {
startStr := strconv.Itoa(start)
endStr := ""
Expand All @@ -351,10 +355,6 @@ func (s *Api) DownloadWithRange(ctx context.Context, u string, start int, end in
}
req.Header.Set("Range", fmt.Sprintf("bytes=%v-%v", startStr, endStr))
}
if err != nil {
log.WithError(err).Error("failed to make new request")
return nil, err
}
res, err := s.cl.Do(req)
if err != nil {
log.WithError(err).Error("failed to do request")
Expand All @@ -379,7 +379,9 @@ func (s *Api) GetOpenSubtitles(ctx context.Context, u string) ([]OpenSubtitleTra
return nil, errors.Wrap(err, "failed to do request")
}
b := res.Body
defer b.Close()
defer func(b io.ReadCloser) {
_ = b.Close()
}(b)
var esubs []ExtSubtitle
var subs []OpenSubtitleTrack
data, err := io.ReadAll(b)
Expand Down Expand Up @@ -414,7 +416,9 @@ func (s *Api) GetMediaProbe(ctx context.Context, u string) (*MediaProbe, error)
return nil, errors.Wrap(err, "failed to do request")
}
b := res.Body
defer b.Close()
defer func(b io.ReadCloser) {
_ = b.Close()
}(b)
mb := MediaProbe{}
data, err := io.ReadAll(b)
if err != nil {
Expand Down Expand Up @@ -534,7 +538,7 @@ func getRemoteAddress(r *http.Request) string {
return ip
}

type ApiClaimsContext struct{}
type ClaimsContext struct{}

func (s *Api) MakeClaimsFromContext(c *gin.Context) (*Claims, error) {
sess, _ := c.Cookie("session")
Expand Down Expand Up @@ -566,18 +570,17 @@ func (s *Api) MakeClaimsFromContext(c *gin.Context) (*Claims, error) {
}

func GetClaimsFromContext(c *gin.Context) *Claims {
return c.Request.Context().Value(ApiClaimsContext{}).(*Claims)
return c.Request.Context().Value(ClaimsContext{}).(*Claims)
}

func (s *Api) RegisterHandler(c *cli.Context, r *gin.Engine) error {
func (s *Api) RegisterHandler(r *gin.Engine) {
r.Use(func(c *gin.Context) {
ac, err := s.MakeClaimsFromContext(c)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), ApiClaimsContext{}, ac))
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), ClaimsContext{}, ac))
c.Next()
})
return nil
}
16 changes: 8 additions & 8 deletions services/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,21 @@ func GetUserFromContext(c *gin.Context) *User {
u.Email = *userInfo.Email
}
}
if err := c.Request.Context().Value(AuthErrorContext{}); err != nil {
if err := c.Request.Context().Value(ErrorContext{}); err != nil {
if defaultErrors.As(err.(error), &errors.TryRefreshTokenError{}) {
u.Expired = true
}
}
return u
}

type AuthErrorContext struct{}
type ErrorContext struct{}

func myVerifySession(options *sessmodels.VerifySessionOptions, otherHandler http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := session.GetSession(r, w, options)
return func(w http.ResponseWriter, r *http.Request) {
sess, err := session.GetSession(r, w, options)
if err != nil {
ctx := context.WithValue(r.Context(), AuthErrorContext{}, err)
ctx := context.WithValue(r.Context(), ErrorContext{}, err)
r := r.WithContext(ctx)
if defaultErrors.As(err, &errors.TryRefreshTokenError{}) {
if r.Header.Get("X-Requested-With") != "XMLHttpRequest" {
Expand Down Expand Up @@ -214,13 +214,13 @@ func myVerifySession(options *sessmodels.VerifySessionOptions, otherHandler http
}
return
}
if session != nil {
ctx := context.WithValue(r.Context(), sessmodels.SessionContext, session)
if sess != nil {
ctx := context.WithValue(r.Context(), sessmodels.SessionContext, sess)
otherHandler(w, r.WithContext(ctx))
} else {
otherHandler(w, r)
}
})
}
}

func verifySession(options *sessmodels.VerifySessionOptions) gin.HandlerFunc {
Expand Down
13 changes: 6 additions & 7 deletions services/claims/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/webtor-io/lazymap"

proto "github.com/webtor-io/claims-provider/proto"
auth "github.com/webtor-io/web-ui-v2/services/auth"
"github.com/webtor-io/web-ui-v2/services/auth"
)

const (
Expand Down Expand Up @@ -81,24 +81,23 @@ func (s *Claims) MakeUserClaimsFromContext(c *gin.Context) (*Data, error) {
return r, nil
}

type ClaimsContext struct{}
type Context struct{}

func GetFromContext(c *gin.Context) *Data {
if r := c.Request.Context().Value(ClaimsContext{}); r != nil {
if r := c.Request.Context().Value(Context{}); r != nil {
return r.(*Data)
}
return nil
}

func (s *Claims) RegisterHandler(c *cli.Context, r *gin.Engine) error {
func (s *Claims) RegisterHandler(r *gin.Engine) {
r.Use(func(c *gin.Context) {
r, err := s.MakeUserClaimsFromContext(c)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), ClaimsContext{}, r))
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), Context{}, r))
c.Next()
})
return nil
}
4 changes: 2 additions & 2 deletions services/claims/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package claims

import (
"fmt"
"google.golang.org/grpc/credentials/insecure"
"sync"

"github.com/urfave/cli"
proto "github.com/webtor-io/claims-provider/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

const (
Expand Down Expand Up @@ -51,7 +51,7 @@ func NewClient(c *cli.Context) *Client {
func (s *Client) Get() (proto.ClaimsProviderClient, error) {
s.once.Do(func() {
addr := fmt.Sprintf("%s:%d", s.host, s.port)
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
s.err = err
return
Expand Down
3 changes: 2 additions & 1 deletion services/embed/domain_settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package embed

import (
"github.com/pkg/errors"
"time"

"github.com/go-pg/pg/v10"
Expand Down Expand Up @@ -37,7 +38,7 @@ func (s *DomainSettings) get(domain string) (*DomainSettingsData, error) {
db := s.pg.Get()
em := &models.EmbedDomain{}
err := db.Model(em).Where("domain = ?", domain).Select()
if err == pg.ErrNoRows {
if errors.Is(err, pg.ErrNoRows) {
return &DomainSettingsData{Ads: true}, nil
} else if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit ae2fd7b

Please sign in to comment.