Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #10

Open
wants to merge 10 commits into
base: test1
Choose a base branch
from
Open

test #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:latest as BUILDER
LABEL maintainer="hourunze"

# build binary
ARG USER
ARG PASS
RUN echo "machine github.com login $USER password $PASS" >/root/.netrc
RUN mkdir -p /go/src/github.com/opensourceways/message-manager
COPY . /go/src/github.com/opensourceways/message-manager
RUN cd /go/src/github.com/opensourceways/message-manager && CGO_ENABLED=1 go build -v -o ./message-manager main.go

# copy binary config and utils
FROM openeuler/openeuler:22.03
RUN dnf -y update && \
dnf in -y shadow && \
groupadd -g 1000 message-center && \
useradd -u 1000 -g message-center -s /bin/bash -m message-center

USER message-center
COPY --from=BUILDER /go/src/github.com/opensourceways/message-manager /opt/app
WORKDIR /opt/app/
ENTRYPOINT ["/opt/app/message-manager"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# OpenEuler-Message-Center-Manager
13 changes: 13 additions & 0 deletions common/cassandra/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved
*/

package cassandra

type Config struct {
Host string `json:"host" required:"true"`
User string `json:"user" required:"true"`
Pwd string `json:"pwd" required:"true"`
Name string `json:"name" required:"true"`
Port int `json:"port" required:"true"`
}
36 changes: 36 additions & 0 deletions common/cassandra/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved
*/

package cassandra

import (
"github.com/gocql/gocql"
"golang.org/x/xerrors"
)

var (
session *gocql.Session
)

func Init(cfg *Config) error {

cluster := gocql.NewCluster(cfg.Host)
cluster.Keyspace = cfg.Name
cluster.Port = cfg.Port
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: cfg.User,
Password: cfg.Pwd,
}
sessionInstance, err := cluster.CreateSession()
if err != nil {
return xerrors.Errorf("create session failed, err:%v", err)
}

session = sessionInstance
return nil
}

func Session() *gocql.Session {
return session
}
54 changes: 54 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved
*/

// Package config define config interface.
package config

type configValidate interface {
Validate() error
}

type configSetDefault interface {
SetDefault()
}

type configItems interface {
ConfigItems() []interface{}
}

// SetDefault sets default values for the provided configuration.
func SetDefault(cfg interface{}) {
if f, ok := cfg.(configSetDefault); ok {
f.SetDefault()
}

if f, ok := cfg.(configItems); ok {
items := f.ConfigItems()

for i := range items {
SetDefault(items[i])
}
}
}

// Validate performs validation for the provided configuration.
func Validate(cfg interface{}) error {
if f, ok := cfg.(configValidate); ok {
if err := f.Validate(); err != nil {
return err
}
}

if f, ok := cfg.(configItems); ok {
items := f.ConfigItems()

for i := range items {
if err := Validate(items[i]); err != nil {
return err
}
}
}

return nil
}
90 changes: 90 additions & 0 deletions common/controller/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved
*/

// Package controller the public ability of controller domain .
package controller

import (
"net/http"

"github.com/opensourceways/message-manager/common/domain/allerror"
)

const (
errorSystemError = "system_error"
errorBadRequestBody = "bad_request_body"
errorBadRequestParam = "bad_request_param"
errorUnauthorized = "unauthorized"
)

type errorCode interface {
ErrorCode() string
}

type errorNotFound interface {
errorCode

NotFound()
}

type errorNoPermission interface {
errorCode

NoPermission()
}

func httpError(err error) (int, string) {
if err == nil {
return http.StatusOK, ""
}

sc := http.StatusInternalServerError
code := errorSystemError

if v, ok := err.(errorCode); ok {
code = v.ErrorCode()

if _, ok := err.(errorNotFound); ok {
sc = http.StatusNotFound

} else if _, ok := err.(errorNoPermission); ok {
sc = http.StatusForbidden

} else {
switch code {
case allerror.ErrorCodeAccessTokenInvalid:
sc = http.StatusUnauthorized

case allerror.ErrorCodeSessionIdMissing:
sc = http.StatusUnauthorized

case allerror.ErrorCodeSessionIdInvalid:
sc = http.StatusUnauthorized

case allerror.ErrorCodeSessionNotFound:
sc = http.StatusUnauthorized

case allerror.ErrorCodeSessionInvalid:
sc = http.StatusUnauthorized

case allerror.ErrorCodeCSRFTokenMissing:
sc = http.StatusUnauthorized

case allerror.ErrorCodeCSRFTokenInvalid:
sc = http.StatusUnauthorized

case allerror.ErrorCodeCSRFTokenNotFound:
sc = http.StatusUnauthorized

case allerror.ErrorCodeAccessDenied:
sc = http.StatusUnauthorized

default:
sc = http.StatusBadRequest
}
}
}

return sc, code
}
87 changes: 87 additions & 0 deletions common/controller/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved
*/

// Package controller file scan service.
package controller

import (
"fmt"
"net/http"
"testing"

"github.com/smartystreets/goconvey/convey"

"github.com/opensourceways/message-manager/common/domain/allerror"
)

// TestControllerHttpError the unit test for the function
func TestControllerHttpError(t *testing.T) {
convey.Convey("test func httpErro, err nil", t, func() {
errcode, errString := httpError(nil)
convey.So(errcode, convey.ShouldEqual, http.StatusOK)
convey.So(errString, convey.ShouldEqual, "")
})

convey.Convey("test func httpErro, err branch test", t, func() {
err := fmt.Errorf("default err")
errcode1, errString1 := httpError(err)
convey.So(errcode1, convey.ShouldEqual, http.StatusInternalServerError)
convey.So(errString1, convey.ShouldEqual, "system_error")

newErr1 := allerror.NewNotFound(allerror.ErrorCodeModelNotFound, "model not found")
errcode2, errString2 := httpError(newErr1)
convey.So(errcode2, convey.ShouldEqual, http.StatusNotFound)
convey.So(errString2, convey.ShouldEqual, allerror.ErrorCodeModelNotFound)

newErr2 := allerror.NewNoPermission("no permission")
errcode3, errString3 := httpError(newErr2)
convey.So(errcode3, convey.ShouldEqual, http.StatusForbidden)
convey.So(errString3, convey.ShouldEqual, "no_permission")

newErr3 := allerror.New(allerror.ErrorCodeAccessTokenInvalid, "error")
errcode4, errString4 := httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeAccessTokenInvalid)

newErr3 = allerror.New(allerror.ErrorCodeSessionIdMissing, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeSessionIdMissing)

newErr3 = allerror.New(allerror.ErrorCodeSessionIdInvalid, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeSessionIdInvalid)

newErr3 = allerror.New(allerror.ErrorCodeSessionNotFound, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeSessionNotFound)

newErr3 = allerror.New(allerror.ErrorCodeSessionInvalid, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeSessionInvalid)

newErr3 = allerror.New(allerror.ErrorCodeCSRFTokenMissing, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeCSRFTokenMissing)

newErr3 = allerror.New(allerror.ErrorCodeCSRFTokenInvalid, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeCSRFTokenInvalid)

newErr3 = allerror.New(allerror.ErrorCodeCSRFTokenNotFound, "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusUnauthorized)
convey.So(errString4, convey.ShouldEqual, allerror.ErrorCodeCSRFTokenNotFound)

newErr3 = allerror.New("Default_ERR", "error")
errcode4, errString4 = httpError(newErr3)
convey.So(errcode4, convey.ShouldEqual, http.StatusBadRequest)
convey.So(errString4, convey.ShouldEqual, "Default_ERR")
})
}
Loading