예제 #1
0
func CreateTicket(ctx *auth.AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
	// developer can call:
	// user, err := ctx.ValidCurrentUser(false, nil)
	// to get the current logged user's infomation
	t := Ticket{}
	err := json.NewDecoder(req.Body).Decode(&t)
	req.Body.Close()
	if err != nil {
		return http.StatusBadRequest, err
	}

	t.Id = bson.NewObjectId()

	// save stuff to database
	db, ok := ctx.Value(DBKey).(*mgo.Database)
	if !ok {
		ctx.Logs.Errorf("Cannot access database")
		return http.StatusInternalServerError, errors.New("Cannot access database")
	}

	err = db.C("tickets").Insert(&t)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	json.NewEncoder(rw).Encode(&t)
	return http.StatusOK, nil
}
예제 #2
0
func GetTicket(ctx *auth.AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
	sid := mux.Vars(req)["ticket_id"]
	if !bson.IsObjectIdHex(sid) {
		return http.StatusBadRequest, errors.New("Invalid id")
	}

	db, ok := ctx.Value(DBKey).(*mgo.Database)
	if !ok {
		ctx.Logs.Errorf("Cannot access database")
		return http.StatusInternalServerError, errors.New("Cannot access database")
	}

	t := Ticket{}
	err := db.C("tickets").FindId(bson.ObjectIdHex(sid)).One(&t)
	if err != nil {
		if err == mgo.ErrNotFound {
			return http.StatusNotFound, err
		}

		return http.StatusInternalServerError, err
	}

	json.NewEncoder(rw).Encode(&t)
	return http.StatusOK, nil
}
예제 #3
0
func (h mongoMngrHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	cloneDB := h.db.Session.Clone().DB(h.db.Name)
	defer cloneDB.Session.Close()

	ctx := auth.AuthContext{}
	ctx.Auth = NewMgoManager(cloneDB)
	ctx.Settings = NewMgoConfigMngr(cloneDB)
	auth.BasicMngrHandler(&ctx, rw, req, &h.cond, h.fn)
}
예제 #4
0
func (n *SESNotificator) SendMail(ctx *auth.AuthContext, subject, message, from, to string) error {
	// Setup headers
	headers := make(map[string]string)
	headers["From"] = from
	headers["To"] = to
	headers["Subject"] = subject

	// Setup message body
	body := ""
	for k, v := range headers {
		body += fmt.Sprintf("%s: %s\r\n", k, v)
	}
	body += "\r\n" + message

	// Connect to the SMTP Server
	servername := n.Server + ":" + strconv.Itoa(n.Port)

	auth := smtp.PlainAuth("", n.User, n.Password, n.Server)

	// TLS config
	tlsconfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         n.Server,
	}

	// Here is the key, you need to call tls.Dial instead of smtp.Dial
	// for smtp servers running on 465 that require an ssl connection
	// from the very beginning (no starttls)
	conn, err := tls.Dial("tcp", servername, tlsconfig)
	if err != nil {
		return err
	}

	send := func() error {
		c, err := smtp.NewClient(conn, n.Server)
		if err != nil {
			return err
		}

		if err = c.Auth(auth); err != nil {
			return err
		}

		if err = c.Mail(from); err != nil {
			return err
		}

		if err = c.Rcpt(to); err != nil {
			return err
		}

		w, err := c.Data()
		if err != nil {
			return err
		}

		_, err = w.Write([]byte(body))
		if err != nil {
			return err
		}

		err = w.Close()
		if err != nil {
			return err
		}

		return c.Quit()
	}

	c := make(chan error, 1)
	go func() {
		c <- send()
	}()

	select {
	case <-ctx.Context.Done():
		conn.Close()
		<-c // Wait for send to return.
		return ctx.Err()
	case err := <-c:
		return err
	}
}