Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
support multiple users
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyangXYZ committed Apr 26, 2018
1 parent 1e4617d commit 9c778c1
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 14 deletions.
3 changes: 3 additions & 0 deletions 1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sudo docker-compose stop
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o AssassinGo
sudo docker-compose up --build -d
Binary file modified AssassinGo
Binary file not shown.
25 changes: 25 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"fmt"
"os"
)

var (
// RootDir of your app
RootDir = "./web"

// SecretKey computes sg_token
SecretKey string

// DB addr and passwd.
DB string
)

func init() {
DB = fmt.Sprintf("%v:%v@tcp(mariadb:3306)/%v?charset=utf8",
os.Getenv("DB_User"),
os.Getenv("DB_Passwd"),
os.Getenv("DB_Db"))
SecretKey = os.Getenv("SecretKey")
}
24 changes: 15 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ services:
ports:
- 8000:8000
links:
- chromedp
- mariadb
- chromedp
volumes:
- ./web/:/web/
- ./dict/:/dict/

chromedp:
image: knqz/chrome-headless
ports:
- 9222:9222
environment:
- DB_Db=ag
- DB_User=ag
- DB_Passwd=password
- SecretKey=biubiubiu

mariadb:
image: mariadb
expose:
- "3306"
environment:
- MYSQL_ROOT_PASSWORD=assassingo
- MYSQL_DATABASE=ag
- MYSQL_USER=ag
- MYSQL_PASSWORD=agpasswd
- MYSQL_DATABASE=ag
- MYSQL_PASSWORD=password

chromedp:
image: knqz/chrome-headless
ports:
- 9222:9222

2 changes: 2 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM alpine

RUN apk --no-cache add ca-certificates

ADD AssassinGo /

CMD [ "./AssassinGo" ]
3 changes: 2 additions & 1 deletion web/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"../assassin"
"../config"
"../logger"
"github.com/AmyangXYZ/sweetygo"
jwt "github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -33,7 +34,7 @@ func signin(ctx *sweetygo.Context) {
claims := token.Claims.(jwt.MapClaims)
claims["username"] = username
claims["exp"] = time.Now().Add(time.Hour * 36).Unix()
t, _ := token.SignedString([]byte("secret-key"))
t, _ := token.SignedString([]byte(config.SecretKey))

ctx.JSON(200, 1, "success", map[string]string{"SG_Token": t})

Expand Down
5 changes: 3 additions & 2 deletions web/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"fmt"
"time"

"../config"
//
_ "github.com/go-sql-driver/mysql"
)

var db *sql.DB

func init() {
db, _ = sql.Open("mysql", "ag:agpasswd@tcp(mariadb:3306)/ag?charset=utf8")
db, _ = sql.Open("mysql", config.DB)

// mysql image starts need time.
for {
err := db.Ping()
if err == nil {
fmt.Println("db ok!")
break
}
fmt.Println(err)
Expand All @@ -36,6 +36,7 @@ func init() {
email VARCHAR(64) NULL DEFAULT NULL,
PRIMARY KEY (id)
);`)
db.Exec(`INSERT INTO users(username, password, email) values("admin","adminn","[email protected]");`)
}

func getPassword(username string) string {
Expand Down
3 changes: 2 additions & 1 deletion web/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"../config"
"github.com/AmyangXYZ/sweetygo"
"github.com/AmyangXYZ/sweetygo/middlewares"
)
Expand All @@ -14,7 +15,7 @@ var (

// SetMiddlewares sets middlewares.
func SetMiddlewares(app *sweetygo.SweetyGo) {
app.USE(middlewares.JWT("Header", "secret-key", requireJWTMap))
app.USE(middlewares.JWT("Header", config.SecretKey, requireJWTMap))
}

// SetRouter sets router.
Expand Down
3 changes: 2 additions & 1 deletion web/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"../assassin"
"../config"
"github.com/AmyangXYZ/sweetygo"
)

Expand All @@ -15,7 +16,7 @@ func init() {

// Run Web GUI.
func Run() {
app := sweetygo.New("./web", nil)
app := sweetygo.New(config.RootDir, nil)
SetMiddlewares(app)
SetRouter(app)
app.RunServer(":8000")
Expand Down

0 comments on commit 9c778c1

Please sign in to comment.