feat: linting
This commit is contained in:
@ -48,10 +48,8 @@ func (h *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", http.DetectContentType(file.Data))
|
||||
w.Write(file.Data)
|
||||
return
|
||||
} else {
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,12 +15,12 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ItemHandler struct {
|
||||
type Handler struct {
|
||||
db *gorm.DB
|
||||
key []byte
|
||||
}
|
||||
|
||||
func (h *ItemHandler) GetItem(ctx context.Context, req *connect.Request[itemv1.GetItemRequest]) (*connect.Response[itemv1.GetItemResponse], error) {
|
||||
func (h *Handler) GetItem(ctx context.Context, req *connect.Request[itemv1.GetItemRequest]) (*connect.Response[itemv1.GetItemResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -38,7 +38,7 @@ func (h *ItemHandler) GetItem(ctx context.Context, req *connect.Request[itemv1.G
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *ItemHandler) GetItems(ctx context.Context, req *connect.Request[itemv1.GetItemsRequest]) (*connect.Response[itemv1.GetItemsResponse], error) {
|
||||
func (h *Handler) GetItems(ctx context.Context, req *connect.Request[itemv1.GetItemsRequest]) (*connect.Response[itemv1.GetItemsResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -88,7 +88,7 @@ func (h *ItemHandler) GetItems(ctx context.Context, req *connect.Request[itemv1.
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *ItemHandler) CreateItem(ctx context.Context, req *connect.Request[itemv1.CreateItemRequest]) (*connect.Response[itemv1.CreateItemResponse], error) {
|
||||
func (h *Handler) CreateItem(ctx context.Context, req *connect.Request[itemv1.CreateItemRequest]) (*connect.Response[itemv1.CreateItemResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -113,7 +113,7 @@ func (h *ItemHandler) CreateItem(ctx context.Context, req *connect.Request[itemv
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *ItemHandler) UpdateItem(ctx context.Context, req *connect.Request[itemv1.UpdateItemRequest]) (*connect.Response[itemv1.UpdateItemResponse], error) {
|
||||
func (h *Handler) UpdateItem(ctx context.Context, req *connect.Request[itemv1.UpdateItemRequest]) (*connect.Response[itemv1.UpdateItemResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -143,7 +143,7 @@ func (h *ItemHandler) UpdateItem(ctx context.Context, req *connect.Request[itemv
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *ItemHandler) DeleteItem(ctx context.Context, req *connect.Request[itemv1.DeleteItemRequest]) (*connect.Response[itemv1.DeleteItemResponse], error) {
|
||||
func (h *Handler) DeleteItem(ctx context.Context, req *connect.Request[itemv1.DeleteItemRequest]) (*connect.Response[itemv1.DeleteItemResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -158,11 +158,11 @@ func (h *ItemHandler) DeleteItem(ctx context.Context, req *connect.Request[itemv
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func NewItemHandler(db *gorm.DB, key string) (string, http.Handler) {
|
||||
func NewHandler(db *gorm.DB, key string) (string, http.Handler) {
|
||||
interceptors := connect.WithInterceptors(interceptors.NewAuthInterceptor(key))
|
||||
|
||||
return itemv1connect.NewItemServiceHandler(
|
||||
&ItemHandler{
|
||||
&Handler{
|
||||
db: db,
|
||||
key: []byte(key),
|
||||
},
|
||||
|
@ -22,7 +22,7 @@ type AuthHandler struct {
|
||||
key []byte
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Login(ctx context.Context, req *connect.Request[userv1.LoginRequest]) (*connect.Response[userv1.LoginResponse], error) {
|
||||
func (h *AuthHandler) Login(_ context.Context, req *connect.Request[userv1.LoginRequest]) (*connect.Response[userv1.LoginResponse], error) {
|
||||
// Validate
|
||||
user := models.User{}
|
||||
if err := h.db.First(&user, "username = ?", req.Msg.Username).Error; err != nil {
|
||||
@ -69,7 +69,7 @@ func (h *AuthHandler) Login(ctx context.Context, req *connect.Request[userv1.Log
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *AuthHandler) SignUp(ctx context.Context, req *connect.Request[userv1.SignUpRequest]) (*connect.Response[userv1.SignUpResponse], error) {
|
||||
func (h *AuthHandler) SignUp(_ context.Context, req *connect.Request[userv1.SignUpRequest]) (*connect.Response[userv1.SignUpResponse], error) {
|
||||
// Validate
|
||||
if err := h.db.First(&models.User{}, "username = ?", req.Msg.Username).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@ -101,7 +101,7 @@ func (h *AuthHandler) SignUp(ctx context.Context, req *connect.Request[userv1.Si
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Logout(ctx context.Context, req *connect.Request[userv1.LogoutRequest]) (*connect.Response[userv1.LogoutResponse], error) {
|
||||
func (h *AuthHandler) Logout(_ context.Context, _ *connect.Request[userv1.LogoutRequest]) (*connect.Response[userv1.LogoutResponse], error) {
|
||||
// Clear cookie
|
||||
cookie := http.Cookie{
|
||||
Name: "token",
|
||||
|
@ -17,12 +17,12 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
type Handler struct {
|
||||
db *gorm.DB
|
||||
key []byte
|
||||
}
|
||||
|
||||
func (h *UserHandler) GetUser(ctx context.Context, req *connect.Request[userv1.GetUserRequest]) (*connect.Response[userv1.GetUserResponse], error) {
|
||||
func (h *Handler) GetUser(ctx context.Context, _ *connect.Request[userv1.GetUserRequest]) (*connect.Response[userv1.GetUserResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -40,7 +40,7 @@ func (h *UserHandler) GetUser(ctx context.Context, req *connect.Request[userv1.G
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *UserHandler) UpdatePassword(ctx context.Context, req *connect.Request[userv1.UpdatePasswordRequest]) (*connect.Response[userv1.UpdatePasswordResponse], error) {
|
||||
func (h *Handler) UpdatePassword(ctx context.Context, req *connect.Request[userv1.UpdatePasswordRequest]) (*connect.Response[userv1.UpdatePasswordResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -75,7 +75,7 @@ func (h *UserHandler) UpdatePassword(ctx context.Context, req *connect.Request[u
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *UserHandler) GetAPIKey(ctx context.Context, req *connect.Request[userv1.GetAPIKeyRequest]) (*connect.Response[userv1.GetAPIKeyResponse], error) {
|
||||
func (h *Handler) GetAPIKey(ctx context.Context, req *connect.Request[userv1.GetAPIKeyRequest]) (*connect.Response[userv1.GetAPIKeyResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -114,7 +114,7 @@ func (h *UserHandler) GetAPIKey(ctx context.Context, req *connect.Request[userv1
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *UserHandler) UpdateProfilePicture(ctx context.Context, req *connect.Request[userv1.UpdateProfilePictureRequest]) (*connect.Response[userv1.UpdateProfilePictureResponse], error) {
|
||||
func (h *Handler) UpdateProfilePicture(ctx context.Context, req *connect.Request[userv1.UpdateProfilePictureRequest]) (*connect.Response[userv1.UpdateProfilePictureResponse], error) {
|
||||
userid, ok := interceptors.GetUserContext(ctx)
|
||||
if !ok {
|
||||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("unauthenticated"))
|
||||
@ -169,11 +169,11 @@ func (h *UserHandler) UpdateProfilePicture(ctx context.Context, req *connect.Req
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func NewUserHandler(db *gorm.DB, key string) (string, http.Handler) {
|
||||
func NewHandler(db *gorm.DB, key string) (string, http.Handler) {
|
||||
interceptors := connect.WithInterceptors(interceptors.NewAuthInterceptor(key))
|
||||
|
||||
return userv1connect.NewUserServiceHandler(
|
||||
&UserHandler{
|
||||
&Handler{
|
||||
db: db,
|
||||
key: []byte(key),
|
||||
},
|
||||
|
@ -50,10 +50,7 @@ func WithAuthRedirect(next http.Handler, key string) http.Handler {
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
case "_app":
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
case "favicon.png":
|
||||
case "_app", "favicon.png", "icon.png":
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
default:
|
||||
@ -69,17 +66,17 @@ func WithAuthRedirect(next http.Handler, key string) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
type authInterceptor struct {
|
||||
type AuthInterceptor struct {
|
||||
key string
|
||||
}
|
||||
|
||||
func NewAuthInterceptor(key string) *authInterceptor {
|
||||
return &authInterceptor{
|
||||
func NewAuthInterceptor(key string) *AuthInterceptor {
|
||||
return &AuthInterceptor{
|
||||
key: key,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *authInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
|
||||
func (i *AuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
|
||||
// Same as previous UnaryInterceptorFunc.
|
||||
return connect.UnaryFunc(func(
|
||||
ctx context.Context,
|
||||
@ -123,7 +120,7 @@ func (i *authInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
|
||||
})
|
||||
}
|
||||
|
||||
func (*authInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
|
||||
func (*AuthInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
|
||||
return connect.StreamingClientFunc(func(
|
||||
ctx context.Context,
|
||||
spec connect.Spec,
|
||||
@ -132,7 +129,7 @@ func (*authInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) co
|
||||
})
|
||||
}
|
||||
|
||||
func (i *authInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
|
||||
func (i *AuthInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
|
||||
return connect.StreamingHandlerFunc(func(
|
||||
ctx context.Context,
|
||||
conn connect.StreamingHandlerConn,
|
||||
|
@ -15,14 +15,14 @@ type visitor struct {
|
||||
lastSeen time.Time
|
||||
}
|
||||
|
||||
type ratelimitInterceptor struct {
|
||||
type RatelimitInterceptor struct {
|
||||
key string
|
||||
visitors map[string]*visitor
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewRateLimitInterceptor(key string) *ratelimitInterceptor {
|
||||
rl := &ratelimitInterceptor{
|
||||
func NewRateLimitInterceptor(key string) *RatelimitInterceptor {
|
||||
rl := &RatelimitInterceptor{
|
||||
key: key,
|
||||
visitors: make(map[string]*visitor),
|
||||
mu: sync.Mutex{},
|
||||
@ -33,7 +33,7 @@ func NewRateLimitInterceptor(key string) *ratelimitInterceptor {
|
||||
return rl
|
||||
}
|
||||
|
||||
func (i *ratelimitInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
|
||||
func (i *RatelimitInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
|
||||
// Same as previous UnaryInterceptorFunc.
|
||||
return connect.UnaryFunc(func(
|
||||
ctx context.Context,
|
||||
@ -54,7 +54,7 @@ func (i *ratelimitInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFu
|
||||
})
|
||||
}
|
||||
|
||||
func (*ratelimitInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
|
||||
func (*RatelimitInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
|
||||
return connect.StreamingClientFunc(func(
|
||||
ctx context.Context,
|
||||
spec connect.Spec,
|
||||
@ -63,7 +63,7 @@ func (*ratelimitInterceptor) WrapStreamingClient(next connect.StreamingClientFun
|
||||
})
|
||||
}
|
||||
|
||||
func (i *ratelimitInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
|
||||
func (i *RatelimitInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
|
||||
return connect.StreamingHandlerFunc(func(
|
||||
ctx context.Context,
|
||||
conn connect.StreamingHandlerConn,
|
||||
@ -78,7 +78,7 @@ func (i *ratelimitInterceptor) WrapStreamingHandler(next connect.StreamingHandle
|
||||
})
|
||||
}
|
||||
|
||||
func (i *ratelimitInterceptor) getVisitor(userAgent string) *rate.Limiter {
|
||||
func (i *RatelimitInterceptor) getVisitor(userAgent string) *rate.Limiter {
|
||||
i.mu.Lock()
|
||||
defer i.mu.Unlock()
|
||||
|
||||
@ -97,7 +97,7 @@ func (i *ratelimitInterceptor) getVisitor(userAgent string) *rate.Limiter {
|
||||
|
||||
// Every minute check the map for visitors that haven't been seen for
|
||||
// more than 3 minutes and delete the entries.
|
||||
func (i *ratelimitInterceptor) cleanupVisitors() {
|
||||
func (i *RatelimitInterceptor) cleanupVisitors() {
|
||||
for {
|
||||
time.Sleep(time.Minute)
|
||||
|
||||
|
@ -111,8 +111,8 @@ func main() {
|
||||
// Serve GRPC Handlers
|
||||
api := http.NewServeMux()
|
||||
api.Handle(withCORS(user.NewAuthHandler(db, env.Key)))
|
||||
api.Handle(withCORS(user.NewUserHandler(db, env.Key)))
|
||||
api.Handle(withCORS(item.NewItemHandler(db, env.Key)))
|
||||
api.Handle(withCORS(user.NewHandler(db, env.Key)))
|
||||
api.Handle(withCORS(item.NewHandler(db, env.Key)))
|
||||
|
||||
// Serve web interface
|
||||
mux := http.NewServeMux()
|
||||
|
27
server/revive.toml
Normal file
27
server/revive.toml
Normal file
@ -0,0 +1,27 @@
|
||||
ignoreGeneratedHeader = false
|
||||
severity = "warning"
|
||||
confidence = 0.8
|
||||
errorCode = 0
|
||||
warningCode = 0
|
||||
|
||||
[rule.blank-imports]
|
||||
[rule.context-as-argument]
|
||||
[rule.context-keys-type]
|
||||
[rule.dot-imports]
|
||||
[rule.error-return]
|
||||
[rule.error-strings]
|
||||
[rule.error-naming]
|
||||
[rule.increment-decrement]
|
||||
[rule.var-naming]
|
||||
[rule.var-declaration]
|
||||
[rule.range]
|
||||
[rule.receiver-naming]
|
||||
[rule.time-naming]
|
||||
[rule.unexported-return]
|
||||
[rule.indent-error-flow]
|
||||
[rule.errorf]
|
||||
[rule.empty-block]
|
||||
[rule.superfluous-else]
|
||||
[rule.unused-parameter]
|
||||
[rule.unreachable-code]
|
||||
[rule.redefines-builtin-id]
|
Reference in New Issue
Block a user