diff options
Diffstat (limited to 'api')
| -rw-r--r-- | api/dns.go | 23 | ||||
| -rw-r--r-- | api/serve.go | 14 | ||||
| -rw-r--r-- | api/template.go | 19 |
3 files changed, 45 insertions, 11 deletions
diff --git a/api/dns.go b/api/dns.go new file mode 100644 index 0000000..3105f91 --- /dev/null +++ b/api/dns.go @@ -0,0 +1,23 @@ +package api + +import ( + "log" + "net/http" + + "git.hatecomputers.club/hatecomputers/hatecomputers.club/database" +) + +func ListDNSRecordsContinuation(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain { + return func(success Continuation, failure Continuation) ContinuationChain { + dnsRecords, err := database.GetUserDNSRecords(context.DBConn, context.User.ID) + if err != nil { + log.Println(err) + resp.WriteHeader(http.StatusInternalServerError) + return failure(context, req, resp) + } + + (*context.TemplateData)["DNSRecords"] = dnsRecords + + return success(context, req, resp) + } +} diff --git a/api/serve.go b/api/serve.go index 758a8d9..38b65b2 100644 --- a/api/serve.go +++ b/api/serve.go @@ -19,7 +19,8 @@ type RequestContext struct { Id string Start time.Time - User *database.User + TemplateData *map[string]interface{} + User *database.User } type Continuation func(*RequestContext, *http.Request, http.ResponseWriter) ContinuationChain @@ -75,12 +76,14 @@ func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server { return &RequestContext{ DBConn: dbConn, Args: argv, + + TemplateData: &map[string]interface{}{}, } } mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() - LogRequestContinuation(requestContext, r, w)(VerifySessionContinuation, FailurePassingContinuation)(IdContinuation, IdContinuation)(TemplateContinuation("home.html", nil, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + LogRequestContinuation(requestContext, r, w)(VerifySessionContinuation, FailurePassingContinuation)(IdContinuation, IdContinuation)(TemplateContinuation("home.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) mux.HandleFunc("GET /api/health", func(w http.ResponseWriter, r *http.Request) { @@ -108,10 +111,15 @@ func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server { LogRequestContinuation(requestContext, r, w)(LogoutContinuation, FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) + mux.HandleFunc("GET /dns", func(w http.ResponseWriter, r *http.Request) { + requestContext := makeRequestContext() + LogRequestContinuation(requestContext, r, w)(VerifySessionContinuation, FailurePassingContinuation)(ListDNSRecordsContinuation, GoLoginContinuation)(TemplateContinuation("dns.html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + }) + mux.HandleFunc("GET /{name}", func(w http.ResponseWriter, r *http.Request) { requestContext := makeRequestContext() name := r.PathValue("name") - LogRequestContinuation(requestContext, r, w)(VerifySessionContinuation, FailurePassingContinuation)(IdContinuation, IdContinuation)(TemplateContinuation(name+".html", nil, true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) + LogRequestContinuation(requestContext, r, w)(VerifySessionContinuation, FailurePassingContinuation)(IdContinuation, IdContinuation)(TemplateContinuation(name+".html", true), FailurePassingContinuation)(LogExecutionTimeContinuation, LogExecutionTimeContinuation)(IdContinuation, IdContinuation) }) return &http.Server{ diff --git a/api/template.go b/api/template.go index a4ccfa8..eeaeb51 100644 --- a/api/template.go +++ b/api/template.go @@ -9,7 +9,7 @@ import ( "os" ) -func renderTemplate(context *RequestContext, templateName string, showBaseHtml bool, data interface{}) (bytes.Buffer, error) { +func renderTemplate(context *RequestContext, templateName string, showBaseHtml bool) (bytes.Buffer, error) { templatePath := context.Args.TemplatePath basePath := templatePath + "/base_empty.html" if showBaseHtml { @@ -22,11 +22,14 @@ func renderTemplate(context *RequestContext, templateName string, showBaseHtml b return bytes.Buffer{}, err } - if data == nil { - data = map[string]interface{}{} + dataPtr := context.TemplateData + if dataPtr == nil { + dataPtr = &map[string]interface{}{} } - if context.User != nil { - data.(map[string]interface{})["User"] = context.User + + data := *dataPtr + if data["User"] == nil { + data["User"] = context.User } var buffer bytes.Buffer @@ -38,13 +41,13 @@ func renderTemplate(context *RequestContext, templateName string, showBaseHtml b return buffer, nil } -func TemplateContinuation(path string, data interface{}, showBase bool) Continuation { +func TemplateContinuation(path string, showBase bool) Continuation { return func(context *RequestContext, req *http.Request, resp http.ResponseWriter) ContinuationChain { return func(success Continuation, failure Continuation) ContinuationChain { - html, err := renderTemplate(context, path, true, data) + html, err := renderTemplate(context, path, true) if errors.Is(err, os.ErrNotExist) { resp.WriteHeader(404) - html, err = renderTemplate(context, "404.html", true, nil) + html, err = renderTemplate(context, "404.html", true) if err != nil { log.Println("error rendering 404 template", err) resp.WriteHeader(500) |
