|
|
@ -5,7 +5,9 @@ import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
auth "github.com/fangdingjun/go-http-auth"
|
|
|
|
auth "github.com/fangdingjun/go-http-auth"
|
|
|
|
"github.com/fangdingjun/gofast"
|
|
|
|
"github.com/fangdingjun/gofast"
|
|
|
|
|
|
|
|
loghandler "github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
@ -13,12 +15,37 @@ import (
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
//"path/filepath"
|
|
|
|
//"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type logwriter struct {
|
|
|
|
|
|
|
|
w io.Writer
|
|
|
|
|
|
|
|
l *sync.Mutex
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (lw *logwriter) Write(buf []byte) (int, error) {
|
|
|
|
|
|
|
|
lw.l.Lock()
|
|
|
|
|
|
|
|
defer lw.l.Unlock()
|
|
|
|
|
|
|
|
return lw.w.Write(buf)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func initRouters(cfg conf) {
|
|
|
|
func initRouters(cfg conf) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logout := os.Stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if logfile != "" {
|
|
|
|
|
|
|
|
fp, err := os.OpenFile(logfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Println(err)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
logout = fp
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w := &logwriter{logout, new(sync.Mutex)}
|
|
|
|
|
|
|
|
|
|
|
|
for _, l := range cfg {
|
|
|
|
for _, l := range cfg {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
router := mux.NewRouter()
|
|
|
|
domains := []string{}
|
|
|
|
domains := []string{}
|
|
|
@ -110,7 +137,7 @@ func initRouters(cfg conf) {
|
|
|
|
srv := http.Server{
|
|
|
|
srv := http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Addr: addr,
|
|
|
|
TLSConfig: tlsconfig,
|
|
|
|
TLSConfig: tlsconfig,
|
|
|
|
Handler: hdlr,
|
|
|
|
Handler: loghandler.CombinedLoggingHandler(w, hdlr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("listen https on %s", addr)
|
|
|
|
log.Printf("listen https on %s", addr)
|
|
|
|
if err := srv.ListenAndServeTLS("", ""); err != nil {
|
|
|
|
if err := srv.ListenAndServeTLS("", ""); err != nil {
|
|
|
@ -119,7 +146,10 @@ func initRouters(cfg conf) {
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.Printf("listen http on %s", addr)
|
|
|
|
log.Printf("listen http on %s", addr)
|
|
|
|
if err := http.ListenAndServe(addr, hdlr); err != nil {
|
|
|
|
if err := http.ListenAndServe(
|
|
|
|
|
|
|
|
addr,
|
|
|
|
|
|
|
|
loghandler.CombinedLoggingHandler(w, hdlr),
|
|
|
|
|
|
|
|
); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|