2021-09-28 11:47:19 +08:00
|
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"syscall"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Web struct {
|
2022-01-05 16:09:55 +08:00
|
|
|
|
Port, ReadTimeout, WriteTimeout, IdleTimeout int
|
|
|
|
|
handler http.Handler
|
|
|
|
|
function func(string) (bool, func(r *http.Request))
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
|
type Option func(web *Web)
|
|
|
|
|
|
|
|
|
|
type Callback func(request *http.Request)
|
|
|
|
|
|
|
|
|
|
func WithPort(port int) Option {
|
|
|
|
|
return func(web *Web) {
|
|
|
|
|
web.Port = port
|
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
|
func WithReadTimeout(readTimeout int) Option {
|
|
|
|
|
return func(web *Web) {
|
|
|
|
|
web.ReadTimeout = readTimeout
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithWriteTimeout(writeTimeout int) Option {
|
|
|
|
|
return func(web *Web) {
|
|
|
|
|
web.WriteTimeout = writeTimeout
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithIdleTimeout(idleTimeout int) Option {
|
|
|
|
|
return func(web *Web) {
|
|
|
|
|
web.IdleTimeout = idleTimeout
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithHandler(handler http.Handler) Option {
|
|
|
|
|
return func(web *Web) {
|
|
|
|
|
web.handler = handler
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithFunction(function func(string) (bool, func(r *http.Request))) Option {
|
|
|
|
|
return func(web *Web) {
|
|
|
|
|
web.function = function
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *Web) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2022-01-06 10:43:27 +08:00
|
|
|
|
//remoteUrl, _ := url.Parse("http://192.168.0.147:9000")
|
2022-01-05 16:09:55 +08:00
|
|
|
|
fmt.Println(r.Host)
|
2022-01-24 17:29:02 +08:00
|
|
|
|
// 获取
|
2022-01-25 11:45:53 +08:00
|
|
|
|
//ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
|
|
|
|
|
//
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// _, _ = w.Write([]byte("403: Host forbidden"))
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//remoteIP := net.ParseIP(ip)
|
|
|
|
|
//
|
|
|
|
|
//if remoteIP == nil {
|
|
|
|
|
// _, _ = w.Write([]byte("403: Host forbidden"))
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//fmt.Println(remoteIP)
|
2022-01-05 16:09:55 +08:00
|
|
|
|
|
|
|
|
|
if this.function != nil {
|
|
|
|
|
pass, callback := this.function(r.Host)
|
|
|
|
|
|
|
|
|
|
if !pass {
|
|
|
|
|
_, _ = w.Write([]byte("403: Host forbidden"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if callback != nil {
|
|
|
|
|
// 执行回调
|
|
|
|
|
callback(r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.handler.ServeHTTP(w, r)
|
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
|
|
|
|
func (this *Web) stop() {
|
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
|
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGTERM)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
err = fmt.Errorf("internal error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
s := <-c
|
|
|
|
|
defer close(c)
|
|
|
|
|
signal.Stop(c)
|
|
|
|
|
fmt.Printf("Http Server Stop:Closed - %d\n", s)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
|
func (this *Web) Run() {
|
|
|
|
|
serve := &http.Server{
|
2021-09-28 11:47:19 +08:00
|
|
|
|
Addr: fmt.Sprintf(":%d", this.Port),
|
2022-01-05 16:09:55 +08:00
|
|
|
|
Handler: this,
|
2021-09-28 11:47:19 +08:00
|
|
|
|
ReadTimeout: time.Duration(this.ReadTimeout) * time.Second,
|
|
|
|
|
WriteTimeout: time.Duration(this.WriteTimeout) * time.Second,
|
|
|
|
|
IdleTimeout: time.Duration(this.IdleTimeout) * time.Second,
|
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
|
}
|
|
|
|
|
this.stop()
|
|
|
|
|
|
|
|
|
|
fmt.Println("Http Server Start")
|
|
|
|
|
fmt.Printf("Http Server Address - %v\n", fmt.Sprintf("http://127.0.0.1:%d", this.Port))
|
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
|
_ = serve.ListenAndServe()
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 16:09:55 +08:00
|
|
|
|
func NewWeb(options ...Option) *Web {
|
|
|
|
|
out := &Web{Port: 80}
|
|
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
|
option(out)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
2022-01-05 16:09:55 +08:00
|
|
|
|
return out
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|