diff --git a/.github/workflows/BuildTest.yml b/.github/workflows/BuildTest.yml index a7efd2a..64d013f 100644 --- a/.github/workflows/BuildTest.yml +++ b/.github/workflows/BuildTest.yml @@ -5,11 +5,10 @@ jobs: name: tryToBuild runs-on: ubuntu-latest steps: - - name: Set up Go 1.13.x + - name: Set up Go 1.13.5 uses: actions/setup-go@v1 with: - go-version: 1.13.x - id: go + go-version: 1.13.5 - name: Check out code into the Go module directory uses: actions/checkout@v1 diff --git a/database/database.go b/database/database.go index 044bb46..82277c8 100644 --- a/database/database.go +++ b/database/database.go @@ -3,6 +3,6 @@ package database import "github.com/go-pg/pg/v9" func New(options *pg.Options) *pg.DB { - migration() + migration() //第一次連接先migration確保所有table都存在 return pg.Connect(options) } diff --git a/database/user.go b/database/user.go new file mode 100644 index 0000000..609491c --- /dev/null +++ b/database/user.go @@ -0,0 +1,20 @@ +package database + +import ( + "fmt" + "github.com/NPC-GO/MaJaJalist-backend/model" + "github.com/go-pg/pg/v9" +) + +type User struct { + DB *pg.DB +} + +func (u *User) GetUserByField(field, value string) (*model.User, error) { + var user model.User + err := u.DB.Model(&user).Where(fmt.Sprintf("%v = ?", field), value).First() + return &user, err +} +func (u *User) GetUserByToken(token string) (*model.User, error) { + return u.GetUserByField("token", token) +} diff --git a/gqlgen.yml b/gqlgen.yml index a86a0ea..b87f9c4 100644 --- a/gqlgen.yml +++ b/gqlgen.yml @@ -45,5 +45,7 @@ models: model: github.com/NPC-GO/MaJaJalist-backend/model.TodoConfig ChangeTodoConfigValueInput: model: github.com/NPC-GO/MaJaJalist-backend/model.ChangeTodoConfigValueInput + TodoAuthorLayout: + model: github.com/NPC-GO/MaJaJalist-backend/model.TodoAuthorLayout diff --git a/middleware/auth.go b/middleware/auth.go new file mode 100644 index 0000000..b139abc --- /dev/null +++ b/middleware/auth.go @@ -0,0 +1,25 @@ +package middleware + +import ( + "github.com/NPC-GO/MaJaJalist-backend/database" + "net/http" +) + +func BeforeLoginAuth(userDatabaseCtrl database.User) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + session, err := r.Cookie("session") + if err == nil { + if _, err := userDatabaseCtrl.GetUserByToken(session.Value); err == nil { + if r.Method == http.MethodGet { + http.Redirect(w, r, "https://"+r.URL.Host, 302) + return + } + http.Error(w, "you has login", 403) + return + } + } + next.ServeHTTP(w, r) + }) + } +} diff --git a/model/model_gqlgen.go b/model/model_gqlgen.go index 13253b9..612beef 100644 --- a/model/model_gqlgen.go +++ b/model/model_gqlgen.go @@ -24,13 +24,6 @@ type LoginInput struct { RecaptchaToken string `json:"recaptchaToken"` } -type TodoAuthorLayout struct { - ID string `json:"id"` - NickName string `json:"nickName"` - Avatar string `json:"avatar"` - Special bool `json:"special"` -} - type Level string const ( diff --git a/router/handler/handler.go b/router/handler/handler.go index cf116f8..c74311a 100644 --- a/router/handler/handler.go +++ b/router/handler/handler.go @@ -1,9 +1,15 @@ package handler import ( + "github.com/NPC-GO/MaJaJalist-backend/database" "net/http" ) func HtmlHandler(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./dist/index.html") } +func Login(userDatabaseCtrl database.User) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + } +} //post /login 的handler diff --git a/router/router.go b/router/router.go index 8850e03..e93b5d9 100644 --- a/router/router.go +++ b/router/router.go @@ -1,19 +1,23 @@ package router import ( + "github.com/NPC-GO/MaJaJalist-backend/database" + "github.com/NPC-GO/MaJaJalist-backend/middleware" "github.com/NPC-GO/MaJaJalist-backend/router/handler" "github.com/go-chi/chi" - "github.com/go-chi/chi/middleware" + chiMiddleware "github.com/go-chi/chi/middleware" "net/http" ) -func InitRouter() chi.Router { +func InitRouter(userDatabaseCtrl database.User) chi.Router { router := chi.NewRouter() router.Use( - middleware.Logger, - middleware.Recoverer, + chiMiddleware.Logger, + chiMiddleware.Recoverer, ) - router.Get("/*", http.StripPrefix("/", http.FileServer(http.Dir("dist"))).ServeHTTP) + router.With(middleware.BeforeLoginAuth(userDatabaseCtrl)).Post("/login", handler.Login(userDatabaseCtrl)) + //用middleware擋住已經登錄的 + router.Get("/*", http.StripPrefix("/", http.FileServer(http.Dir("dist"))).ServeHTTP) //用來提供js與css檔案 router.Get("/", handler.HtmlHandler) return router } diff --git a/server.go b/server.go index f3ef10f..412ef5b 100644 --- a/server.go +++ b/server.go @@ -14,12 +14,13 @@ func main() { Password: "postgres", Database: "MaJaJalist", Addr: "database:5432", - }) - defer DB.Close() - server := router.InitRouter() + }) //連接到database的設定 + defer DB.Close() //在最後斷開連接 + userDatabaseCtrl := database.User{DB: DB} + server := router.InitRouter(userDatabaseCtrl) //把db傳進router就不用每次要使用db都連接一次 err := http.ListenAndServeTLS(":443", "./certs/server.crt", "./certs/server.key", server) if err != nil { fmt.Println(err) - http.ListenAndServe(":80", server) + http.ListenAndServe(":80", server) //無法使用https則用http } }