49 lines
881 B
Go
49 lines
881 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"git.lumeweb.com/LumeWeb/gitea-github-proxy/config"
|
||
|
"github.com/gorilla/mux"
|
||
|
"go.uber.org/fx"
|
||
|
"go.uber.org/zap"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type ServerParams struct {
|
||
|
fx.In
|
||
|
Logger *zap.Logger
|
||
|
Router *mux.Router
|
||
|
Config *config.Config
|
||
|
}
|
||
|
|
||
|
func NewServer(lc fx.Lifecycle, params ServerParams) (*http.Server, error) {
|
||
|
srv := &http.Server{
|
||
|
Addr: fmt.Sprintf("%s:%d", params.Config.Host, params.Config.Port),
|
||
|
Handler: params.Router,
|
||
|
}
|
||
|
|
||
|
lc.Append(fx.Hook{
|
||
|
OnStart: func(ctx context.Context) error {
|
||
|
ln, err := net.Listen("tcp", srv.Addr)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
go func() {
|
||
|
err := srv.Serve(ln)
|
||
|
if err != nil {
|
||
|
params.Logger.Fatal("Failed to serve", zap.Error(err))
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return nil
|
||
|
},
|
||
|
OnStop: func(ctx context.Context) error {
|
||
|
return srv.Shutdown(ctx)
|
||
|
},
|
||
|
})
|
||
|
return srv, nil
|
||
|
}
|