Skip to content

Commit

Permalink
Merge pull request #64 from android-project-46group/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
kokoichi206 authored May 25, 2023
2 parents 9f855a9 + f38183e commit 2fcd3f7
Show file tree
Hide file tree
Showing 35 changed files with 36 additions and 39 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ DB_URL=postgresql://ubuntu:sakamichi@localhost:5432/sakamichi?sslmode=disable
server:
go run main.go


deploy: ## ラズパイ上へのデプロイ
go build -o sakamichi-api-bff main.go
mv sakamichi-api-bff /home/ubuntu/deploy/
sudo systemctl restart sakamichi-api-bff.service

migrateup:
migrate -path migrations -database "$(DB_URL)" -verbose up

Expand Down
2 changes: 1 addition & 1 deletion api/blogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
* /blogs?gn=sakurazaka&key=xxxyyyzzzhogehoge
*/
func (server *Server) getAllBlogs(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()

span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllBlogs")
defer span.Finish()

Expand Down
1 change: 0 additions & 1 deletion api/blogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

func TestGetAllBlogsAPI(t *testing.T) {

groupName := "nogizaka"
key := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Expand Down
1 change: 1 addition & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ func ErrorJson(msg string) string {
"error": msg,
},
)

return string(j)
}
5 changes: 3 additions & 2 deletions api/formations.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

func (server *Server) getAllFormations(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllBlogs")

span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllFormations")
defer span.Finish()

// debug log
Expand Down Expand Up @@ -93,5 +93,6 @@ func createFormationResponse(dbRes []db.PositionSongsBind) []FormationResponse {
}
}
}

return res
}
1 change: 0 additions & 1 deletion api/formations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

