Skip to content

Commit

Permalink
Moved logic from main to server initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
darleet committed May 28, 2024
1 parent 069f343 commit 63fcde3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 52 deletions.
54 changes: 4 additions & 50 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
package main

import (
"context"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/darleet/blog-graphql/internal/middleware/auth"
"github.com/darleet/blog-graphql/internal/middleware/loader"
"github.com/darleet/blog-graphql/internal/middleware/logging"
"github.com/darleet/blog-graphql/internal/middleware/recoverer"
"github.com/darleet/blog-graphql/internal/ports/gql/resolver"
"github.com/darleet/blog-graphql/internal/ports/gql/runtime"
"github.com/darleet/blog-graphql/internal/repository/pg"
"github.com/darleet/blog-graphql/internal/usecase/article"
"github.com/darleet/blog-graphql/internal/usecase/comment"
"github.com/darleet/blog-graphql/internal/usecase/user"
"github.com/darleet/blog-graphql/internal/usecase/vote"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/darleet/blog-graphql/config"
"github.com/darleet/blog-graphql/internal/server"
"go.uber.org/zap"
"net/http"
"os"
)

const defaultPort = "8080"

Check failure on line 9 in cmd/server/main.go

View workflow job for this annotation

GitHub Actions / build

const `defaultPort` is unused (unused)
Expand All @@ -33,42 +17,12 @@ func main() {
}
}(log)

port := os.Getenv("SERVER_PORT")
if port == "" {
port = defaultPort
}

pool, err := pgxpool.New(context.Background(), os.Getenv("PG_URL"))
conf, err := config.NewConfig(".env")
if err != nil {
log.Fatal(err)
}

err = pool.Ping(context.Background())
err = server.NewServer(log, conf).Start()
if err != nil {
log.Fatal(err)
}

repo := pg.NewRepository(pool)

articles := article.NewUsecase(repo)
comments := comment.NewUsecase(repo)
votes := vote.NewUsecase(repo)
users := user.NewUsecase()

res := resolver.NewRootResolvers(log, articles, comments, users, votes)
srv := handler.NewDefaultServer(runtime.NewExecutableSchema(res))

srv.AddTransport(&transport.Websocket{})

srv.Use(extension.FixedComplexityLimit(300))

recMW := recoverer.Middleware(log)
authMW := auth.Middleware()
logMW := logging.Middleware(log)

http.Handle("/query", loader.Middleware(repo, authMW(logMW(recMW(srv)))))

log.Info("Server started on http://localhost:%s/", port)
log.Fatal(http.ListenAndServe(":"+port, nil))

}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package config

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ func (s *Server) Start() error {
}

http.Handle("/query", h)
s.log.Info("Server started on http://%s:%s/", s.config.Host, s.config.Port)
s.log.Infof("Server started on http://%s:%s/", s.config.Host, s.config.Port)
return http.ListenAndServe(s.config.Host+":"+s.config.Port, nil)
}

0 comments on commit 63fcde3

Please sign in to comment.