Skip to content

Commit

Permalink
Merge pull request #10 from MuShare/change/config
Browse files Browse the repository at this point in the history
Change(config): remove baseURL from server config
  • Loading branch information
leeif committed Sep 10, 2019
2 parents 59a7ee7 + c7ec4dd commit 12236eb
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 69 deletions.
1 change: 1 addition & 0 deletions .chglog/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ options:
- Update
- Fix
- Feature
- Change
commit_groups:
# title_maps:
# feat: Features
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ADD . /go/src/pluto

WORKDIR /go/src/pluto

RUN export GO111MODULE=on && go build -ldflags="-X 'main.VERSION=${VERSION}'" -o pluto-server cmd/pluto-server/main.go
RUN export GO111MODULE=on GOPROXY=https://proxy.golang.org && go build -ldflags="-X 'main.VERSION=${VERSION}'" -o pluto-server cmd/pluto-server/main.go

FROM ubuntu:18.04

Expand Down
3 changes: 1 addition & 2 deletions config/config_server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package config

type ServerConfig struct {
BaseURL *string `kiper_value:"name:base_url;help:base url;default:http://localhost:8010"`
Port *Port `kiper_value:"name:port;help:server listen port;default:8010"`
Port *Port `kiper_value:"name:port;help:server listen port;default:8010"`
}

