From 4a0b23e6ed9e7793082865ce55dde0e4202dda2f Mon Sep 17 00:00:00 2001 From: elskow Date: Tue, 22 Oct 2024 02:16:12 +0700 Subject: [PATCH] feat: use config.yaml for configuring system config, restructure the code --- videochat-cp/Dockerfile | 7 ++-- videochat-cp/config.yaml | 5 +++ videochat-cp/config/config.go | 30 +++++++++++++++++ videochat-cp/go.mod | 17 ++++++++++ videochat-cp/go.sum | 50 +++++++++++++++++++++++++++-- videochat-cp/main.go | 29 ++++++++++------- videochat-cp/scripts/entrypoint.sh | 11 +++++++ videochat-cp/{ => server}/server.go | 11 ++++--- videochat-cp/{ => server}/webrtc.go | 4 +-- 9 files changed, 142 insertions(+), 22 deletions(-) create mode 100644 videochat-cp/config.yaml create mode 100644 videochat-cp/config/config.go create mode 100644 videochat-cp/scripts/entrypoint.sh rename videochat-cp/{ => server}/server.go (95%) rename videochat-cp/{ => server}/webrtc.go (97%) diff --git a/videochat-cp/Dockerfile b/videochat-cp/Dockerfile index a5f0478..cef9dc1 100644 --- a/videochat-cp/Dockerfile +++ b/videochat-cp/Dockerfile @@ -16,7 +16,10 @@ FROM alpine:3.18 WORKDIR /root/ COPY --from=builder /app/main . +COPY config.yaml . +COPY entrypoint.sh . -EXPOSE 3000 +RUN chmod +x entrypoint.sh -CMD ["./main"] +ENTRYPOINT ["./entrypoint.sh"] +CMD ["./main", "--config", "config.yaml"] diff --git a/videochat-cp/config.yaml b/videochat-cp/config.yaml new file mode 100644 index 0000000..4bd9ee5 --- /dev/null +++ b/videochat-cp/config.yaml @@ -0,0 +1,5 @@ +server: + address: ":3000" + stunServerURL: "stun:stun.l.google.com:19302" + cleanupInterval: "5m" + shutdownTimeout: "5s" diff --git a/videochat-cp/config/config.go b/videochat-cp/config/config.go new file mode 100644 index 0000000..717a23d --- /dev/null +++ b/videochat-cp/config/config.go @@ -0,0 +1,30 @@ +package config + +import ( + "time" + + "github.com/spf13/viper" +) + +type Config struct { + Server struct { + Address string `mapstructure:"address"` + StunServerURL string `mapstructure:"stunServerURL"` + CleanupInterval time.Duration `mapstructure:"cleanupInterval"` + ShutdownTimeout time.Duration `mapstructure:"shutdownTimeout"` + } `mapstructure:"server"` +} + +func LoadConfig(configFile string) (Config, error) { + viper.SetConfigFile(configFile) + if err := viper.ReadInConfig(); err != nil { + return Config{}, err + } + + var config Config + if err := viper.Unmarshal(&config); err != nil { + return Config{}, err + } + + return config, nil +} diff --git a/videochat-cp/go.mod b/videochat-cp/go.mod index 97e76b2..3c0888c 100644 --- a/videochat-cp/go.mod +++ b/videochat-cp/go.mod @@ -7,17 +7,23 @@ require ( github.com/gofiber/fiber/v2 v2.52.5 github.com/gofiber/websocket/v2 v2.2.1 github.com/pion/webrtc/v4 v4.0.0-beta.29 + github.com/spf13/viper v1.19.0 go.uber.org/zap v1.27.0 ) require ( github.com/andybalholm/brotli v1.1.1 // indirect github.com/fasthttp/websocket v1.5.3 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/klauspost/compress v1.17.11 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pion/datachannel v1.5.9 // indirect github.com/pion/dtls/v3 v3.0.1 // indirect github.com/pion/ice/v4 v4.0.1 // indirect @@ -34,13 +40,24 @@ require ( github.com/pion/transport/v3 v3.0.7 // indirect github.com/pion/turn/v4 v4.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.56.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect github.com/wlynxg/anet v0.0.3 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.27.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/sys v0.26.0 // indirect + golang.org/x/text v0.18.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/videochat-cp/go.sum b/videochat-cp/go.sum index 05d2a9e..e3797fd 100644 --- a/videochat-cp/go.sum +++ b/videochat-cp/go.sum @@ -1,20 +1,35 @@ github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fasthttp/websocket v1.5.3 h1:TPpQuLwJYfd4LJPXvHDYPMFWbLjsT91n3GpWtCQtdek= github.com/fasthttp/websocket v1.5.3/go.mod h1:46gg/UBmTU1kUaTcwQXpUxtRwG2PvIZYeA8oL6vF3Fs= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gofiber/contrib/fiberzap/v2 v2.1.4 h1:GCtCQnT4Cr9az4qab2Ozmqsomkxm4Ei86MfKk/1p5+0= github.com/gofiber/contrib/fiberzap/v2 v2.1.4/go.mod h1:PkdXgUzw+oj4m6ksfKJ0Hs3H7iPhwvhfI4b2LSA9hhA= github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/gofiber/websocket/v2 v2.2.1 h1:C9cjxvloojayOp9AovmpQrk8VqvVnT8Oao3+IUygH7w= github.com/gofiber/websocket/v2 v2.2.1/go.mod h1:Ao/+nyNnX5u/hIFPuHl28a+NIkrqK7PRimyKaj4JxVU= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -22,6 +37,10 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pion/datachannel v1.5.9 h1:LpIWAOYPyDrXtU+BW7X0Yt/vGtYxtXQ8ql7dFfYUVZA= github.com/pion/datachannel v1.5.9/go.mod h1:kDUuk4CU4Uxp82NH4LQZbISULkX/HtzKa4P7ldf9izE= github.com/pion/dtls/v3 v3.0.1 h1:0kmoaPYLAo0md/VemjcrAXQiSf8U+tuU3nDYVNpEKaw= @@ -54,13 +73,30 @@ github.com/pion/turn/v4 v4.0.0 h1:qxplo3Rxa9Yg1xXDxxH8xaqcyGUtbHYw4QSCvmFWvhM= github.com/pion/turn/v4 v4.0.0/go.mod h1:MuPDkm15nYSklKpN8vWJ9W2M0PlyQZqYt1McGuxG7mA= github.com/pion/webrtc/v4 v4.0.0-beta.29 h1:ahc4r88phf+Y+7YGl20gEfIYQ/eEMzNvd8KOMtxsE1s= github.com/pion/webrtc/v4 v4.0.0-beta.29/go.mod h1:z1oOHeVfz+XE9bpuXODxIDJw+/TUvENs34YGbQEdB+c= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk= github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -70,6 +106,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.56.0 h1:bEZdJev/6LCBlpdORfrLu/WOZXXxvrUQSiyniuaoW8U= @@ -88,13 +126,21 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/videochat-cp/main.go b/videochat-cp/main.go index fb3d86a..c8f037a 100644 --- a/videochat-cp/main.go +++ b/videochat-cp/main.go @@ -2,12 +2,14 @@ package main import ( "context" + "flag" "log" "os" "os/signal" "syscall" - "time" + "github.com/elskow/codepair/videochat-cp/config" + "github.com/elskow/codepair/videochat-cp/server" "github.com/gofiber/contrib/fiberzap/v2" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" @@ -17,12 +19,15 @@ import ( "go.uber.org/zap" ) -const ( - address = ":3000" - stunServerURL = "stun:stun.l.google.com:19302" -) - func main() { + configFile := flag.String("config", "config.yaml", "Path to the config file") + flag.Parse() + + config, err := config.LoadConfig(*configFile) + if err != nil { + log.Fatalf("Failed to load config: %v", err) + } + logger, err := zap.NewProduction() if err != nil { log.Fatalf("Failed to create logger: %v", err) @@ -40,28 +45,28 @@ func main() { app.Use(healthcheck.New()) app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed})) - server := NewServer(app, logger) - server.setupRoutes() + srv := server.NewServer(app, logger, config) + srv.SetupRoutes() stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt, syscall.SIGTERM) go func() { - if err := app.Listen(address); err != nil { + if err := app.Listen(config.Server.Address); err != nil { logger.Fatal("Failed to start server", zap.Error(err)) } }() - logger.Info("Server started", zap.String("address", address)) + logger.Info("Server started", zap.String("address", config.Server.Address)) <-stop logger.Info("Shutting down server...") - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), config.Server.ShutdownTimeout) defer cancel() - if err := server.Shutdown(ctx); err != nil { + if err := srv.Shutdown(ctx); err != nil { logger.Error("Server forced to shutdown", zap.Error(err)) } diff --git a/videochat-cp/scripts/entrypoint.sh b/videochat-cp/scripts/entrypoint.sh new file mode 100644 index 0000000..c16d33e --- /dev/null +++ b/videochat-cp/scripts/entrypoint.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# Read the port from the config file +PORT=$(grep 'address' config.yaml | sed 's/.*://') + +# Expose the port dynamically +echo "Exposing port $PORT" +echo "EXPOSE $PORT" > Dockerfile.dynamic + +# Run the main application +exec "$@" diff --git a/videochat-cp/server.go b/videochat-cp/server/server.go similarity index 95% rename from videochat-cp/server.go rename to videochat-cp/server/server.go index 323f4e0..c31c9fa 100644 --- a/videochat-cp/server.go +++ b/videochat-cp/server/server.go @@ -1,4 +1,4 @@ -package main +package server import ( "context" @@ -6,6 +6,7 @@ import ( "sync" "time" + "github.com/elskow/codepair/videochat-cp/config" "github.com/gofiber/fiber/v2" "github.com/gofiber/websocket/v2" "github.com/pion/webrtc/v4" @@ -22,13 +23,15 @@ type Server struct { rooms map[string]*Room roomsMutex sync.RWMutex logger *zap.Logger + config config.Config } -func NewServer(app *fiber.App, logger *zap.Logger) *Server { +func NewServer(app *fiber.App, logger *zap.Logger, config config.Config) *Server { server := &Server{ app: app, rooms: make(map[string]*Room), logger: logger, + config: config, } go server.cleanupInactiveClients() @@ -36,7 +39,7 @@ func NewServer(app *fiber.App, logger *zap.Logger) *Server { return server } -func (s *Server) setupRoutes() { +func (s *Server) SetupRoutes() { s.app.Get("/:roomID", websocket.New(s.handleWebSocket)) } @@ -120,7 +123,7 @@ func (s *Server) removeClientFromRoom(roomID string, c *websocket.Conn) { } func (s *Server) cleanupInactiveClients() { - ticker := time.NewTicker(5 * time.Minute) + ticker := time.NewTicker(s.config.Server.CleanupInterval) defer ticker.Stop() for range ticker.C { diff --git a/videochat-cp/webrtc.go b/videochat-cp/server/webrtc.go similarity index 97% rename from videochat-cp/webrtc.go rename to videochat-cp/server/webrtc.go index c0cbe5a..0b68c74 100644 --- a/videochat-cp/webrtc.go +++ b/videochat-cp/server/webrtc.go @@ -1,4 +1,4 @@ -package main +package server import ( "context" @@ -13,7 +13,7 @@ func (s *Server) createPeerConnection() (*webrtc.PeerConnection, error) { config := webrtc.Configuration{ ICEServers: []webrtc.ICEServer{ { - URLs: []string{stunServerURL}, + URLs: []string{s.config.Server.StunServerURL}, }, }, }