2019-06-11 16:00:59 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package marketingweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-16 16:50:22 +01:00
|
|
|
"errors"
|
2019-06-11 16:00:59 +01:00
|
|
|
"html/template"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-06-12 14:42:39 +01:00
|
|
|
"path/filepath"
|
2019-07-10 18:12:40 +01:00
|
|
|
"strconv"
|
2019-06-11 16:00:59 +01:00
|
|
|
|
2019-06-18 02:57:04 +01:00
|
|
|
"github.com/gorilla/mux"
|
2019-06-11 16:00:59 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-06-28 15:34:10 +01:00
|
|
|
|
2020-04-16 16:50:22 +01:00
|
|
|
"storj.io/common/errs2"
|
2019-06-28 15:34:10 +01:00
|
|
|
"storj.io/storj/satellite/rewards"
|
2019-06-11 16:00:59 +01:00
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Error is satellite marketing error type.
|
2019-07-01 20:16:49 +01:00
|
|
|
var Error = errs.Class("satellite marketing error")
|
2019-06-11 16:00:59 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Config contains configuration for marketingweb server.
|
2019-06-11 16:00:59 +01:00
|
|
|
type Config struct {
|
2019-08-30 23:43:53 +01:00
|
|
|
BaseURL string `help:"base url for marketing Admin GUI" default:""`
|
2019-06-12 14:42:39 +01:00
|
|
|
Address string `help:"server address of the marketing Admin GUI" default:"127.0.0.1:8090"`
|
2019-06-11 16:00:59 +01:00
|
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
}
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Server represents marketing offersweb server.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
2019-06-11 16:00:59 +01:00
|
|
|
type Server struct {
|
2019-11-05 12:58:09 +00:00
|
|
|
log *zap.Logger
|
|
|
|
config Config
|
|
|
|
|
|
|
|
listener net.Listener
|
|
|
|
server http.Server
|
|
|
|
|
|
|
|
rewards rewards.DB
|
|
|
|
partners *rewards.PartnersService
|
|
|
|
|
2019-06-12 14:42:39 +01:00
|
|
|
templateDir string
|
2019-06-12 21:27:07 +01:00
|
|
|
templates struct {
|
|
|
|
home *template.Template
|
|
|
|
pageNotFound *template.Template
|
|
|
|
internalError *template.Template
|
2019-06-28 15:34:10 +01:00
|
|
|
badRequest *template.Template
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// commonPages returns templates that are required for all routes.
|
2019-06-12 14:42:39 +01:00
|
|
|
func (s *Server) commonPages() []string {
|
|
|
|
return []string{
|
|
|
|
filepath.Join(s.templateDir, "base.html"),
|
|
|
|
filepath.Join(s.templateDir, "index.html"),
|
|
|
|
filepath.Join(s.templateDir, "banner.html"),
|
2019-06-18 02:57:04 +01:00
|
|
|
filepath.Join(s.templateDir, "logo.html"),
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewServer creates new instance of offersweb server.
|
2019-11-05 12:58:09 +00:00
|
|
|
func NewServer(logger *zap.Logger, config Config, rewards rewards.DB, partners *rewards.PartnersService, listener net.Listener) (*Server, error) {
|
2019-06-12 14:42:39 +01:00
|
|
|
s := &Server{
|
2019-06-11 16:00:59 +01:00
|
|
|
log: logger,
|
|
|
|
config: config,
|
|
|
|
listener: listener,
|
2019-11-05 12:58:09 +00:00
|
|
|
|
|
|
|
rewards: rewards,
|
|
|
|
partners: partners,
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:31:17 +01:00
|
|
|
logger.Debug("Starting Marketing Admin UI.", zap.Stringer("Address", s.listener.Addr()))
|
2019-06-18 02:57:04 +01:00
|
|
|
fs := http.StripPrefix("/static/", http.FileServer(http.Dir(s.config.StaticDir)))
|
|
|
|
mux := mux.NewRouter()
|
2019-06-12 14:42:39 +01:00
|
|
|
if s.config.StaticDir != "" {
|
2019-06-28 15:34:10 +01:00
|
|
|
mux.HandleFunc("/", s.GetOffers)
|
2019-06-18 02:57:04 +01:00
|
|
|
mux.PathPrefix("/static/").Handler(fs)
|
2019-06-28 15:34:10 +01:00
|
|
|
mux.HandleFunc("/create/{offer_type}", s.CreateOffer)
|
2019-07-10 18:12:40 +01:00
|
|
|
mux.HandleFunc("/stop/{offer_id}", s.StopOffer)
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
2019-06-12 14:42:39 +01:00
|
|
|
s.server.Handler = mux
|
2019-06-11 16:00:59 +01:00
|
|
|
|
2019-06-12 14:42:39 +01:00
|
|
|
s.templateDir = filepath.Join(s.config.StaticDir, "pages")
|
|
|
|
|
2019-06-12 21:27:07 +01:00
|
|
|
if err := s.parseTemplates(); err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetOffers renders the tables for free credits and referral offers to the UI.
|
2019-06-28 15:34:10 +01:00
|
|
|
func (s *Server) GetOffers(w http.ResponseWriter, req *http.Request) {
|
2019-06-11 16:00:59 +01:00
|
|
|
if req.URL.Path != "/" {
|
2019-06-12 14:42:39 +01:00
|
|
|
s.serveNotFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
offers, err := s.rewards.ListAll(req.Context())
|
2019-06-12 21:27:07 +01:00
|
|
|
if err != nil {
|
2019-06-28 15:34:10 +01:00
|
|
|
s.log.Error("failed to retrieve all offers", zap.Error(err))
|
|
|
|
s.serveInternalError(w, req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
if err := s.templates.home.ExecuteTemplate(w, "base", s.OrganizeOffersByType(offers)); err != nil {
|
2019-06-12 21:27:07 +01:00
|
|
|
s.log.Error("failed to execute template", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// parseTemplates parses and stores all templates in server.
|
2019-06-12 21:27:07 +01:00
|
|
|
func (s *Server) parseTemplates() (err error) {
|
|
|
|
homeFiles := append(s.commonPages(),
|
2019-06-12 14:42:39 +01:00
|
|
|
filepath.Join(s.templateDir, "home.html"),
|
2019-06-18 02:57:04 +01:00
|
|
|
filepath.Join(s.templateDir, "referral-offers.html"),
|
|
|
|
filepath.Join(s.templateDir, "referral-offers-modal.html"),
|
|
|
|
filepath.Join(s.templateDir, "free-offers.html"),
|
|
|
|
filepath.Join(s.templateDir, "free-offers-modal.html"),
|
2019-07-12 14:52:00 +01:00
|
|
|
filepath.Join(s.templateDir, "partner-offers.html"),
|
|
|
|
filepath.Join(s.templateDir, "partner-offers-modal.html"),
|
2019-07-10 18:12:40 +01:00
|
|
|
filepath.Join(s.templateDir, "stop-free-credit.html"),
|
|
|
|
filepath.Join(s.templateDir, "stop-referral-offer.html"),
|
2019-07-25 23:06:23 +01:00
|
|
|
filepath.Join(s.templateDir, "partner-offers.html"),
|
|
|
|
filepath.Join(s.templateDir, "stop-partner-offer.html"),
|
2019-06-12 14:42:39 +01:00
|
|
|
)
|
|
|
|
|
2019-06-12 21:27:07 +01:00
|
|
|
pageNotFoundFiles := append(s.commonPages(),
|
|
|
|
filepath.Join(s.templateDir, "page-not-found.html"),
|
|
|
|
)
|
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
internalErrorFiles := append(s.commonPages(),
|
|
|
|
filepath.Join(s.templateDir, "internal-server-error.html"),
|
|
|
|
)
|
|
|
|
|
|
|
|
badRequestFiles := append(s.commonPages(),
|
|
|
|
filepath.Join(s.templateDir, "err.html"),
|
|
|
|
)
|
|
|
|
|
2019-08-30 23:43:53 +01:00
|
|
|
s.templates.home, err = template.New("home-page").Funcs(template.FuncMap{
|
|
|
|
"BaseURL": s.GetBaseURL,
|
2019-11-05 12:58:09 +00:00
|
|
|
"ReferralLink": s.generatePartnerLink,
|
2019-07-10 18:12:40 +01:00
|
|
|
}).ParseFiles(homeFiles...)
|
2019-06-12 14:42:39 +01:00
|
|
|
if err != nil {
|
2019-06-12 21:27:07 +01:00
|
|
|
return Error.Wrap(err)
|
2019-06-12 14:42:39 +01:00
|
|
|
}
|
|
|
|
|
2019-08-30 23:43:53 +01:00
|
|
|
s.templates.pageNotFound, err = template.New("page-not-found").Funcs(template.FuncMap{
|
|
|
|
"BaseURL": s.GetBaseURL,
|
|
|
|
}).ParseFiles(pageNotFoundFiles...)
|
2019-06-28 15:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2019-06-12 14:42:39 +01:00
|
|
|
|
2019-08-30 23:43:53 +01:00
|
|
|
s.templates.internalError, err = template.New("internal-server-error").Funcs(template.FuncMap{
|
|
|
|
"BaseURL": s.GetBaseURL,
|
|
|
|
}).ParseFiles(internalErrorFiles...)
|
2019-06-12 14:42:39 +01:00
|
|
|
if err != nil {
|
2019-06-12 21:27:07 +01:00
|
|
|
return Error.Wrap(err)
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
|
|
|
|
2019-08-30 23:43:53 +01:00
|
|
|
s.templates.badRequest, err = template.New("bad-request-error").Funcs(template.FuncMap{
|
|
|
|
"BaseURL": s.GetBaseURL,
|
|
|
|
}).ParseFiles(badRequestFiles...)
|
2019-06-28 15:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-06-12 21:27:07 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
func (s *Server) generatePartnerLink(offerName string) ([]string, error) {
|
|
|
|
return s.partners.GeneratePartnerLink(context.TODO(), offerName)
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
// CreateOffer handles requests to create new offers.
|
|
|
|
func (s *Server) CreateOffer(w http.ResponseWriter, req *http.Request) {
|
|
|
|
offer, err := parseOfferForm(w, req)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("failed to convert form to struct", zap.Error(err))
|
|
|
|
s.serveBadRequest(w, req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
offer.Status = rewards.Active
|
|
|
|
offerType := mux.Vars(req)["offer_type"]
|
|
|
|
|
|
|
|
switch offerType {
|
2019-08-23 19:33:21 +01:00
|
|
|
case "referral":
|
2019-06-28 15:34:10 +01:00
|
|
|
offer.Type = rewards.Referral
|
|
|
|
case "free-credit":
|
|
|
|
offer.Type = rewards.FreeCredit
|
2019-08-23 19:33:21 +01:00
|
|
|
case "partner":
|
2019-07-25 23:06:23 +01:00
|
|
|
offer.Type = rewards.Partner
|
2019-06-28 15:34:10 +01:00
|
|
|
default:
|
|
|
|
err := errs.New("response status %d : invalid offer type", http.StatusBadRequest)
|
|
|
|
s.serveBadRequest(w, req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
if _, err := s.rewards.Create(req.Context(), &offer); err != nil {
|
2019-06-28 15:34:10 +01:00
|
|
|
s.log.Error("failed to insert new offer", zap.Error(err))
|
|
|
|
s.serveBadRequest(w, req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, req, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2019-07-10 18:12:40 +01:00
|
|
|
// StopOffer expires the current offer and replaces it with the default offer.
|
|
|
|
func (s *Server) StopOffer(w http.ResponseWriter, req *http.Request) {
|
|
|
|
offerID, err := strconv.Atoi(mux.Vars(req)["offer_id"])
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("failed to parse offer id", zap.Error(err))
|
|
|
|
s.serveBadRequest(w, req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
if err := s.rewards.Finish(req.Context(), offerID); err != nil {
|
2019-07-10 18:12:40 +01:00
|
|
|
s.log.Error("failed to stop offer", zap.Error(err))
|
|
|
|
s.serveInternalError(w, req, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, req, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2019-08-30 23:43:53 +01:00
|
|
|
// GetBaseURL returns base url from config.
|
|
|
|
func (s *Server) GetBaseURL() string {
|
|
|
|
return s.config.BaseURL
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
// serveNotFound handles 404 errors and defaults to 500 if template parsing fails.
|
2019-06-12 21:27:07 +01:00
|
|
|
func (s *Server) serveNotFound(w http.ResponseWriter, req *http.Request) {
|
2019-06-12 14:42:39 +01:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2019-06-12 21:27:07 +01:00
|
|
|
err := s.templates.pageNotFound.ExecuteTemplate(w, "base", nil)
|
2019-06-11 16:00:59 +01:00
|
|
|
if err != nil {
|
2019-06-12 14:42:39 +01:00
|
|
|
s.log.Error("failed to execute template", zap.Error(err))
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
// serveInternalError handles 500 errors and renders err to the internal-server-error template.
|
|
|
|
func (s *Server) serveInternalError(w http.ResponseWriter, req *http.Request, errMsg error) {
|
2019-06-12 14:42:39 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-06-12 21:27:07 +01:00
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
if err := s.templates.internalError.ExecuteTemplate(w, "base", errMsg); err != nil {
|
|
|
|
s.log.Error("failed to execute template", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveBadRequest handles 400 errors and renders err to the bad-request template.
|
|
|
|
func (s *Server) serveBadRequest(w http.ResponseWriter, req *http.Request, errMsg error) {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
|
|
|
if err := s.templates.badRequest.ExecuteTemplate(w, "base", errMsg); err != nil {
|
2019-06-12 14:42:39 +01:00
|
|
|
s.log.Error("failed to execute template", zap.Error(err))
|
2019-06-11 16:00:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Run starts the server that host admin web app and api endpoint.
|
2019-06-11 16:00:59 +01:00
|
|
|
func (s *Server) Run(ctx context.Context) error {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
2019-08-22 12:40:15 +01:00
|
|
|
return Error.Wrap(s.server.Shutdown(context.Background()))
|
2019-06-11 16:00:59 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
2020-04-16 16:50:22 +01:00
|
|
|
err := s.server.Serve(s.listener)
|
|
|
|
if errs2.IsCanceled(err) || errors.Is(err, http.ErrServerClosed) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return Error.Wrap(err)
|
2019-06-11 16:00:59 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes server and underlying listener.
|
2019-06-11 16:00:59 +01:00
|
|
|
func (s *Server) Close() error {
|
|
|
|
return Error.Wrap(s.server.Close())
|
|
|
|
}
|