2022-07-11 21:47:22 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"golang.captainalm.com/GOPackageHeaderServer/conf"
|
2022-07-12 15:41:50 +01:00
|
|
|
"golang.captainalm.com/GOPackageHeaderServer/web/utils"
|
2022-07-11 21:47:22 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(yaml conf.ConfigYaml) (*http.Server, map[string]*PageHandler) {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
var pages = make(map[string]*PageHandler)
|
|
|
|
for _, zc := range yaml.Zones {
|
|
|
|
currentPage := &PageHandler{
|
2022-07-12 15:41:50 +01:00
|
|
|
Name: zc.Name,
|
|
|
|
CSS: zc.CssURL,
|
|
|
|
OutputPage: zc.HavePageContents,
|
|
|
|
RangeSupported: zc.RangeSupported,
|
|
|
|
MetaOutput: zc.GetPackageMetaTagOutputter(),
|
|
|
|
CacheSettings: zc.CacheSettings,
|
2022-07-11 21:47:22 +01:00
|
|
|
}
|
|
|
|
for _, d := range zc.Domains {
|
|
|
|
ld := strings.ToLower(d)
|
|
|
|
if _, exists := pages[ld]; !exists {
|
|
|
|
pages[ld] = currentPage
|
|
|
|
router.Host(ld).HandlerFunc(currentPage.ServeHTTP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 12:01:53 +01:00
|
|
|
router.PathPrefix("/").HandlerFunc(domainNotAllowed)
|
2022-07-11 21:47:22 +01:00
|
|
|
if yaml.Listen.Identify {
|
|
|
|
router.Use(headerMiddleware)
|
|
|
|
}
|
|
|
|
if yaml.Listen.Web == "" {
|
|
|
|
log.Fatalf("[Http] Invalid Listening Address")
|
|
|
|
}
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: yaml.Listen.Web,
|
|
|
|
Handler: router,
|
|
|
|
ReadTimeout: yaml.Listen.GetReadTimeout(),
|
|
|
|
WriteTimeout: yaml.Listen.GetWriteTimeout(),
|
|
|
|
}
|
|
|
|
go runBackgroundHttp(s)
|
|
|
|
return s, pages
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBackgroundHttp(s *http.Server) {
|
|
|
|
err := s.ListenAndServe()
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:01:53 +01:00
|
|
|
func domainNotAllowed(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method == http.MethodGet || req.Method == http.MethodHead {
|
2022-07-12 15:41:50 +01:00
|
|
|
utils.WriteResponseHeaderCanWriteBody(req.Method, rw, http.StatusNotFound, "Domain Not Allowed")
|
2022-07-12 12:01:53 +01:00
|
|
|
} else {
|
|
|
|
rw.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead)
|
|
|
|
if req.Method == http.MethodOptions {
|
2022-07-12 15:41:50 +01:00
|
|
|
utils.WriteResponseHeaderCanWriteBody(req.Method, rw, http.StatusOK, "")
|
2022-07-12 12:01:53 +01:00
|
|
|
} else {
|
2022-07-12 15:41:50 +01:00
|
|
|
utils.WriteResponseHeaderCanWriteBody(req.Method, rw, http.StatusMethodNotAllowed, "")
|
2022-07-12 12:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:47:22 +01:00
|
|
|
func headerMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Server", "Clerie Gilbert")
|
|
|
|
w.Header().Set("X-Powered-By", "Love")
|
|
|
|
w.Header().Set("X-Friendly", "True")
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|