Finish adding listening code.

This commit is contained in:
Captain ALM 2022-07-14 18:07:07 +01:00
parent c2b22f615e
commit 659b5aa1af
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1
4 changed files with 56 additions and 2 deletions

View File

@ -4,10 +4,12 @@ import (
"fmt" "fmt"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"golang.captainalm.com/cityuni-webserver/conf" "golang.captainalm.com/cityuni-webserver/conf"
"golang.captainalm.com/cityuni-webserver/pageHandler"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"log" "log"
"net" "net"
"net/http" "net/http"
"net/http/fcgi"
"os" "os"
"os/signal" "os/signal"
"path" "path"
@ -79,13 +81,18 @@ func main() {
var fcgiListen net.Listener var fcgiListen net.Listener
switch strings.ToLower(configYml.Listen.WebMethod) { switch strings.ToLower(configYml.Listen.WebMethod) {
case "http": case "http":
webServer, _ = web.New(configYml, getListener(configYml, cwdDir)) webServer = &http.Server{
Handler: pageHandler.GetRouter(configYml),
ReadTimeout: configYml.Listen.ReadTimeout,
ReadHeaderTimeout: configYml.Listen.WriteTimeout,
}
go runBackgroundHttp(webServer, getListener(configYml, cwdDir), false)
case "fcgi": case "fcgi":
fcgiListen = getListener(configYml, cwdDir) fcgiListen = getListener(configYml, cwdDir)
if fcgiListen == nil { if fcgiListen == nil {
log.Fatalln("Listener Nil") log.Fatalln("Listener Nil")
} else { } else {
//Serve FCGI go runBackgroundFCgi(pageHandler.GetRouter(configYml), fcgiListen)
} }
default: default:
log.Fatalln("Unknown Web Method.") log.Fatalln("Unknown Web Method.")
@ -180,3 +187,30 @@ func getListener(config conf.ConfigYaml, cwd string) net.Listener {
return theListener return theListener
} }
} }
func runBackgroundHttp(s *http.Server, l net.Listener, tlsEnabled bool) {
var err error
if tlsEnabled {
err = s.ServeTLS(l, "", "")
} else {
err = s.Serve(l)
}
if err != nil {
if err == http.ErrServerClosed {
log.Println("The http server shutdown successfully")
} else {
log.Fatalf("[Http] Error trying to host the http server: %s\n", err.Error())
}
}
}
func runBackgroundFCgi(h http.Handler, l net.Listener) {
err := fcgi.Serve(l, h)
if err != nil {
if err == net.ErrClosed {
log.Println("The fcgi server shutdown successfully")
} else {
log.Fatalf("[Http] Error trying to host the fcgi server: %s\n", err.Error())
}
}
}

1
go.mod
View File

@ -3,6 +3,7 @@ module golang.captainalm.com/cityuni-webserver
go 1.18 go 1.18
require ( require (
github.com/gorilla/mux v1.8.0
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )

2
go.sum
View File

@ -1,3 +1,5 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

View File

@ -0,0 +1,17 @@
package pageHandler
import (
"github.com/gorilla/mux"
"golang.captainalm.com/cityuni-webserver/conf"
"net/http"
)
var theRouter *mux.Router
func GetRouter(config conf.ConfigYaml) http.Handler {
if theRouter == nil {
theRouter = mux.NewRouter()
//Mux routing stuff
}
return theRouter
}