package main import ( "net/http" "encoding/json" "strings" "errors" "fmt" "io" "net/url" "strconv" "party.at/party/internal/validator" "github.com/julienschmidt/httprouter" "crypto/rsa" "crypto/x509" "encoding/pem" "crypto/rand" ) type envelope map[string]interface{} func (app *application) readString(qs url.Values, key string, defaultValue string) string { // Extract the value for a given key from the query string. If no key exists this // will return the empty string "". s := qs.Get(key) // If no key exists (or the value is empty) then return the default value. if s == "" { return defaultValue } // Otherwise return the string. return s } func (app *application) readCSV(qs url.Values, key string, defaultValue []string) []string { // Extract the value from the query string. csv := qs.Get(key) // If no key exists (or the value is empty) then return the default value. if csv == "" { return defaultValue } // Otherwise parse the value into a []string slice and return it. return strings.Split(csv, ",") } func (app *application) readInt(qs url.Values, key string, defaultValue int, v *validator.Validator) int { // Extract the value from the query string. s := qs.Get(key) // If no key exists (or the value is empty) then return the default value. if s == "" { return defaultValue } // Try to convert the value to an int. If this fails, add an error message to the // validator instance and return the default value. i, err := strconv.Atoi(s) if err != nil { v.AddError(key, "must be an integer value") return defaultValue } // Otherwise, return the converted integer value. return i } func (app *application) readIDParam(r *http.Request) (int64, error) { params := httprouter.ParamsFromContext(r.Context()) id, err := strconv.ParseInt(params.ByName("id"), 10, 64) if err != nil || id < 1 { return 0, errors.New("invalid id parameter") } return id, nil } func (app *application) writeJSON(w http.ResponseWriter, status int, data envelope, headers http.Header) error { // Encode the data to JSON, returning the error if there was one. js, err := json.MarshalIndent(data, "", "\t") if err != nil { return err } // Append a newline to make it easier to view in terminal applications. js = append(js, '\n') // At this point, we know that we won't encounter any more errors before writing the // response, so it's safe to add any headers that we want to include. We loop // through the header map and add each header to the http.ResponseWriter header map. // Note that it's OK if the provided header map is nil. Go doesn't throw an error // if you try to range over (or generally, read from) a nil map. for key, value := range headers { w.Header()[key] = value } // Add the "Content-Type: application/json" header, then write the status code and // JSON response. w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) w.Write(js) return nil } func (app *application) readJSON(w http.ResponseWriter, r *http.Request, dst interface{}) error { maxBytes := 1048576 r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes)) // Initialize the json.Decoder, and call the DisallowUnknownFields() method on it // before decoding. This means that if the JSON from the client now includes any // field which cannot be mapped to the target destination, the decoder will return // an error instead of just ignoring the field. dec := json.NewDecoder(r.Body) dec.DisallowUnknownFields() // Decode the request body to the destination. err := dec.Decode(dst) if err != nil { var syntaxError *json.SyntaxError var unmarshalTypeError *json.UnmarshalTypeError var invalidUnmarshalError *json.InvalidUnmarshalError switch { case errors.As(err, &syntaxError): return fmt.Errorf("body contains badly-formed JSON (at character %d)", syntaxError.Offset) case errors.Is(err, io.ErrUnexpectedEOF): return errors.New("body contains badly-formed JSON") case errors.As(err, &unmarshalTypeError): if unmarshalTypeError.Field != "" { return fmt.Errorf("body contains incorrect JSON type for field %q", unmarshalTypeError.Field) } return fmt.Errorf("body contains incorrect JSON type (at character %d)", unmarshalTypeError.Offset) case errors.Is(err, io.EOF): return errors.New("body must not be empty") // If the JSON contains a field which cannot be mapped to the target destination // then Decode() will now return an error message in the format "json: unknown // field """. We check for this, extract the field name from the error, // and interpolate it into our custom error message. Note that there's an open // issue at https://github.com/golang/go/issues/29035 regarding turning this // into a distinct error type in the future. case strings.HasPrefix(err.Error(), "json: unknown field "): fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ") return fmt.Errorf("body contains unknown key %s", fieldName) // If the request body exceeds 1MB in size the decode will now fail with the // error "http: request body too large". There is an open issue about turning // this into a distinct error type at https://github.com/golang/go/issues/30715. case err.Error() == "http: request body too large": return fmt.Errorf("body must not be larger than %d bytes", maxBytes) case errors.As(err, &invalidUnmarshalError): panic(err) default: return err } } // Call Decode() again, using a pointer to an empty anonymous struct as the // destination. If the request body only contained a single JSON value this will // return an io.EOF error. So if we get anything else, we know that there is // additional data in the request body and we return our own custom error message. err = dec.Decode(&struct{}{}) if err != io.EOF { return errors.New("body must only contain a single JSON value") } return nil } func (app *application) background(fn func()) { // Launch a background goroutine. go func() { // Recover any panic. defer func() { if err := recover(); err != nil { app.logger.PrintError(fmt.Errorf("%s", err), nil) } }() // Execute the arbitrary function that we passed as the parameter. fn() }() } func GenerateIssueKey() ([]byte, int, []byte, error) { key, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return nil, 0, nil, err } // Encode private key as PEM privDER := x509.MarshalPKCS1PrivateKey(key) privPEM := pem.EncodeToMemory(&pem.Block{ Type: "RSA PRIVATE KEY", Bytes: privDER, }) return key.N.Bytes(), key.E, privPEM, err }