net

package
v0.0.0-...-f28940e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Cookieの作成、取得、削除を行います。

Example:

cookie := NewCookie("example.com", true, http.SameSiteNoneMode)
exp := NewCookieHourExp(10)
cookie.Set(w, "key", "value", exp)

value, err := cookie.Get(r, "key")

err := cookie.Delete(w, r, "key")

Example:

exp := NewCookieHourExp(10) // 10時間
exp2 := NewCookieHourExp(2) // 2時間

exp3 := NewCookieMinutsExp(10) // 10分
exp4 := NewCookieMinutsExp(3) // 3分

POSTフォームの操作

Example:

type Json struct {
	Name string `json:"name"`
}

if CheckContentType(r) {
	var json Json
	if err := GetJsonForm(w, r, json); err != nil {
		panic(err)
	}
	...
}

返すレスポンスを統一します。

Example:

err := errors.New("dummy error")
ResponseError(w, 500, err) // internal server error

WriteBody(w, response) // 200 OK

Index

Constants

View Source
const (
	Success           = iota // 成功
	DefaultError             // エラー
	ErrorIntoError           // ResponseError内でのエラー
	BlockedError             // ブロックリストに入っていたエラー
	IncorrectMail            //  メールアドレスが正しくないエラー
	BotError                 // Bot判定したためエラー
	TimeOutError             // 時間切れ
	AlreadyDone              // 既に認証済み
	AccountNoExist           // アカウント無い
	ExistUserName            // ユーザIDがすでにある
	FailedLogin              // ログインできない
	IncorrectUserName        // ユーザ名が正しくない
	PasswordWrong            // パスワードが違う
	FailedOTP                // OTPが違う
	NoRedirectURI            // SSO serviceのリダイレクトURIが違う
	NoRefererURI             // SSO serviceのrefer uriが違う
	NoRole                   // roleが違う
	NotExistService          // そのようなSSO Serviceは存在しない
	IncorrectOIDC            // OpenID Connectの仕様に沿っていない
)

独自ステータスコード

Variables

This section is empty.

Functions

func CheckContentType

func CheckContentType(r *http.Request) bool

HTTPヘッダのContent-Typeが`application/json`であるかをチェックする

func CorsConfig

func CorsConfig() *cors.Cors

func DeleteCookie

func DeleteCookie(w http.ResponseWriter, req *http.Request, key string) error

cookieを削除

func GetCookie

func GetCookie(req *http.Request, key string) (string, error)

keyで指定した名前のcookieを返します

func GetIPAddress

func GetIPAddress(r *http.Request) string

func GetJsonForm

func GetJsonForm(w http.ResponseWriter, r *http.Request, obj interface{}) error

postのformのjsonをパースして内容を取得します

func GetQuery

func GetQuery(r *http.Request, key string) (string, error)

URLのクエリパラメータを取得します

func GetUserAgent

func GetUserAgent(r *http.Request) string

func ResponseCustomStatus

func ResponseCustomStatus(w http.ResponseWriter, statusCode int, body interface{})

bodyをHTTP Responceに書き出す

func ResponseError

func ResponseError(w http.ResponseWriter, err error)

エラーをHTTPで返す レスポンスではerror idを返し、ログからそのIDを検索することでエラーメッセージを参照できる

http statusはHTTPErrorで定義してください。 See more: https://github.com/cateiru/go-http-error

func ResponseErrorCustomCode

func ResponseErrorCustomCode(w http.ResponseWriter, statusCode int, err error, code int)

カスタムに独自ステータスコードを決定し、エラーをHTTPで返す レスポンスではerror idを返し、ログからそのIDを検索することでエラーメッセージを参照できる

func ResponseOK

func ResponseOK(w http.ResponseWriter, body interface{})

ステータスコード200で書き出す

Types

type AbstractResponse

type AbstractResponse struct {
	// 独自ステータスコード
	//
	// 特殊な事情でエラーが起きた場合HTTP ステータスコード以外にこのコードを指定します。
	Code int `json:"code"`
}
type Cookie struct {
	Path     string
	Domain   string
	Secure   bool
	HttpOnly bool
	SomeSite http.SameSite
}

func NewCookie

func NewCookie(domain string, secure bool, someSite http.SameSite, httpOnly bool) *Cookie

func (*Cookie) Delete

func (c *Cookie) Delete(w http.ResponseWriter, key string)

func (*Cookie) Set

func (c *Cookie) Set(w http.ResponseWriter, key string, value string, exp *CookieExp)

Cookieをセットします

type CookieExp

type CookieExp struct {
	IsSession bool
	// contains filtered or unexported fields
}

func NewCookieDayExp

func NewCookieDayExp(day int) *CookieExp

日単位の時間を作成

func NewCookieHourExp

func NewCookieHourExp(hour int) *CookieExp

1時間(60分)単位の時間を作成

func NewCookieMinutsExp

func NewCookieMinutsExp(minuts int) *CookieExp

分単位の時間を作成

func NewSession

func NewSession() *CookieExp

同一セッションのみ ブラウザが削除されるとcookieも削除されます

Warning: 多くのウェブブラウザーはセッション復元と呼ばれる機能を持っており、 これによってすべてのタブを保存し、次回ブラウザーを起動したときに復元することができます。 ブラウザーを実際には閉じていないかのように、セッションクッキーも復元されます。 by. MDN(https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Set-Cookie)

func (*CookieExp) GetNum

func (c *CookieExp) GetNum() int

時間をミリ秒のintで返す

func (*CookieExp) GetTime

func (c *CookieExp) GetTime() time.Duration

時間をtime.Durationで返す

type ErrorResponse

type ErrorResponse struct {
	StatusCode int    `json:"status_code"`
	ErrorID    string `json:"error_id"`

	AbstractResponse
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL