meloalright-io


  • Home

  • Archives

  • About

Golang JWT 示例

Posted on 2020-02 |

Golang JWT 示例

Demo

.env

1
SECRET_KEY=secret_key

main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main

import (
"os"
"fmt"
"time"
"github.com/joho/godotenv"
"github.com/dgrijalva/jwt-go"
)

func init() {
// loads values from .env into the system
if err := godotenv.Load(); err != nil {
fmt.Print("No .env file found")
}
}

func main() {
SecretString, exists := os.LookupEnv("SECRET_KEY")

if !exists {
return
}

fmt.Println(SecretString)
Secret := []byte(SecretString)

type MyCustomClaims struct {
ID int `json:"id"`
jwt.StandardClaims
}

// Create the Claims
claims := MyCustomClaims{
10012,
jwt.StandardClaims{
ExpiresAt: time.Time.AddDate(time.Now(),0, 0, 7).Unix(),
Issuer: "localhost",
},
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(Secret)
fmt.Printf("%v %v", ss, err)
}
Read more »

基于 dep 依赖管理的 Golang Gitlab CI 配置方法

Posted on 2020-02 |

.gitlab-ci.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
image: golang:latest

variables:
REPO_NAME: {Your Repo}

before_script:
- mkdir -p $GOPATH/src/$(dirname $REPO_NAME)
- ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME
- cd $GOPATH/src/$REPO_NAME
- go get -u github.com/golang/dep/cmd/dep


stages:
- test
- build

format:
stage: test
script:
- dep ensure
- go test

compile:
stage: build
script:
- dep ensure
- go build -o main
- mkdir mybinary && cp main mybinary/
artifacts:
paths:
- mybinary
Read more »
123…6

meloalright

12 posts
GitHub
© 2021 MELOALRIGHT
Powered by Hexo
|
Theme — NexT.Muse v5.1.4
MELOALRIGHT.COM|京公网安备 11010502041127号
0%