this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: abstract request body decoding logic

+25 -35
+3 -34
server/server.go
··· 1 1 package main 2 2 3 3 import ( 4 - "encoding/json" 5 - "io" 6 4 "net/http" 7 5 "sync" 8 6 9 7 "git.inpt.fr/churros/notella" 10 8 "git.inpt.fr/churros/notella/openapi" 11 - ll "github.com/ewen-lbh/label-logger-go" 12 9 "github.com/google/uuid" 13 10 ) 14 11 ··· 20 17 21 18 func (Server) PostSchedule(w http.ResponseWriter, r *http.Request) { 22 19 var req openapi.PostScheduleJSONRequestBody 23 - body, err := io.ReadAll(r.Body) 24 - if err != nil { 25 - ll.ErrorDisplay("could not read request body: %w", err) 26 - http.Error(w, "could not read request body", http.StatusBadRequest) 27 - } 28 - 29 - err = json.Unmarshal(body, &req) 30 - if err != nil { 31 - ll.ErrorDisplay("could not decode json", err) 32 - http.Error(w, "could not decode json", http.StatusBadRequest) 33 - return 34 - } 35 - 36 - ll.Debug("got request POST /schedule %+v", req) 20 + decodeRequest(w, r, &req) 37 21 38 22 job := notella.ScheduledJob{ 39 23 ID: uuid.New().String(), ··· 44 28 45 29 job.Schedule() 46 30 w.WriteHeader(http.StatusCreated) 47 - 48 31 } 49 32 50 33 func (Server) PostScheduleBatch(w http.ResponseWriter, r *http.Request) { 51 34 var req openapi.PostScheduleBatchJSONRequestBody 52 - body, err := io.ReadAll(r.Body) 53 - if err != nil { 54 - ll.ErrorDisplay("could not read request body: %w", err) 55 - http.Error(w, "could not read request body", http.StatusBadRequest) 56 - } 57 - 58 - err = json.Unmarshal(body, &req) 59 - if err != nil { 60 - ll.ErrorDisplay("could not decode json", err) 61 - http.Error(w, "could not decode json", http.StatusBadRequest) 62 - return 63 - } 64 - 65 - ll.Debug("got request POST /schedule/batch %+v", req) 35 + decodeRequest(w, r, &req) 66 36 67 37 var wg sync.WaitGroup 68 38 ··· 75 45 Event: schedule.Event, 76 46 } 77 47 78 - go func (job notella.ScheduledJob, wg *sync.WaitGroup) { 48 + go func(job notella.ScheduledJob, wg *sync.WaitGroup) { 79 49 job.Schedule() 80 50 wg.Done() 81 51 }(job, &wg) 82 52 } 83 53 84 54 wg.Wait() 85 - 86 55 w.WriteHeader(http.StatusCreated) 87 56 }
+22 -1
server/utils.go
··· 1 1 package main 2 2 3 - import "net/url" 3 + import ( 4 + "encoding/json" 5 + "io" 6 + "net/http" 7 + "net/url" 8 + 9 + ll "github.com/ewen-lbh/label-logger-go" 10 + ) 4 11 5 12 func redactURL(rawURL string) string { 6 13 u, err := url.Parse(rawURL) ··· 15 22 u.User = url.UserPassword(u.User.Username(), "REDACTED") 16 23 return u.String() 17 24 } 25 + 26 + func decodeRequest(w http.ResponseWriter, r *http.Request, v any) { 27 + body, err := io.ReadAll(r.Body) 28 + if err != nil { 29 + ll.ErrorDisplay("could not read request body: %w", err) 30 + http.Error(w, "could not read request body", http.StatusBadRequest) 31 + } 32 + 33 + err = json.Unmarshal(body, &v) 34 + if err != nil { 35 + ll.ErrorDisplay("could not decode json", err) 36 + http.Error(w, "could not decode json", http.StatusBadRequest) 37 + } 38 + }