1. ~/project/xyz/views/mgmt/admin/update_form.html 을 작성한다.

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 수정</h4>
</div>
<div class="modal-body">
  <form name="update_form" action="/mgmt/admin/update" method="post">
    <div class="form-group">
      <label>아이디</label>
      <input type="text" name="userid" class="form-control" readonly required pattern="[a-zA-Z0-9]+" value="{{ .Admin.Userid }}"/>
      <input type="hidden" name="id" class="form-control" value="{{ .Admin.Sno }}" />
    </div>
    <div class="form-group">
      <label>별명 <small>(필수)</small></label>
      <input type="text" name="nick" class="form-control" required value="{{ .Admin.Nick }}"/>
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 수정" />
    </div>
  </form>
</div>

 

2. ~/project/xyz/controllers/mgmt/admin.go 에 다음을 추가한다.

...
 
// 관리자 수정 폼
// /mgnt/admin/update_form/{id}
func UpdateForm (c *fiber.Ctx) error {
    type Admin struct {
        Sno    int
        Userid string
        Passwd string
        Nick   string
    }
    var admin Admin
 
    id := c.Params("id")
 
    db := database.DBConn
    db.Raw("CALL getAdmin(?)", id).First(&admin)
    data := fiber.Map{"Admin": admin}
    return c.Render("mgmt/admin/update_form", data)
}
 
...

 

3. ~/project/xyz/routes/web.go 에 다음을 추가한다.

    mgmtApp.Get("/admin/update_form/:id", mgmt.UpdateForm)

 

4. 이제, 수정 버튼을 클릭하면 수정할 수 있는 폼이 나타나게 될 것이다. 마지막으로 실제로 수정을 처리하는 루틴을 작성하고 라우터에 등록하자. 그전에 updateAdmin 이라는 저장 프로시저부터 만들어야겠지?

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 262
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE updateAdmin
    (i_sno INT,
     i_nick VARCHAR(255))
BEGIN
    UPDATE admins SET nick = i_nick WHERE sno = i_sno;
END $$
DELIMITER ;
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd106 | nick6 |
+-----+---------+-----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> CALL updateAdmin(1, 'nick101');
Query OK, 1 ROW affected (0.00 sec)
 
mysql> CALL listAdmins();
+-----+---------+-----------+---------+
| sno | userid  | password  | nick    |
+-----+---------+-----------+---------+
|   1 | testid1 | passwd101 | nick101 |
|   2 | testid2 | passwd2   | nick2   |
|   3 | testid3 | passwd3   | nick3   |
|   4 | testid4 | passwd4   | nick4   |
|   5 | testid5 | passwd5   | nick5   |
|   9 | testid6 | passwd106 | nick6   |
+-----+---------+-----------+---------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

5. ~/project/xyz/controllers/mgmt/admin.go 에 다음을 추가한다.

...
 
// 관리자 수정
// /mgmt/admin/update
func Update (c *fiber.Ctx) error {
    id   := c.FormValue("id")
    nick := c.FormValue("nick")
 
    db := database.DBConn
    db.Exec("CALL updateAdmin(?, ?)", id, nick)
 
    return c.Redirect("/mgmt/admin")
}
 
...

 

6. ~/project/xyz/routes/web.go 에 다음을 추가하고, 수정 작업을 진행해보자.

    mgmtApp.Post("/admin/update", mgmt.Update)

 

이번에는 관리자의 비밀번호를 변경해볼 것입니다. 이를 위해서 저장프로시저를 2개 생성할 것이고, 관련 코드들을 작성해보겠습니다. 코드들이 이전과 비슷하지만, 조금씩 다들 부분들이 있으니 주의해서 봐주세요.

 

그리고, 이 튜토리얼의 소스는 https://github.com/gauryan/fiber-tutorial에서도 확인할 수 있습니다.

 

