Skip to content

Commit

Permalink
Support environment variables, add API k8s (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roma7-7-7 authored Mar 11, 2024
1 parent 3ed96eb commit 0573c2f
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 21 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ FROM alpine:3.19

EXPOSE 8080

VOLUME /app/config

COPY --from=build /app/bin/app /app
COPY --from=build /app/configs /app/config

WORKDIR /app

Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

VERSION=0.2.0
VERSION=0.3.0

DB_URL ?= postgres://postgres:postgres@localhost:5432/clipboard-share?sslmode=disable

Expand Down Expand Up @@ -36,5 +36,8 @@ run:
build-docker:
docker build -t clipboard-share-api:$(VERSION) .

build-docker-release:
make build-docker TAG_SUFFIX=

run-docker:
docker run -p 8080:8080 -v $(shell pwd)/configs:/app/config --name clipboard-share-api clipboard-share-api:$(VERSION)
docker run -p 8080:8080 --name clipboard-share-api clipboard-share-api:$(VERSION)
14 changes: 11 additions & 3 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ import (
"fmt"
"io"
"net/http"
"strings"
)

var port = flag.String("port", "8888", "proxy server port")
var target = flag.String("target", "http://localhost:8080", "target server")
var targetWeb = flag.String("target-web", "http://localhost:8080", "target web server")
var apiHost = flag.String("api-host", "api.clipboard-share.home", "api host")
var targetApi = flag.String("target-api", "http://localhost:8080", "target api server")

func main() {
flag.Parse()

http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
req, err := http.NewRequestWithContext(r.Context(), r.Method, *target+r.URL.Path, r.Body)
url := *targetWeb + r.URL.Path
if strings.EqualFold(r.Host, *apiHost) {
url = *targetApi + r.URL.Path
}

req, err := http.NewRequestWithContext(r.Context(), r.Method, url, r.Body)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -41,7 +49,7 @@ func main() {
_, _ = rw.Write(all)
})

fmt.Println("Proxy server is running on port", *port, "and target server is", *target)
fmt.Println("Proxy server is running on port", *port, "target web server", *targetWeb, "target api server", *targetApi)
if err := http.ListenAndServe(":"+*port, nil); err != nil {
panic(err)
}
Expand Down
9 changes: 7 additions & 2 deletions configs/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dev": true,
"port": 8080,
"cors": {
"allow_origins": ["http://localhost", "http://localhost:80", "http://localhost:3000", "http://localhost:5173", "http://clipboard-share.home"],
"allow_origins": ["http://localhost", "http://localhost:80", "http://localhost:3000", "http://localhost:5173"],
"allow_methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
"allow_headers": ["Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization", "If-Modified-Since"],
"expose_headers": ["Location"],
Expand All @@ -27,6 +27,11 @@
},
"db": {
"driver": "postgres",
"data_source": "postgres://postgres:postgres@postgres:5432/clipboard-share?sslmode=disable"
"host": "postgres",
"port": 5432,
"name": "clipboard-share",
"user": "postgres",
"password": "postgres",
"ssl_mode": "disable"
}
}
2 changes: 0 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ services:
build: .
ports:
- "8080:8080"
volumes:
- ./configs/:/app/config/
web:
build: ./web
ports:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/lib/pq v1.10.9
github.com/redis/go-redis/v9 v9.5.1
go.uber.org/zap v1.26.0
github.com/kelseyhightower/envconfig v1.4.0
golang.org/x/crypto v0.19.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/golang-jwt/jwt/v5 v5.1.0 h1:UGKbA/IPjtS6zLcdB7i5TyACMgSbOTiR8qzXgw8HW
github.com/golang-jwt/jwt/v5 v5.1.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
7 changes: 5 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type (
)

func NewApp(ctx context.Context, conf config.App, traced log.TracedLogger) (*App, error) {
traced.Infow(ctx, "Initializing SQL DB")
sqlDB, err := sql.Open(conf.DB.Driver, conf.DB.DataSource)
dbURL := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
conf.DB.Host, conf.DB.Port, conf.DB.User, conf.DB.Password, conf.DB.Name, conf.DB.SSLMode,
)
traced.Infow(ctx, "Initializing SQL DB", "host", conf.DB.Host, "port", conf.DB.Port, "name", conf.DB.Name)
sqlDB, err := sql.Open(conf.DB.Driver, dbURL)
if err != nil {
return nil, fmt.Errorf("open sql db: %w", err)
}
Expand Down
42 changes: 34 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"os"
"strings"

