package api import ( "database/sql" "fmt" "log" "net/http" "time" "git.simponic.xyz/simponic/phoneassistant/api/template" "git.simponic.xyz/simponic/phoneassistant/api/types" "git.simponic.xyz/simponic/phoneassistant/args" "git.simponic.xyz/simponic/phoneassistant/utils" ) func LogRequestContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain { context.Start = time.Now() context.Id = utils.RandomId() log.Println(req.Method, req.URL.Path, req.RemoteAddr, context.Id) return success(context, req, resp) } } func LogExecutionTimeContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain { end := time.Now() log.Println(context.Id, "took", end.Sub(context.Start)) return success(context, req, resp) } } func HealthCheckContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain { resp.WriteHeader(200) resp.Write([]byte("healthy")) return success(context, req, resp) } } func FailurePassingContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { return func(_success types.Continuation, failure types.Continuation) types.ContinuationChain { return failure(context, req, resp) } } func IdContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain { return success(context, req, resp) } } func CacheControlMiddleware(next http.Handler, maxAge int) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { header := fmt.Sprintf("public, max-age=%d", maxAge) w.Header().Set("Cache-Control", header) next.ServeHTTP(w, r) }) } func MakeMux(argv *args.Arguments, dbConn *sql.DB) *http.ServeMux { mux := http.NewServeMux() staticFileServer := http.FileServer(http.Dir(argv.StaticPath)) mux.Handle("GET /static/", http.StripPrefix("/static/", CacheControlMiddleware(staticFileServer, 3600))) makeRequestContext := func() *types.RequestContext { return &types.RequestContext{ DBConn: dbConn, Args: argv, TemplateData: &map[string]interface{}{}, } } mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() LogRequestContinuation(requestContext, r, w)(HealthCheckContinuation, FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() (*requestContext.TemplateData)["Service"] = "phoneassistant" templateFile := "hello.html" LogRequestContinuation(requestContext, r, w)(template.TemplateContinuation(templateFile, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) return mux }