Skip to content

Commit

Permalink
It even works
Browse files Browse the repository at this point in the history
  • Loading branch information
petuhovskiy committed Jan 18, 2019
1 parent 36094c9 commit eb0fc78
Show file tree
Hide file tree
Showing 51 changed files with 289 additions and 274 deletions.
8 changes: 4 additions & 4 deletions admin/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

type Controller struct{}

func (s Controller) test(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, Message{"Congrats, you are admin!"})
func NewController() *Controller {
return &Controller{}
}

func NewController() Controller {
return Controller{}
func (s *Controller) test(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, Message{"Congrats, you are admin!"})
}
8 changes: 4 additions & 4 deletions admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/rwlist/rwcore/auth"
)

type Router *chi.Mux
type Router struct{ *chi.Mux }

func NewRouter(c Controller) Router {
func NewRouter(c *Controller, middleware *auth.Middleware) Router {
r := chi.NewRouter()
r.Use(auth.HasRole("admin"))
r.Use(middleware.HasRole("admin"))
r.Get("/test", c.test)
return r
return Router{r}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion modules/articles/impl.go → articles/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/rwlist/rwcore/app/db"
"github.com/rwlist/rwcore/app/model"
"github.com/rwlist/rwcore/modules/utils"
"github.com/rwlist/rwcore/utils"
)

type impl struct{}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions articles/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package articles

import (
"github.com/globalsign/mgo"
"github.com/rwlist/rwcore/cxt"
"net/http"
)

const (
CollName = "articles"
)

func DB(r *http.Request) Store {
return Store{
cxt.DB(r.Context()).C(CollName),
}
}

type Store struct {
*mgo.Collection
}
2 changes: 1 addition & 1 deletion auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import (
"github.com/globalsign/mgo/bson"
"github.com/rwlist/rwcore/db/users"
"github.com/rwlist/rwcore/users"
"golang.org/x/crypto/bcrypt"
)

Expand Down
2 changes: 1 addition & 1 deletion auth/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type StdClaims struct {
duration time.Duration
}

func NewStdClaims(c *JWTConfig) (*StdClaims, error) {
func NewStdClaims(c JWTConfig) (*StdClaims, error) {
duration, err := time.ParseDuration(c.Duration)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions auth/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import (
"encoding/json"
"github.com/rwlist/rwcore/db/users"
"github.com/rwlist/rwcore/users"
"github.com/rwlist/rwcore/resp"
"net/http"

Expand Down Expand Up @@ -93,8 +93,8 @@ func (c *Controller) signup(w http.ResponseWriter, r *http.Request) {
func (c *Controller) status(w http.ResponseWriter, r *http.Request) {
claims := c.middleware.ParseClaims(r)

if claims != nil {
render.Render(w, r, resp.ErrUnathorized)
if claims == nil {
render.Render(w, r, resp.ErrUnauthorized)
return
}

Expand Down
2 changes: 1 addition & 1 deletion auth/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package auth
import (
jwt "github.com/dgrijalva/jwt-go"
"github.com/google/wire"
"github.com/rwlist/rwcore/db/users"
"github.com/rwlist/rwcore/users"
)

type JWTConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type JWT struct{
signingMethod jwt.SigningMethod
}

func NewJWT(c *JWTConfig) *JWT {
func NewJWT(c JWTConfig) *JWT {
return &JWT{
secret: []byte(c.Secret),
signingMethod: jwt.GetSigningMethod(c.SigningMethod),
Expand Down
22 changes: 8 additions & 14 deletions auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package auth
import (
"context"
"errors"
"github.com/rwlist/rwcore/db/users"
"github.com/rwlist/rwcore/cxt"
"github.com/rwlist/rwcore/users"
"log"
"net/http"
"strings"
Expand All @@ -13,13 +14,6 @@ import (
"github.com/rwlist/rwcore/resp"
)

type contextKey int

const (
UserKey contextKey = iota
ClaimsKey
)

const (
BearerPrefix = "Bearer "
)
Expand Down Expand Up @@ -70,8 +64,8 @@ func (m *Middleware) UpdateContext(next http.Handler) http.Handler {

claims := m.ParseClaims(r)
if claims != nil {
ctx = context.WithValue(ctx, ClaimsKey, claims)
ctx = context.WithValue(ctx, UserKey, claims.User)
ctx = context.WithValue(ctx, cxt.ClaimsKey, claims)
ctx = context.WithValue(ctx, cxt.UserKey, claims.User)
}

next.ServeHTTP(w, r.WithContext(ctx))
Expand All @@ -82,9 +76,9 @@ func (m *Middleware) UpdateContext(next http.Handler) http.Handler {

func (m *Middleware) Authorized(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
_, ok := r.Context().Value(UserKey).(*model.User)
_, ok := r.Context().Value(cxt.UserKey).(*model.User)
if !ok {
render.Render(w, r, resp.ErrUnathorized)
render.Render(w, r, resp.ErrUnauthorized)
return
}

Expand All @@ -97,9 +91,9 @@ func (m *Middleware) Authorized(next http.Handler) http.Handler {
func (m *Middleware) HasRole(role string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(UserKey).(*model.User)
user, ok := r.Context().Value(cxt.UserKey).(*model.User)
if !ok {
render.Render(w, r, resp.ErrUnathorized)
render.Render(w, r, resp.ErrUnauthorized)
return
}

Expand Down
4 changes: 2 additions & 2 deletions auth/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"github.com/go-chi/chi"
)

type Router *chi.Mux
type Router struct { *chi.Mux }

func NewRouter(c *Controller) Router {
r := chi.NewRouter()
r.Get("/status", c.status)
r.Post("/login", c.login)
r.Post("/signup", c.signup)
return r
return Router{r}
}
8 changes: 4 additions & 4 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
config "github.com/micro/go-config"
"github.com/micro/go-config/source/file"
"github.com/rwlist/rwcore/auth"
"github.com/rwlist/rwcore/db"
"github.com/rwlist/rwcore/mod"
"github.com/rwlist/rwcore/srv"
)

type Config struct {
Server srv.Config
Mongo db.Config
JWT auth.JWTConfig
Mongo mod.Config
JWT auth.JWTConfig
}

func New(filepath string) (Config, error) {
Expand All @@ -37,7 +37,7 @@ func ProvideSrv(c Config) srv.Config {
return c.Server
}

func ProvideMongo(c Config) db.Config {
func ProvideMongo(c Config) mod.Config {
return c.Mongo
}

Expand Down
5 changes: 1 addition & 4 deletions conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ bindAddr = ":9090"
addr = "localhost"
dbName = "rwlist"

[auth.jwt]
[jwt]
secret = "sample_secret"
duration = "3000h"
signingMethod = "HS256"

[auth]
userKey = "User"
4 changes: 2 additions & 2 deletions ctx/db.go → cxt/db.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ctx
package cxt

import (
"context"
"github.com/globalsign/mgo"
)

func DB(ctx context.Context) *mgo.Database {
return ctx.Value("db").(*mgo.Database)
return ctx.Value(DbKey).(*mgo.Database)
}
11 changes: 11 additions & 0 deletions cxt/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cxt

type key int

const (
UserKey key = iota
ClaimsKey

DbKey
MgoKey
)
25 changes: 0 additions & 25 deletions db/collections.go

This file was deleted.

40 changes: 0 additions & 40 deletions db/db.go

This file was deleted.

16 changes: 0 additions & 16 deletions db/init.go

This file was deleted.

29 changes: 0 additions & 29 deletions db/middleware.go

This file was deleted.

2 changes: 1 addition & 1 deletion modules/habr/client/client.go → habr/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"time"

"github.com/rwlist/rwcore/modules/habr/models"
"github.com/rwlist/rwcore/hab"
)

type Client struct {
Expand Down
2 changes: 1 addition & 1 deletion modules/habr/client/daily.go → habr/client/daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"time"

"github.com/rwlist/rwcore/modules/habr/models"
"github.com/rwlist/rwcore/hab"
)

var (
Expand Down
File renamed without changes.
Loading

0 comments on commit eb0fc78

Please sign in to comment.