sample_families.go (3091B)
1 package web 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "log" 8 "net/http" 9 "time" 10 11 "git.taler.net/taler-merchant-demos/internal/backend" 12 ) 13 14 const ( 15 couponSlug = "samples-coffee-coupon" 16 passSlug = "samples-day-pass" 17 ) 18 19 type sampleFamily struct { 20 Slug string `json:"slug"` 21 Name string `json:"name"` 22 Description string `json:"description"` 23 Kind string `json:"kind"` 24 Duration backend.RelativeTime `json:"duration"` 25 Granularity backend.RelativeTime `json:"validity_granularity"` 26 StartOffset backend.RelativeTime `json:"start_offset"` 27 ValidAfter backend.Timestamp `json:"valid_after"` 28 } 29 30 // ProvisionSamples keeps the landing page available while the dedicated 31 // merchant starts up. The caller runs it in a goroutine tied to the server. 32 func (a *App) ProvisionSamples(ctx context.Context) { 33 day := backend.RelativeTime{Microseconds: (24 * time.Hour).Microseconds()} 34 midnight := backend.Timestamp{Seconds: time.Now().UTC().Truncate(24 * time.Hour).Unix()} 35 families := []sampleFamily{ 36 { 37 Slug: couponSlug, Kind: "discount", Name: "Coffee coupon", 38 Description: "A discount on one coffee.", 39 Duration: backend.RelativeTime{Microseconds: 30 * day.Microseconds}, 40 Granularity: day, ValidAfter: midnight, 41 }, 42 { 43 Slug: passSlug, Kind: "subscription", Name: "Exhibit day pass", 44 Description: "Admission until midnight UTC.", 45 Duration: day, Granularity: day, ValidAfter: midnight, 46 }, 47 } 48 for _, family := range families { 49 delay := time.Second 50 for { 51 err := ensureSampleFamily(ctx, a.opts.SamplesBackend, family) 52 if err == nil { 53 break 54 } 55 if ctx.Err() != nil { 56 return 57 } 58 log.Printf("provision merchant samples family %s: %v (retry in %s)", family.Slug, err, delay) 59 select { 60 case <-ctx.Done(): 61 return 62 case <-time.After(delay): 63 } 64 delay = min(2*delay, time.Minute) 65 } 66 } 67 a.samplesReady.Store(true) 68 } 69 70 func ensureSampleFamily(ctx context.Context, client *backend.Client, want sampleFamily) error { 71 const path = "private/tokenfamilies" 72 var got struct { 73 sampleFamily 74 ValidBefore struct { 75 Seconds json.RawMessage `json:"t_s"` 76 } `json:"valid_before"` 77 } 78 read := func() error { return client.Get(ctx, path+"/"+want.Slug, nil, &got) } 79 err := read() 80 if backendStatus(err, http.StatusNotFound) { 81 err = client.Post(ctx, path, want, nil) 82 if !backendStatus(err, http.StatusConflict) { 83 return err 84 } 85 // Another landing process may have created this slug after our lookup. 86 err = read() 87 } 88 if err != nil { 89 return err 90 } 91 if got.Kind != want.Kind || got.Duration != want.Duration || 92 got.Granularity != want.Granularity || got.StartOffset != want.StartOffset { 93 return errors.New("existing family has incompatible kind, duration, granularity or offset") 94 } 95 today := time.Now().UTC().Truncate(24 * time.Hour).Unix() 96 if got.ValidAfter.Seconds > today || string(got.ValidBefore.Seconds) != `"never"` { 97 return errors.New("existing family must cover the current validity period and be open-ended") 98 } 99 return nil 100 }