func TestGetAllFormationsAPI(t *testing.T) {

groupName := "nogizaka"
key := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Expand Down
2 changes: 1 addition & 1 deletion api/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// Handler function to get all groups.
// Must have api-key as a query parameter.
func (server *Server) getAllGroups(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()

span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllGroups")
defer span.Finish()

Expand Down
1 change: 0 additions & 1 deletion api/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

func TestGetAllGroupsAPI(t *testing.T) {

key := "valid_api_key"

testCases := []struct {
Expand Down
4 changes: 2 additions & 2 deletions api/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
* /members?gn=sakurazaka
*/
func (server *Server) getAllMembers(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllBlogs")

span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllMembers")
defer span.Finish()

// debug log
Expand Down
1 change: 0 additions & 1 deletion api/members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
)

func TestGetAllMembersAPI(t *testing.T) {

groupName := "nogizaka"
key := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Expand Down
2 changes: 2 additions & 0 deletions api/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func (server *Server) Health(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

span, ctx := tracer.StartSpanFromContext(ctx, "api.Health")
defer span.Finish()

Expand All @@ -25,6 +26,7 @@ func (server *Server) Health(w http.ResponseWriter, r *http.Request) {

func (server *Server) healthGrpc(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

span, ctx := tracer.StartSpanFromContext(ctx, "api.healthGrpc")
defer span.Finish()

Expand Down
4 changes: 2 additions & 2 deletions api/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
* /members?gn=sakurazaka
*/
func (server *Server) getPositions(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllBlogs")

span, ctx := tracer.StartSpanFromContext(ctx, "api.getPositions")
defer span.Finish()

// debug log
Expand Down
1 change: 0 additions & 1 deletion api/positions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

func TestGetPositionsAPI(t *testing.T) {

title := "nogizaka"
key := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Expand Down
2 changes: 0 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func NewServer(
config util.Config, querier db.Querier, matcher language.Matcher,
logger util.Logger, grpcClient grpc.GrpcClient,
) (*Server, error) {

server := &Server{
config: config,
querier: querier,
Expand Down Expand Up @@ -107,7 +106,6 @@ func (server *Server) setupRouter() error {

// start server depending on the cgi serve or not
func (server *Server) Start() error {

if server.config.IsCGI {
return cgi.Serve(server.router)
}
Expand Down
4 changes: 2 additions & 2 deletions api/songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

func (server *Server) getAllSongs(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllBlogs")

span, ctx := tracer.StartSpanFromContext(ctx, "api.getAllSongs")
defer span.Finish()

// debug log
Expand Down
1 change: 0 additions & 1 deletion api/songs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

func TestGetAllSongsAPI(t *testing.T) {

groupName := "nogizaka"
key := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

Expand Down
2 changes: 1 addition & 1 deletion api/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

func (server *Server) isApiKeyValid(ctx context.Context, key string) error {

if key == "" {
return errors.New("API key is empty")
}
_, err := server.querier.FindApiKeyByName(ctx, key)

return err
}
1 change: 0 additions & 1 deletion api/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

func TestIsApiKeyValid(t *testing.T) {

key := "valid_api_key"

testCases := []struct {
Expand Down
2 changes: 0 additions & 2 deletions db/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func (q *SqlQuerier) InsertApiKey(ctx context.Context, key string) error {

span, ctx := tracer.StartSpanFromContext(ctx, "db.InsertApiKey")
defer span.Finish()

Expand All @@ -27,7 +26,6 @@ func (q *SqlQuerier) InsertApiKey(ctx context.Context, key string) error {
}

func (q *SqlQuerier) FindApiKeyByName(ctx context.Context, key string) (*models.APIKey, error) {

span, ctx := tracer.StartSpanFromContext(ctx, "db.FindApiKeyByName")
defer span.Finish()

Expand Down
1 change: 1 addition & 0 deletions db/data/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ func DbInit() (*sql.DB, error) {

Ctx = context.Background()
DB, err = sql.Open(config.DBDriver, config.DBSource)

return DB, err
}
1 change: 1 addition & 0 deletions db/data/db_insert_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ func shouldInsert() bool {
if err != nil {
return false
}

return count == 0
}
1 change: 1 addition & 0 deletions db/data/find_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ func FindMemberByNameEn(nameEn string) (int, error) {
if err != nil {
return 0, err
}

return member_info.MemberID, err
}
1 change: 0 additions & 1 deletion db/data/insert_blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func InsertBlogs() {

groups := []string{"nogizaka", "sakurazaka", "hinatazaka"}
for _, group := range groups {
blogs := LoadBlogInfoFile(group)
Expand Down
1 change: 1 addition & 0 deletions db/data/insert_formation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ func MakeFormations(positions []Position) []int {
firstRowNum += 1
}
}

return []int{firstRowNum, secondRowNum, thirdRowNum}
}
2 changes: 1 addition & 1 deletion db/data/insert_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func InsertGroups() {
}

func FindGroupIdByName(group string) (int, error) {

ss, err := models.Groups(qm.Where("group_name = ?", group)).One(Ctx, DB)
if err != nil {
return 0, err
}

return ss.GroupID, nil
}
1 change: 1 addition & 0 deletions db/data/insert_member_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ func ExistMemberTag(mId int, tId int) bool {
if err != nil {
return false
}

return true
}
1 change: 1 addition & 0 deletions db/data/insert_song.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ func FindFormationIdByPositions(positions []Position) (int, error) {
if err != nil {
return 0, err
}

return ss.FormationID, nil
}
2 changes: 1 addition & 1 deletion db/data/insert_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ func GetTagIdByName(name string) (int, error) {
if err != nil {
return 0, err
}
return ss.TagID, nil

return ss.TagID, nil
}
1 change: 0 additions & 1 deletion db/data/update_blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func UpdateBlogs() {
}

func FindBlogByMemberId(id int) (*models.Blog, error) {

g, err := models.Blogs(
qm.Where("member_id = ?", id),
).One(Ctx, DB)
Expand Down
1 change: 0 additions & 1 deletion db/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type SqlQuerier struct {
}

func NewQuerier(config util.Config, logger util.Logger) (*SqlQuerier, error) {

con, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
return nil, fmt.Errorf("NewQuerier: %w", err)
Expand Down
2 changes: 0 additions & 2 deletions db/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

// Get all groups from Database.
func (q *SqlQuerier) GetAllGroups(ctx context.Context) ([]*models.Group, error) {

span, ctx := tracer.StartSpanFromContext(ctx, "db.FindGroupByName")
defer span.Finish()

Expand All @@ -27,7 +26,6 @@ func (q *SqlQuerier) GetAllGroups(ctx context.Context) ([]*models.Group, error)
}

func (q *SqlQuerier) FindGroupByName(ctx context.Context, groupName string) (*models.Group, error) {

span, ctx := tracer.StartSpanFromContext(ctx, "db.FindGroupByName")
defer span.Finish()

Expand Down
2 changes: 0 additions & 2 deletions db/locales.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func (q *SqlQuerier) InsertLocale(ctx context.Context, name string) error {

span, ctx := tracer.StartSpanFromContext(ctx, "db.InsertLocale")
defer span.Finish()

Expand All @@ -27,7 +26,6 @@ func (q *SqlQuerier) InsertLocale(ctx context.Context, name string) error {
}

func (q *SqlQuerier) FindLocaleByName(ctx context.Context, name string) (*models.Locale, error) {

span, ctx := tracer.StartSpanFromContext(ctx, "db.FindLocaleByName")
defer span.Finish()

Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"github.com/android-project-46group/api-server/db"
"github.com/android-project-46group/api-server/repository/grpc"
"github.com/android-project-46group/api-server/util"
"github.com/opentracing/opentracing-go"
)

func main() {
// tr, closer, err := util.NewJaegerTracer()
// if err != nil {
// log.Fatal("cannot initialize jaeger tracer: ", err)
// }
// defer closer.Close()
tr, closer, err := util.NewJaegerTracer()
if err != nil {
log.Fatal("cannot initialize jaeger tracer: ", err)
}
defer closer.Close()

// opentracing.SetGlobalTracer(tr)
opentracing.SetGlobalTracer(tr)

config, err := util.LoadConfig(".")
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion repository/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type grpcClient struct {
}

func NewClient(logger util.Logger, config util.Config, downloadClient pb.DownloadClient) GrpcClient {

return &grpcClient{
logger: logger,
grpcURL: config.GRPCURL,
Expand Down
1 change: 0 additions & 1 deletion repository/grpc/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

func TestHealthGrpc(t *testing.T) {

testCases := []struct {
name string
buildStubs func(mock *mockgrpc.MockDownloadClient)
Expand Down

0 comments on commit 2fcd3f7

Please sign in to comment.