"github.com/kelseyhightower/envconfig"
)

type (
Expand All @@ -21,18 +23,18 @@ type (

Cookie struct {
Path string `json:"path"`
Domain string `json:"domain"`
Domain string `json:"domain" envconfig:"APP_COOKIE_DOMAIN"`
}

JWT struct {
Issuer string `json:"issuer"`
Audience []string `json:"audience"`
Audience []string `json:"audience" envconfig:"APP_JWT_AUDIENCE"`
ExpireInMinutes uint64 `json:"expire_in_minutes"`
Secret string `json:"secret"`
}

CORS struct {
AllowOrigins []string `json:"allow_origins"`
AllowOrigins []string `json:"allow_origins" envconfig:"APP_CORS_ALLOW_ORIGINS"`
AllowMethods []string `json:"allow_methods"`
AllowHeaders []string `json:"allow_headers"`
ExposeHeaders []string `json:"expose_headers"`
Expand All @@ -45,12 +47,17 @@ type (
}

DB struct {
Driver string `json:"driver"`
DataSource string `json:"data_source"`
Driver string `json:"driver"`
Host string `json:"host" envconfig:"APP_DB_HOST"`
Port int `json:"port"`
Name string `json:"name"`
User string `json:"user"`
Password string `json:"password"`
SSLMode string `json:"ssl_mode"`
}

Redis struct {
Addr string `json:"addr"`
Addr string `json:"addr" envconfig:"APP_REDIS_ADDR"`
Password string `json:"password"`
DB int `json:"db"`
TimeoutMillis int `json:"timeout_millis"`
Expand All @@ -63,6 +70,10 @@ func NewApp(confPath string) (App, error) {
return app, fmt.Errorf("read config: %w", err)
}

if err := envconfig.Process("app", &app); err != nil {
return app, fmt.Errorf("process env: %w", err)
}

return app, validateApp(app)
}

Expand Down Expand Up @@ -97,8 +108,23 @@ func validateApp(app App) error {
if app.DB.Driver == "" {
res = append(res, "empty DB driver")
}
if app.DB.DataSource == "" {
res = append(res, "empty DB data source")
if app.DB.Host == "" {
res = append(res, "empty DB host")
}
if app.DB.Port <= 0 || app.DB.Port > 65535 {
res = append(res, "invalid DB port")
}
if app.DB.Name == "" {
res = append(res, "empty DB name")
}
if app.DB.User == "" {
res = append(res, "empty DB user")
}
if app.DB.Password == "" {
res = append(res, "empty DB password")
}
if app.DB.SSLMode == "" {
res = append(res, "empty DB ssl mode")
}

if len(res) != 0 {
Expand Down
37 changes: 37 additions & 0 deletions k8s/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: v1
kind: Pod
metadata:
name: api-pod
labels:
app.kubernetes.io/name: api
spec:
containers:
- name: api
image: clipboard-share-api:0.3.0
ports:
- containerPort: 8080
name: http-api-svc
env:
- name: APP_DB_HOST
value: "host.minikube.internal"
- name: APP_CORS_ALLOW_ORIGINS
value: "http://clipboard-share.home"
- name: APP_COOKIE_DOMAIN
value: "clipboard-share.home"
- name: APP_JWT_AUDIENCE
value: "api.clipboard-share.home"
- name: APP_REDIS_ADDR
value: "host.minikube.internal:6379"
---
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app.kubernetes.io/name: api
ports:
- protocol: TCP
port: 8080
targetPort: http-api-svc
type: NodePort
3 changes: 3 additions & 0 deletions web/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ run:
build-docker:
docker build --build-arg API_HOST=$(API_HOST) -t clipboard-share-web:$(VERSION)$(TAG_SUFFIX) -f ./Dockerfile .

build-docker-release:
make build-docker TAG_SUFFIX= API_HOST=http://api.clipboard-share.home

run-docker:
docker run -p 80:80 --name clipboard-share-web clipboard-share-web:$(VERSION)$(TAG_SUFFIX)

0 comments on commit 0573c2f

Please sign in to comment.