1. 저장 프로시저 (getAdmin) 생성

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE getAdmin(i_sno INT)
BEGIN
  SELECT sno, userid, password, nick FROM admins WHERE sno = i_sno LIMIT 1;
END $$
DELIMITER ;
mysql> CALL getAdmin(2);
+------+---------+----------+-------+
| sno  | userid  | password | nick  |
+------+---------+----------+-------+
|    2 | testid2 | passwd2  | nick2 |
+------+---------+----------+-------+
1 ROW IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

2. 뷰 디렉토리에 ~/project/xyz/views/mgmt/admin/chg_passwd_form.html 파일을 생성한다.

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 비밀번호 변경</h4>
</div>
<div class="modal-body">
  <form name="chg_passwd_form" action="/mgmt/admin/chg_passwd" method="post">
    <div class="form-group">
      <label>아이디</label>
      <input type="text" name="userid" class="form-control" readonly required value="{{ .Admin.Userid }}"/>
      <input type="hidden" name="id" value="{{ .Admin.Sno }}" />
    </div>
    <div class="form-group">
      <label>비밀번호 <small>(필수)</small></label>
      <input type="password" name="passwd1" class="form-control" required />
    </div>
    <div class="form-group">
      <label>비밀번호 확인 <small>(필수)</small></label>
      <input type="password" name="passwd2" class="form-control" required />
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 비밀번호 변경" />
    </div>
  </form>
</div>

 

3. 컨트롤러(~/project/xyz/controllers/mgmt/admin.go)에 다음을 추가한다.

...
 
// 관리자 비밀번호변경 폼
// /mgnt/admin/chg_passwd_form/:id
func ChgPasswdForm (c *fiber.Ctx) error {
    type Admin struct {
        Sno    int
        Userid string
        Passwd string
        Nick   string
    }
    var admin Admin
 
    id := c.Params("id")
 
    db := database.DBConn
    db.Raw("CALL getAdmin(?)", id).First(&admin)
    data := fiber.Map{"Admin": admin}
    return c.Render("mgmt/admin/chg_passwd_form", data)
}
 
...

 

4. 라우터(~/project/xyz/routes/web.go)에 다음을 추가한다.

    mgmtApp.Get("/admin/chg_passwd_form/:id", mgmt.ChgPasswdForm)

 

5. 이제, 비밀번호변경 버튼을 클릭하면 비밀번호변경을 위한 모달 다이얼로그박스가 나타날 것이다.

 

6. 실제로 비밀번호를 변경하는 작업을 해보자. 우선 저장 프로시저 (updateAdminPassword) 생성하자.

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE updateAdminPassword
    (i_sno INT,
     i_password VARCHAR(255))
BEGIN
    UPDATE admins SET password = i_password WHERE sno = i_sno;
END $$
DELIMITER ;
mysql> CALL listAdmins();
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
|   9 | testid6 | passwd6  | nick6 |
+-----+---------+----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> CALL updateAdminPassword(1, 'passwd101');
Query OK, 1 ROW affected (0.00 sec)
 
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd6   | nick6 |
+-----+---------+-----------+-------+
6 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

7. 그리고, 컨트롤러(~/project/xyz/controllers/mgmt/admin.go)에 다음을 추가한다.

...
 
// 관리자 비밀번호변경
// /mgmt/admin/chg_passwd
func ChgPasswd (c *fiber.Ctx) error {
    id      := c.FormValue("id")
    passwd1 := c.FormValue("passwd1")
    passwd2 := c.FormValue("passwd2")
 
    if passwd1 != passwd2 {
        return c.Redirect("/mgmt/admin")
    }
    db := database.DBConn
    db.Exec("CALL updateAdminPassword(?, ?)", id, passwd1)
 
    return c.Redirect("/mgmt/admin")
}
 
...

 

8. 라우터(~/project/xyz/routes/web.go)에는 다음을 추가한다.

    mgmtApp.Post("/admin/chg_passwd", mgmt.ChgPasswd)

 

