Skip to content

Commit bcf750b

Browse files
committed
Add static server
1 parent 59fa325 commit bcf750b

File tree

10 files changed

+178
-30
lines changed

10 files changed

+178
-30
lines changed

assets/procat_logo_test.png

348 KB
Loading

cmd/auth-service/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414

1515
"github.com/Newella-HQ/newella-backend/internal/auth-service/config"
1616
"github.com/Newella-HQ/newella-backend/internal/auth-service/handler"
17-
"github.com/Newella-HQ/newella-backend/internal/auth-service/server"
1817
"github.com/Newella-HQ/newella-backend/internal/auth-service/service"
1918
"github.com/Newella-HQ/newella-backend/internal/auth-service/storage"
2019
sharedcfg "github.com/Newella-HQ/newella-backend/internal/config"
2120
"github.com/Newella-HQ/newella-backend/internal/logger"
21+
"github.com/Newella-HQ/newella-backend/internal/server"
2222
)
2323

2424
const (
@@ -62,7 +62,7 @@ func main() {
6262
authService := service.NewAuthService(zapLogger, authStorage, oauthCodesCache, oauthConfig, authConfig.JWTConfig.SigningKey)
6363
authHandler := handler.NewHandler(zapLogger, authService)
6464

65-
srv := server.NewAuthServiceServer(authConfig.ServerConfig.Port, authHandler.InitRoutes())
65+
srv := server.NewServiceServer(authConfig.ServerConfig.Port, authHandler.InitRoutes())
6666

6767
go func() {
6868
if err := srv.Start(); !errors.Is(err, http.ErrServerClosed) {

cmd/static-server/main.go

+59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
package main
22

3+
import (
4+
"context"
5+
"errors"
6+
"log"
7+
"net/http"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
12+
"github.com/Newella-HQ/newella-backend/internal/logger"
13+
"github.com/Newella-HQ/newella-backend/internal/server"
14+
"github.com/Newella-HQ/newella-backend/internal/static-server/config"
15+
"github.com/Newella-HQ/newella-backend/internal/static-server/handler"
16+
)
17+
318
func main() {
19+
cfg, err := config.InitStaticServerConfig()
20+
if err != nil {
21+
log.Fatalf("can't init config: %s\n", err)
22+
}
23+
24+
zapLogger, err := logger.NewZapLogger(cfg.LogLevel)
25+
if err != nil {
26+
log.Fatalf("can't init logger: %s\n", err)
27+
}
28+
defer func(zapLogger *logger.ZapLogger) {
29+
err := zapLogger.Sync()
30+
if err != nil && (!errors.Is(err, syscall.EBADF) && !errors.Is(err, syscall.ENOTTY)) {
31+
zapLogger.Errorf("can't sync logger: %s", err)
32+
}
33+
}(zapLogger)
34+
35+
serverContext, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
36+
defer cancel()
37+
38+
if err := createAssetsDirectory("./assets"); err != nil {
39+
zapLogger.Fatalf("can't create assets directory: %s", err)
40+
}
41+
42+
h := handler.NewHandler(zapLogger)
43+
44+
srv := server.NewServiceServer(cfg.ServerConfig.Port, h.InitRoutes())
45+
46+
go func() {
47+
if err := srv.Start(); !errors.Is(err, http.ErrServerClosed) {
48+
zapLogger.Fatalf("can't start server: %s", err)
49+
}
50+
}()
51+
defer func() {
52+
if err := srv.Shutdown(serverContext); err != nil {
53+
zapLogger.Errorf("can't shutdown server: %s", err)
54+
}
55+
}()
56+
57+
zapLogger.Infof("server started on port: %s", cfg.ServerConfig.Port)
58+
59+
<-serverContext.Done()
60+
}
461

62+
func createAssetsDirectory(path string) error {
63+
return os.MkdirAll(path, os.ModeDir)
564
}

docker-compose.yml

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
services:
2+
static-server:
3+
build:
4+
context: .
5+
dockerfile: ./docker/Dockerfile_StaticServer
6+
container_name: static-server
7+
restart: unless-stopped
8+
volumes:
9+
- newella_assets:/go/assets
10+
ports:
11+
- "${STATIC_SERVER_PORT}:${STATIC_SERVER_PORT}"
12+
networks:
13+
- appnet
14+
215
auth-service:
316
build:
417
context: .
@@ -35,6 +48,7 @@ services:
3548
- appnet
3649

3750
volumes:
51+
newella_assets:
3852
newella_db_tmp:
3953

4054
networks:

docker/Dockerfile_StaticServer

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1.23-alpine3.19
2+
3+
ENV GOPATH=/
4+
RUN go env -w GOCACHE=/.cache
5+
6+
COPY ./ ./
7+
8+
RUN go install github.com/maoueh/zap-pretty@latest
9+
RUN --mount=type=cache,target=/.cache go build -mod=vendor -v -o static-server ./cmd/static-server
10+
11+
ENTRYPOINT ./static-server | zap-pretty

internal/auth-service/config/config.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package config
22

33
import (
44
"fmt"
5-
"os"
65
"time"
76

87
"github.com/joho/godotenv"
@@ -34,38 +33,29 @@ func InitAuthServiceConfig() (cfg *AuthServiceConfig, err error) {
3433

3534
return &AuthServiceConfig{
3635
PostgresConfig: config.PostgresConfig{
37-
Host: GetAndValidateEnv("POSTGRES_HOST"),
38-
Port: GetAndValidateEnv("POSTGRES_PORT"),
39-
Username: GetAndValidateEnv("POSTGRES_USERNAME"),
40-
Password: GetAndValidateEnv("POSTGRES_PASSWORD"),
41-
Name: GetAndValidateEnv("POSTGRES_NAME"),
42-
SSLMode: GetAndValidateEnv("POSTGRES_SSLMODE"),
36+
Host: config.GetAndValidateEnv("POSTGRES_HOST"),
37+
Port: config.GetAndValidateEnv("POSTGRES_PORT"),
38+
Username: config.GetAndValidateEnv("POSTGRES_USERNAME"),
39+
Password: config.GetAndValidateEnv("POSTGRES_PASSWORD"),
40+
Name: config.GetAndValidateEnv("POSTGRES_NAME"),
41+
SSLMode: config.GetAndValidateEnv("POSTGRES_SSLMODE"),
4342
},
4443
ServerConfig: config.ServerConfig{
45-
Host: GetAndValidateEnv("SERVER_HOST"),
46-
Port: GetAndValidateEnv("AUTH_SERVER_PORT"),
44+
Host: config.GetAndValidateEnv("SERVER_HOST"),
45+
Port: config.GetAndValidateEnv("AUTH_SERVER_PORT"),
4746
},
4847
OAuthConfig: config.OAuthConfig{
49-
ClientID: GetAndValidateEnv("GOOGLE_CLIENT_ID"),
50-
ClientSecret: GetAndValidateEnv("GOOGLE_CLIENT_SECRET"),
48+
ClientID: config.GetAndValidateEnv("GOOGLE_CLIENT_ID"),
49+
ClientSecret: config.GetAndValidateEnv("GOOGLE_CLIENT_SECRET"),
5150
},
5251
JWTConfig: config.JWTConfig{
53-
SigningKey: GetAndValidateEnv("JWT_SIGNING_KEY"),
52+
SigningKey: config.GetAndValidateEnv("JWT_SIGNING_KEY"),
5453
},
55-
LogLevel: config.ConvertLogLevel(GetAndValidateEnv("LOG_LEVEL")),
54+
LogLevel: config.ConvertLogLevel(config.GetAndValidateEnv("LOG_LEVEL")),
5655
DatabaseTimeout: 15 * time.Second,
5756
}, nil
5857
}
5958

60-
func GetAndValidateEnv(key string) string {
61-
s := os.Getenv(key)
62-
if s == "" {
63-
panic(fmt.Sprintf("empty %s parameter", key))
64-
}
65-
66-
return s
67-
}
68-
6959
const (
7060
UserInfoEmailScope = "https://www.googleapis.com/auth/userinfo.email"
7161
UserInfoProfileScope = "https://www.googleapis.com/auth/userinfo.profile"

internal/config/config.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"os"
6+
)
47

58
type PostgresConfig struct {
69
Host string
@@ -53,3 +56,12 @@ func ConvertLogLevel(lvl string) LogLevel {
5356

5457
return Debug
5558
}
59+
60+
func GetAndValidateEnv(key string) string {
61+
s := os.Getenv(key)
62+
if s == "" {
63+
panic(fmt.Sprintf("empty %s parameter", key))
64+
}
65+
66+
return s
67+
}

internal/auth-service/server/server.go internal/server/server.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"time"
77
)
88

9-
type AuthServiceServer struct {
9+
type ServiceServer struct {
1010
s *http.Server
1111
}
1212

13-
func NewAuthServiceServer(port string, handler http.Handler) *AuthServiceServer {
14-
return &AuthServiceServer{
13+
func NewServiceServer(port string, handler http.Handler) *ServiceServer {
14+
return &ServiceServer{
1515
s: &http.Server{
1616
Handler: handler,
1717
Addr: ":" + port,
@@ -20,10 +20,10 @@ func NewAuthServiceServer(port string, handler http.Handler) *AuthServiceServer
2020
}
2121
}
2222

23-
func (s *AuthServiceServer) Start() error {
23+
func (s *ServiceServer) Start() error {
2424
return s.s.ListenAndServe()
2525
}
2626

27-
func (s *AuthServiceServer) Shutdown(ctx context.Context) error {
27+
func (s *ServiceServer) Shutdown(ctx context.Context) error {
2828
return s.s.Shutdown(ctx)
2929
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joho/godotenv"
7+
8+
"github.com/Newella-HQ/newella-backend/internal/config"
9+
)
10+
11+
type StaticServerConfig struct {
12+
ServerConfig config.ServerConfig
13+
LogLevel config.LogLevel
14+
}
15+
16+
func InitStaticServerConfig() (cfg *StaticServerConfig, err error) {
17+
defer func() {
18+
if recErr := recover(); recErr != nil {
19+
err = fmt.Errorf("can't init static server config: %s", recErr)
20+
}
21+
}()
22+
23+
if err := godotenv.Load(); err != nil {
24+
return nil, fmt.Errorf("can't get env variables: %w", err)
25+
}
26+
27+
return &StaticServerConfig{
28+
ServerConfig: config.ServerConfig{
29+
Host: config.GetAndValidateEnv("SERVER_HOST"),
30+
Port: config.GetAndValidateEnv("STATIC_SERVER_PORT"),
31+
},
32+
LogLevel: config.ConvertLogLevel(config.GetAndValidateEnv("LOG_LEVEL")),
33+
}, nil
34+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package handler
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
8+
"github.com/Newella-HQ/newella-backend/internal/logger"
9+
)
10+
11+
type Handler struct {
12+
logger logger.Logger
13+
}
14+
15+
func NewHandler(logger logger.Logger) *Handler {
16+
return &Handler{
17+
logger: logger,
18+
}
19+
}
20+
21+
func (h *Handler) InitRoutes() http.Handler {
22+
r := gin.New()
23+
r.Use(func(context *gin.Context) {
24+
h.logger.Debugf("got request for: %s", context.Request.URL.Path)
25+
})
26+
r.StaticFS("/assets", gin.Dir("./assets", false))
27+
return r
28+
}

0 commit comments

Comments
 (0)