type Port struct {
Expand Down
2 changes: 1 addition & 1 deletion integration/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
ports:
- 8010:8010
volumes:
- "./:/etc/pluto"
- ./:/etc/pluto
restart: always
command: dockerize -wait tcp://db:3306 -timeout 60s /usr/bin/pluto-server --config.file=/etc/pluto/config_test.json

Expand Down
40 changes: 34 additions & 6 deletions integration/intergration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package main

import (
"errors"
"fmt"
"log"
"net/http"
"os/exec"
"time"

"github.com/leeif/pluto/config"
"github.com/leeif/pluto/utils/rsa"
)

type testCase struct {
Expand All @@ -14,6 +18,10 @@ type testCase struct {
}

var testCases = []testCase{
{
Name: "initRSA",
Func: initRSA,
},
{
Name: "testHealthCheck",
Func: testHealthCheck,
Expand Down Expand Up @@ -74,8 +82,8 @@ var testCases = []testCase{

func testHealthCheck() error {
url := "http://localhost:8010/healthcheck"
for i := 0; i < 500; i++ {
log.Printf("try count: %v\n", i)
for i := 0; i < 100; i++ {
log.Printf("retry count: %v\n", i)
resp, err := http.Get(url)
time.Sleep(time.Duration(5) * time.Second)
if err != nil {
Expand All @@ -88,20 +96,40 @@ func testHealthCheck() error {
return errors.New("Healthcheck failed")
}

func initRSA() error {
cfg := config.Config{}
cfg.RSA = &config.RSAConfig{}
name := "ids_rsa_test"
cfg.RSA.Name = &name
path := "./docker"
cfg.RSA.Path = &path
if err := rsa.Init(&cfg); err != nil {
return fmt.Errorf("Expect no error, but %v", err)
}
return nil
}

func main() {
log.Println("docker-compose -f docker/docker-compose.yml up -d")
cmd := exec.Command("docker-compose", "-f", "docker/docker-compose.yml", "up", "-d")
cmd.Start()
time.Sleep(time.Duration(10) * time.Second)
err := cmd.Run()
if err != nil {
log.Panicf("Error: %v", err)
}
time.Sleep(time.Duration(5) * time.Second)
defer func() {
time.Sleep(time.Duration(5) * time.Second)
log.Println("docker-compose -f docker/docker-compose.yml down --rmi all")
cmd := exec.Command("docker-compose", "-f", "docker/docker-compose.yml", "down", "--rmi", "all")
cmd.Start()
time.Sleep(time.Duration(10) * time.Second)
err := cmd.Run()
if err != nil {
log.Printf("Error: %v", err)
}
}()
for _, tc := range testCases {
log.Printf("====== start %v ======\n", tc.Name)
err := tc.Func()
time.Sleep(time.Duration(1) * time.Second)
if err != nil {
log.Panicf("Error: %v", err)
}
Expand Down
12 changes: 0 additions & 12 deletions integration/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
"net/http"
"strings"

"github.com/leeif/pluto/config"

"github.com/leeif/pluto/utils/jwt"
"github.com/leeif/pluto/utils/rsa"

"github.com/leeif/pluto/datatype/request"
)
Expand Down Expand Up @@ -123,15 +120,6 @@ func testMailRegisterOK() error {
}

func testRegisterVerifyOK() error {
cfg := config.Config{}
cfg.RSA = &config.RSAConfig{}
name := "ids_rsa_test"
cfg.RSA.Name = &name
path := "./docker"
cfg.RSA.Path = &path
if err := rsa.Init(&cfg); err != nil {
return fmt.Errorf("Expect no error, but %v", err)
}
token, perror := jwt.GenerateJWT(jwt.Head{Type: jwt.REGISTERVERIFY, Alg: jwt.ALGRAS},
&jwt.RegisterVerifyPayload{UserID: 1}, 60*60)

Expand Down
11 changes: 0 additions & 11 deletions integration/reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,12 @@ import (
"fmt"
"net/http"

"github.com/leeif/pluto/config"
"github.com/leeif/pluto/utils/jwt"
"github.com/leeif/pluto/utils/rsa"

"github.com/leeif/pluto/datatype/request"
)

func testResetPassword() error {
cfg := config.Config{}
cfg.RSA = &config.RSAConfig{}
name := "ids_rsa_test"
cfg.RSA.Name = &name
path := "./docker"
cfg.RSA.Path = &path
if err := rsa.Init(&cfg); err != nil {
return fmt.Errorf("Expect no error, but %v", err)
}
token, perror := jwt.GenerateJWT(jwt.Head{Type: jwt.PASSWORDRESET, Alg: jwt.ALGRAS},
&jwt.PasswordResetPayload{Mail: "test@gmail.com"}, 60*60)

Expand Down
31 changes: 9 additions & 22 deletions integration/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"net/http"

"github.com/leeif/pluto/config"
"github.com/leeif/pluto/utils/jwt"
"github.com/leeif/pluto/utils/rsa"
)

func testPasswordResetResultFail() error {
Expand All @@ -26,23 +24,15 @@ func testPasswordResetResultFail() error {
}

func testPasswordResetResultOK() error {
cfg := config.Config{}
cfg.RSA = &config.RSAConfig{}
name := "ids_rsa_test"
cfg.RSA.Name = &name
path := "./docker"
cfg.RSA.Path = &path
if err := rsa.Init(&cfg); err != nil {
return fmt.Errorf("Expect no error, but %v", err)
}
token, perror := jwt.GenerateJWT(jwt.Head{Type: jwt.PASSWORDRESETRESULT, Alg: jwt.ALGRAS},
&jwt.PasswordResetResultPayload{Message: "success"}, 60*60)
&jwt.PasswordResetResultPayload{Message: "Success"}, 10*60)

if perror != nil {
return fmt.Errorf("Expect no error, but %v", perror.LogError)
}

url := "http://localhost:8010/password/reset/result/" + base64.StdEncoding.EncodeToString([]byte(token))
fmt.Println(url)
resp, err := http.Get(url)

if err != nil {
Expand All @@ -58,7 +48,12 @@ func testPasswordResetResultOK() error {
func testPasswordResetFail() error {

url := "http://localhost:8010/password/reset/" + "random"
resp, err := http.Get(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("Expect no error, but %v", err)
}
c := http.Client{}
resp, err := c.Do(req)

if err != nil {
return fmt.Errorf("Expect no error, but %v", err)
Expand All @@ -71,15 +66,6 @@ func testPasswordResetFail() error {
}

func testPasswordResetOK() error {
cfg := config.Config{}
cfg.RSA = &config.RSAConfig{}
name := "ids_rsa_test"
cfg.RSA.Name = &name
path := "./docker"
cfg.RSA.Path = &path
if err := rsa.Init(&cfg); err != nil {
return fmt.Errorf("Expect no error, but %v", err)
}
token, perror := jwt.GenerateJWT(jwt.Head{Type: jwt.PASSWORDRESET, Alg: jwt.ALGRAS},
&jwt.PasswordResetPayload{Mail: "test@gmail.com"}, 60*60)

Expand All @@ -88,6 +74,7 @@ func testPasswordResetOK() error {
}

url := "http://localhost:8010/password/reset/" + base64.StdEncoding.EncodeToString([]byte(token))
fmt.Println(url)
resp, err := http.Get(url)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions manage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (m *Manger) RegisterWithEmail(register request.MailRegister) (uint, *perror
return user.ID, nil
}

func (m *Manger) RegisterVerifyMail(db *gorm.DB, rvm request.RegisterVerifyMail) *perror.PlutoError {
func (m *Manger) RegisterVerifyMail(db *gorm.DB, rvm request.RegisterVerifyMail, domain string) *perror.PlutoError {
if db == nil {
return perror.ServerError.Wrapper(errors.New("DB connection is empty"))
}
Expand All @@ -88,7 +88,7 @@ func (m *Manger) RegisterVerifyMail(db *gorm.DB, rvm request.RegisterVerifyMail)
}

ml := mail.NewMail(m.config)
if err := ml.SendRegisterVerify(user.ID, *user.Mail); err != nil {
if err := ml.SendRegisterVerify(user.ID, *user.Mail, domain); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions manage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/leeif/pluto/utils/mail"
)

func (m *Manger) ResetPasswordMail(rpm request.ResetPasswordMail) *perror.PlutoError {
func (m *Manger) ResetPasswordMail(rpm request.ResetPasswordMail, domain string) *perror.PlutoError {

user := models.User{}
identifyToken := b64.StdEncoding.EncodeToString([]byte(rpm.Mail))
Expand All @@ -27,7 +27,7 @@ func (m *Manger) ResetPasswordMail(rpm request.ResetPasswordMail) *perror.PlutoE
}

ml := mail.NewMail(m.config)
if err := ml.SendResetPassword(*user.Mail); err != nil {
if err := ml.SendResetPassword(*user.Mail, domain); err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions route/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func userRouter(router *mux.Router, db *gorm.DB, config *config.Config, logger *
respBody := make(map[string]interface{})
respBody["mail"] = register.Mail
ml := mail.NewMail(config)
go ml.SendRegisterVerify(userID, register.Mail)
go ml.SendRegisterVerify(userID, register.Mail, r.Host)
responseOK(respBody, w)
})).Methods("POST")

Expand All @@ -81,7 +81,7 @@ func userRouter(router *mux.Router, db *gorm.DB, config *config.Config, logger *
return
}

err := manager.RegisterVerifyMail(db, rvm)
err := manager.RegisterVerifyMail(db, rvm, r.Host)

if err != nil {
// set err to context for log
Expand All @@ -104,7 +104,7 @@ func userRouter(router *mux.Router, db *gorm.DB, config *config.Config, logger *
return
}

if err := manager.ResetPasswordMail(rpm); err != nil {
if err := manager.ResetPasswordMail(rpm, r.Host); err != nil {
context.Set(r, "pluto_error", err)
responseError(err, w)
next(w, r)
Expand Down
1 change: 0 additions & 1 deletion route/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func webRouter(router *mux.Router, db *gorm.DB, config *config.Config, logger *l
Message string
}
data := &Data{Message: prp.Message}

responseHTMLOK("password_reset_result.html", data, w)
})).Methods("GET")
}
12 changes: 6 additions & 6 deletions utils/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (m *Mail) Send(recv, subj, contentType, body string) error {
return nil
}

func (m *Mail) SendRegisterVerify(userID uint, address string) *perror.PlutoError {
func (m *Mail) SendRegisterVerify(userID uint, address string, domain string) *perror.PlutoError {
// expire time 10 mins
token, err := jwt.GenerateJWT(jwt.Head{Type: jwt.REGISTERVERIFY}, &jwt.RegisterVerifyPayload{UserID: userID}, 10*60)
if err != nil {
Expand All @@ -121,16 +121,16 @@ func (m *Mail) SendRegisterVerify(userID uint, address string) *perror.PlutoErro
BaseURL string
Token string
}
baseURL := m.config.Server.BaseURL
t.Execute(&buffer, Data{Token: b64.StdEncoding.EncodeToString([]byte(token)), BaseURL: *baseURL})
baseURL := "https://" + domain
t.Execute(&buffer, Data{Token: b64.StdEncoding.EncodeToString([]byte(token)), BaseURL: baseURL})
if err := m.Send(address, "[MuShare]Mail Verification", "text/html", buffer.String()); err != nil {
return perror.ServerError.Wrapper(errors.New("Mail sending failed: " + err.Error()))
}

return nil
}

func (m *Mail) SendResetPassword(address string) *perror.PlutoError {
func (m *Mail) SendResetPassword(address string, domain string) *perror.PlutoError {
// expire time 10 mins
token, err := jwt.GenerateJWT(jwt.Head{Type: jwt.PASSWORDRESET}, &jwt.PasswordResetPayload{Mail: address}, 10*60)
if err != nil {
Expand All @@ -144,8 +144,8 @@ func (m *Mail) SendResetPassword(address string) *perror.PlutoError {
BaseURL string
Token string
}
baseURL := m.config.Server.BaseURL
t.Execute(&buffer, Data{Token: b64.StdEncoding.EncodeToString([]byte(token)), BaseURL: *baseURL})
baseURL := "https://" + domain
t.Execute(&buffer, Data{Token: b64.StdEncoding.EncodeToString([]byte(token)), BaseURL: baseURL})
if err := m.Send(address, "[MuShare]Password Reset", "text/html", buffer.String()); err != nil {
return perror.ServerError.Wrapper(errors.New("Mail sending failed: " + err.Error()))
}
Expand Down

0 comments on commit 12236eb

Please sign in to comment.