9. 비밀번호를 변경해보고, DB의 내용이 잘 반영되었는지 확인해보자.

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 241
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> CALL listAdmins();
+-----+---------+-----------+-------+
| sno | userid  | password  | nick  |
+-----+---------+-----------+-------+
|   1 | testid1 | passwd101 | nick1 |
|   2 | testid2 | passwd2   | nick2 |
|   3 | testid3 | passwd3   | nick3 |
|   4 | testid4 | passwd4   | nick4 |
|   5 | testid5 | passwd5   | nick5 |
|   9 | testid6 | passwd106 | nick6 |
+-----+---------+-----------+-------+
6 rows in set (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
mysql>

이 튜토리얼의 소스는 https://github.com/gauryan/fiber-tutorial 에서도 확인할 수 있습니다.

관리자 목록을 보았으니, 이제는 새로운 관리자를 추가해봅시다. 

 

1. 저장 프로시저 (insertAdmin) 생성

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE insertAdmin
  (userid VARCHAR(255),
   password VARCHAR(255),
   nick VARCHAR(255))
BEGIN
  INSERT INTO admins(userid, password, nick) VALUES(userid, password, nick);
END $$
DELIMITER ;
mysql> SELECT * FROM admins;
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
+-----+---------+----------+-------+
5 ROWS IN SET (0.00 sec)
 
mysql> CALL insertAdmin('testid6', 'passwd6', 'nick6');
Query OK, 1 ROW affected (0.01 sec)
 
mysql> SELECT * FROM admins;
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
|   6 | testid6 | passwd6  | nick6 |
+-----+---------+----------+-------+
6 ROWS IN SET (0.00 sec)
 
mysql> exit

 

2. 관리자 입력 양식을 만들자. ~/project/xyz/views/mgmt/admin/insert_form.html

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 추가</h4>
</div>
<div class="modal-body">
  <form name="insert_form" action="/mgmt/admin/insert" method="post">
    <div class="form-group">
      <label>아이디 <small>(필수)</small></label>
      <input type="text" name="userid" class="form-control" required>
    </div>
    <div class="form-group">
      <label>비밀번호 <small>(필수)</small></label>
      <input type="password" id="password" name="passwd1" class="form-control" required>
    </div>
    <div class="form-group">
      <label>비밀번호 확인 <small>(필수)</small></label>
      <input type="password" name="passwd2" class="form-control" required>
    </div>
    <div class="form-group">
      <label>별명 <small>(필수)</small></label>
      <input type="text" name="nick" class="form-control" required>
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 추가" />
    </div>
  </form>
</div>

 

3. ~/project/xyz/controllers/mgmt/admin.go 에 다음 코드를 추가한다.

...
 
// 관리자 추가 폼
func InsertForm(c *fiber.Ctx) error {
    return c.Render("mgmt/admin/insert_form")
}
 
...

 

4. ~/project/xyz/routes/web.go 에서 mgmt 그룹안에 다음을 추가한다.

    mgmtApp.Get("/admin/insert_form", mgmt.InsertForm)

 

5. 관리자 추가 버튼을 클릭하면 모달 다이얼로그 박스 형식의 입력 양식이 나올 것이다.

 

6. 이제, 실제로 DB에 관리자를 추가해보자. ~/project/xyz/controllers/mgmt/admin.go 에 다음을 추가한다.

...
 
// 관리자 추가
func Insert (c *fiber.Ctx) error {
    userid  := c.FormValue("userid")
    passwd1 := c.FormValue("passwd1")
    passwd2 := c.FormValue("passwd2")
    nick    := c.FormValue("nick")
 
    if passwd1 != passwd2 {
        return c.Redirect("/mgmt/admin")
    }
    db := database.DBConn
    db.Exec("CALL insertAdmin(?, ?, ?)", userid, passwd1, nick)
 
    return c.Redirect("/mgmt/admin")
}
 
...

 

7. ~/project/xyz/routes/web.go 에서 mgmt 그룹안에 다음을 추가한다.

    mgmtApp.Post("/admin/insert", mgmt.Insert)

 

8. 코드 작성은 완료되었으니, 실제 화면에서 관리자를 등록해보면 목록에 표시되는 것을 볼 수 있을 것이다.

 

지난 글에서 admins 테이블을 생성하고, 기초 데이터 5개를 넣어두었습니다. 이제 http://xyz.test.com/mgmt/admin 를 접속하면 관리자 목록을 출력하는 페이지를 만들 것입니다. http://xyz.test.com/mgmt 에서 mgmt는 Management 를 줄인말입니다. xyz 사이트의 백엔드 프로그램이라고 생각하시면 될 듯 합니다.

 

먼저, MySQL 을 설치하고 기초데이터를 입력해 놓아야 합니다. 아래 링크를 먼저 보고 MySQL 설정을 하시고 다음을 진행하세요~!

MySQL 설치, 연결, DB생성, 사용자 생성, 테이블 생성, 기초데이터 입력

 

그리고, 이 튜토리얼의 소스는 (https://github.com/gauryan/fiber-tutorial)에서도 확인할 수 있습니다.

 

1. 저장 프로시저 (listAdmins) 생성 : 관리자 목록을 뽑아오는 쿼리입니다.

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE listAdmins()
BEGIN
  SELECT sno, userid, password, nick FROM admins;
END $$
DELIMITER ;
mysql> CALL listAdmins();
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
+-----+---------+----------+-------+
5 ROWS IN SET (0.00 sec)
 
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> exit

 

2. database 디렉토리 아래 database.go 를 만든다.

package database
 
import (
    "fmt"
    "gorm.io/gorm"
    "gorm.io/driver/mysql"
)
 
var (
    DBConn *gorm.DB
)
 
func Init() {
    var err error
    // dsn := "xyz:xyz123@tcp(127.0.0.1:3306)/xyz?charset=utf8mb4&parseTime=True&loc=Local"
    dsn := "xyz:xyz123@tcp(127.0.0.1:3306)/xyz"
    DBConn, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
        panic("failed to connect database")
    }
    fmt.Println("Connection Opened to Database")
}

GROM을 사용하고, MySQL에 접속하는 함수를 작성합니다.

 

3. ~/project/xyz/main.go 에 DB관련 코드 추가합니다.

package main
 
import (
    "github.com/gauryan/xyz/routes"
    "github.com/gauryan/xyz/database"
)
 
 
func main() {
    app := routes.Router()
    database.Init()
    app.Listen(":3000")
}

기본적인 DB설정은 끝났네요. 이제 본격적으로 코드를 작성해봅시다.

 

4. ~/project/xyz/controllers 밑에 mgmt 디렉토리를 만들고, admin.go 파일을 생성합니다.

package mgmt
// controllers/mgmt
 
import (
    "github.com/gauryan/xyz/database"
    "github.com/gofiber/fiber/v2"
)
 
 
// Admin 목록
func ListAdmin(c *fiber.Ctx) error {
    type Admin struct {
        Sno    int
        Userid string
        Nick   string
    }
    var admins []Admin
 
    db := database.DBConn
    // db.Raw("SELECT sno, userid, nick FROM admins").Scan(&admins)
    db.Raw("CALL listAdmins()").Scan(&admins)
 
    data := fiber.Map{"Admins": admins}
    return c.Render("mgmt/admin/index", data, "mgmt/base")
}

ListAdmin함수가 관리자 목록을 보여주기 위한 컨트롤러가 되겠습니다. 먼저, DB에 접속해서 위에서 만든 listAdmins 저장프로시저를 호출해서 admins 배열 변수에 담아줍니다. 그리고, Render 함수에서 템플릿(View) 파일인 view/mgmt/admin/index.html을 호출합니다. 이때, data 변수를 통해서 관리자목록도 함께 전달합니다. mgmt/base는 레이아웃 파일입니다. ORM 기능을 사용하지 않더라도 저장프로시저를 사용하므로 DB작업이 간단합니다. 재사용 계획이 있는 DB작업의 경우에는 Model 작성하는 것이 좋겠지만, 이 번 과정에서는 그럴 일이 없으므로 컨트롤러에서 직접 DB작업을 하는 것이 좋을 듯 하다.

 

5. ~/project/xyz/views/mgmt/base.html 파일을 생성한다.

<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Adonis Tutorial</title>
</head>
<body>
  {{embed}}
</body>
</html>

 

6. ~/project/xyz/views/mgmt/admin/index.html 파일을 생성한다.

<div class="container">
  <a href="/mgmt/logout">[로그아웃]</a>
  <div class="page-header">
    <h1>Administrator (관리자)</h1>
  </div>
 
  <div style="text-align: right; padding-bottom: 10px">
    <a href="/mgmt/admin/insert_form" class="btn btn-default" data-toggle="modal" data-target="#myModal">관리자 추가</a>
  </div>
 
  <table class="table table-striped table-hover table-condensed">
  <tr>
    <th style="text-align: center">아이디</th>
    <th style="text-align: center">별명</th>
    <th style="text-align: center">수정/삭제</th>
  </tr>
  {{range .Admins}}
  <tr>
    <td style="text-align: center">{{.Userid}}</td>
    <td style="text-align: center">{{.Nick}}</td>
    <td style="text-align: center">
      <a href="/mgmt/admin/chg_passwd_form/{{.Sno}}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">비밀번호변경</a>
      <a href="/mgmt/admin/update_form/{{.Sno}}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">수정</a>
      <button onclick="delete_admin('/mgmt/admin/delete/{{.Sno}}')" class="btn btn-default btn-xs">삭제</button>
    </td>
  </tr>
  {{end}}
  </table>
</div>
 
<div id="myModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
    </div>
  </div>
</div>
 
<script>
// Modal Remote Reload
$(document).on('hidden.bs.modal', function (e) {
    $(e.target).removeData('bs.modal');
})
</script>
 
<script>
function delete_admin(url) {
    var result = confirm("관리자를 정말로 삭제하시겠습니까?");
    if( result == false ) return;
    location.href = url;
}
</script>

.Admins 가 컨트롤러에서 넘겨받은 데이터이다. 이것을 range 로 루프를 돌려주면, 그 안에서 .Userid.Nick.Sno 등이 튀어나온다.

 

7. ~/project/xyz/routes/web.go 에 다음처럼 수정한다.

package routes
 
import (
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/template/html"
    // "github.com/gauryan/xyz/controllers"
    "github.com/gauryan/xyz/controllers/mgmt"
)
 
func Router() *fiber.App {
    // App 생성과 템플릿 설정
    app := fiber.New(fiber.Config{
        Views: html.New("./views", ".html"),
    })
 
    // Route 설정
    // app.Get("/", controllers.Index)
    mgmtApp := app.Group("/mgmt")
    mgmtApp.Get("/admin", mgmt.ListAdmin)
 
    return app
}

기존과 다른 점은 /mgmt 로 그룹을 만들고, 그 밑에 필요한 디렉토리들을 등록한다. 이렇게 그룹으로 관리하면 여러가지로 편한 면이 있다.

 

8. 모듈 의존성 업데이트

$ go mod tidy

 

9. 개발서버 실행

$ ./run.sh
 ┌---------------------------------------------------┐  
 │                   Fiber v2.19.0                   │ 
 │               http://127.0.0.1:3000               │ 
 │                                                   │ 
 │ Handlers ............. 2  Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID ............. 35382 │ 
 └---------------------------------------------------┘

 

10. 이제, 웹브라우저에서 http://xyz.test.com/mgmt/admin 에 접속하면 관리자 목록을 볼 수 있을 것이다.

 

+ Recent posts