81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package web
|
|
|
|
import (
|
|
"github.com/gorilla/mux"
|
|
"golang.captainalm.com/GOPackageHeaderServer/conf"
|
|
"golang.captainalm.com/GOPackageHeaderServer/web/utils"
|
|
"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{
|
|
Name: zc.Name,
|
|
CSS: zc.CssURL,
|
|
OutputPage: zc.HavePageContents,
|
|
RangeSupported: zc.RangeSupported,
|
|
MetaOutput: zc.GetPackageMetaTagOutputter(),
|
|
CacheSettings: zc.CacheSettings,
|
|
}
|
|
for _, d := range zc.Domains {
|
|
ld := strings.ToLower(d)
|
|
if _, exists := pages[ld]; !exists {
|
|
pages[ld] = currentPage
|
|
router.Host(ld).HandlerFunc(currentPage.ServeHTTP)
|
|
}
|
|
}
|
|
}
|
|
router.PathPrefix("/").HandlerFunc(domainNotAllowed)
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
|
|
func domainNotAllowed(rw http.ResponseWriter, req *http.Request) {
|
|
if req.Method == http.MethodGet || req.Method == http.MethodHead {
|
|
utils.WriteResponseHeaderCanWriteBody(req.Method, rw, http.StatusNotFound, "Domain Not Allowed")
|
|
} else {
|
|
rw.Header().Set("Allow", http.MethodOptions+", "+http.MethodGet+", "+http.MethodHead)
|
|
if req.Method == http.MethodOptions {
|
|
utils.WriteResponseHeaderCanWriteBody(req.Method, rw, http.StatusOK, "")
|
|
} else {
|
|
utils.WriteResponseHeaderCanWriteBody(req.Method, rw, http.StatusMethodNotAllowed, "")
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|