Rename server options struct and init func

This commit is contained in:
Evan Fiordeliso 2023-08-14 18:46:16 -04:00
parent 1a4f6ebaf2
commit 496f246a2d
4 changed files with 34 additions and 11 deletions

View File

@ -0,0 +1,23 @@
name: Release
on:
push:
tags:
- "*"
jobs:
ytdl-web:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
- uses: github.com/docker/setup-qemu-action@v2
- uses: github.com/docker/setup-buildx-action@v2
- uses: github.com/goreleaser/goreleaser-action@v2
with:
version: latest
args: release --snapshot --clean
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}

View File

@ -46,7 +46,7 @@ thousand websites using the yt-dlp project under the hood.`,
ytdl := ytdl.NewYtdl(cfg, slog.Default(), cache)
s := server.New(
server.DefaultServerOptions().
server.DefaultOptions().
WithListenAddr(viper.GetString("http.listen")).
WithListenPort(viper.GetInt("http.port")).
WithLogger(logger.With("module", "server")),

View File

@ -2,31 +2,31 @@ package server
import "golang.org/x/exp/slog"
type ServerOptions struct {
type Options struct {
ListenAddr string
ListenPort int
Logger *slog.Logger
}
func DefaultServerOptions() *ServerOptions {
return &ServerOptions{
func DefaultOptions() *Options {
return &Options{
ListenAddr: "127.0.0.1",
ListenPort: 8080,
Logger: slog.Default(),
}
}
func (o *ServerOptions) WithListenAddr(addr string) *ServerOptions {
func (o *Options) WithListenAddr(addr string) *Options {
o.ListenAddr = addr
return o
}
func (o *ServerOptions) WithListenPort(port int) *ServerOptions {
func (o *Options) WithListenPort(port int) *Options {
o.ListenPort = port
return o
}
func (o *ServerOptions) WithLogger(logger *slog.Logger) *ServerOptions {
func (o *Options) WithLogger(logger *slog.Logger) *Options {
o.Logger = logger
return o
}

View File

@ -17,17 +17,17 @@ type Server interface {
type DefaultServer struct {
r chi.Router
opts *ServerOptions
opts *Options
}
var _ Server = (*DefaultServer)(nil)
func New(options ...*ServerOptions) *DefaultServer {
var opts *ServerOptions
func New(options ...*Options) *DefaultServer {
var opts *Options
if len(options) > 0 {
opts = options[0]
} else {
opts = DefaultServerOptions()
opts = DefaultOptions()
}
r := chi.NewRouter()