merp/server.go

63 lines
1.4 KiB
Go
Raw Normal View History

2019-10-03 19:30:04 +00:00
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
package main
import "flag"
import "net/http"
import "time"
2019-10-25 12:01:38 +00:00
import "github.com/hoisie/web"
import "golang.org/x/crypto/ssh/terminal"
2019-10-03 19:30:04 +00:00
2019-10-25 12:01:38 +00:00
func hello(ctx *web.Context, val string) {
for k, v := range ctx.Params {
println(k, v)
}
}
func main() {
web.Get("/(.*)", hello)
web.Run("0.0.0.0:9999")
}
2019-10-03 19:30:04 +00:00
2019-10-25 12:01:38 +00:00
/*
2019-10-03 19:30:04 +00:00
func LoggerMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
startTime := time.Now().UTC()
h.ServeHTTP(writer, request)
log.WithFields(logrus.Fields{
"method": request.Method,
"path": request.URL.Path,
"duration_ms": time.Since(startTime).Milliseconds(),
}).Info("pageload")
})
}
func setupHttpRouter(staticdir string) http.Handler {
r := mux.NewRouter()
// This will serve files under
// http://localhost:8000/static/<filename>
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticdir))))
return r
}
func setupHttpServer() *http.Server {
var dir string
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
flag.Parse()
router := setupHttpRouter(dir)
server := alice.New(LoggerMiddleware).Then(router)
listener := &http.Server{
Handler: server,
Addr: ":8000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
return listener
}
2019-10-25 12:01:38 +00:00
*/