Kaynağa Gözat

session is prepared, and now starts to make sure login works.

master
sp 4 yıl önce
ebeveyn
işleme
4a2e0ec82f
7 değiştirilmiş dosya ile 35 ekleme ve 15 silme
  1. +2
    -1
      apiV1Response.go
  2. +2
    -1
      apiV1login.go
  3. +7
    -3
      apiv1.go
  4. +9
    -3
      config.go
  5. +7
    -1
      config.json
  6. +3
    -3
      go.mod
  7. +5
    -3
      main.go

+ 2
- 1
apiV1Response.go Dosyayı Görüntüle

package main package main


import ( import (
"biukop.com/sfm/loan"
"biukop/sfm/loan"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
Msg string Msg string
TimeStamp string TimeStamp string
Body interface{} Body interface{}
Session loan.Session
} }


type apiV1Response struct { type apiV1Response struct {

+ 2
- 1
apiV1login.go Dosyayı Görüntüle

package main package main


import ( import (
"biukop/sfm/loan"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
"time" "time"
ts time.Time ts time.Time
} }


func apiV1Login(w http.ResponseWriter, r *http.Request) {
func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
res := apiV1ResponseBlank() res := apiV1ResponseBlank()


l := login{} l := login{}

+ 7
- 3
apiv1.go Dosyayı Görüntüle

package main package main


import ( import (
"biukop/sfm/loan"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
) )
type apiV1HandlerMap struct { type apiV1HandlerMap struct {
Method string Method string
Path string //regex Path string //regex
Handler func(http.ResponseWriter, *http.Request)
Handler func(http.ResponseWriter, *http.Request, *loan.Session)
} }


var apiV1Handler = []apiV1HandlerMap{ var apiV1Handler = []apiV1HandlerMap{
w.Header().Set("Content-Type", "application/json;charset=UTF-8") w.Header().Set("Content-Type", "application/json;charset=UTF-8")
path := r.URL.Path[len(apiV1Prefix):] //strip API prefix path := r.URL.Path[len(apiV1Prefix):] //strip API prefix


session := loan.Session{}
session.Retrieve(r) //TODO: check remote_addr changes in DB, to prevent possible Hack

for _, node := range apiV1Handler { for _, node := range apiV1Handler {
if r.Method == node.Method && path == node.Path { if r.Method == node.Method && path == node.Path {
node.Handler(w, r)
node.Handler(w, r, &session)
return return
} }
} }
//Catch for all //Catch for all
apiV1DumpRequest(w, r)
apiV1DumpRequest(w, r, &session)
} }


func apiV1ErrorCheck(e error) { func apiV1ErrorCheck(e error) {

+ 9
- 3
config.go Dosyayı Görüntüle

} }


type configuration struct { type configuration struct {
Host string
Port string
Static []configStaticHtml
Host string
Port string
Static []configStaticHtml
Session struct {
Guest bool
Year int //how many years
Month int //how many years
Day int //how many years
}
} }


var configFile = "config.json" var configFile = "config.json"

+ 7
- 1
config.json Dosyayı Görüntüle

"StaticUrl": "/spa2/", "StaticUrl": "/spa2/",
"StripPrefix" : "/spa2/" "StripPrefix" : "/spa2/"
} }
]
],
"Session" : {
"Guest": true,
"Year": 10,
"Month": 1,
"Day": 1
}
} }

+ 3
- 3
go.mod Dosyayı Görüntüle

module SFM_Loan_RestApi
module biukop/sfm/apiv1


go 1.15 go 1.15


replace biukop.com/sfm/loan => /home/sp/GolandProjects/SFM-loan
replace biukop/sfm/loan => /home/sp/GolandProjects/SFM-loan


require ( require (
biukop.com/sfm/loan v0.0.0-00010101000000-000000000000
biukop/sfm/loan v0.0.0-00010101000000-000000000000
github.com/VividCortex/mysqlerr v0.0.0-20201215173831-4c396ae82aac github.com/VividCortex/mysqlerr v0.0.0-20201215173831-4c396ae82aac
github.com/brianvoe/gofakeit/v6 v6.0.1 github.com/brianvoe/gofakeit/v6 v6.0.1
github.com/go-sql-driver/mysql v1.5.0 github.com/go-sql-driver/mysql v1.5.0

+ 5
- 3
main.go Dosyayı Görüntüle

package main package main


import ( import (
"biukop.com/sfm/loan"
"biukop/sfm/loan"
"encoding/json" "encoding/json"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
if e == nil { if e == nil {
fmt.Fprint(w, string(b)) fmt.Fprint(w, string(b))
} else { } else {
apiV1DumpRequest(w, nil)
apiV1DumpRequest(w, nil, nil)
} }
} }


func apiV1DumpRequest(w http.ResponseWriter, r *http.Request) {
func apiV1DumpRequest(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
dump := logRequestDebug(httputil.DumpRequest(r, true)) dump := logRequestDebug(httputil.DumpRequest(r, true))
dump = strings.TrimSpace(dump) dump = strings.TrimSpace(dump)
msg := fmt.Sprintf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path) msg := fmt.Sprintf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path)
dumpLines := strings.Split(dump, "\r\n") dumpLines := strings.Split(dump, "\r\n")
ar := apiV1ResponseBlank() ar := apiV1ResponseBlank()
ar.Env.Msg = msg ar.Env.Msg = msg
ar.Env.Session = *ss
ar.Env.Session.Secret = "***********"
ar.add("Body", dumpLines) ar.add("Body", dumpLines)
b, _ := ar.toJson() b, _ := ar.toJson()
fmt.Fprintf(w, "%s\n", b) fmt.Fprintf(w, "%s\n", b)

Yükleniyor…
İptal
Kaydet