Преглед изворни кода

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

master
sp пре 4 година
родитељ
комит
4a2e0ec82f
7 измењених фајлова са 35 додато и 15 уклоњено
  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 Прегледај датотеку

@@ -1,7 +1,7 @@
package main

import (
"biukop.com/sfm/loan"
"biukop/sfm/loan"
"encoding/json"
"fmt"
"net/http"
@@ -14,6 +14,7 @@ type apiV1Envelop struct {
Msg string
TimeStamp string
Body interface{}
Session loan.Session
}

type apiV1Response struct {

+ 2
- 1
apiV1login.go Прегледај датотеку

@@ -1,6 +1,7 @@
package main

import (
"biukop/sfm/loan"
log "github.com/sirupsen/logrus"
"net/http"
"time"
@@ -15,7 +16,7 @@ type login struct {
ts time.Time
}

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

l := login{}

+ 7
- 3
apiv1.go Прегледај датотеку

@@ -1,6 +1,7 @@
package main

import (
"biukop/sfm/loan"
"net/http"
"net/http/httputil"
)
@@ -10,7 +11,7 @@ const apiV1Prefix = "/api/v1/"
type apiV1HandlerMap struct {
Method string
Path string //regex
Handler func(http.ResponseWriter, *http.Request)
Handler func(http.ResponseWriter, *http.Request, *loan.Session)
}

var apiV1Handler = []apiV1HandlerMap{
@@ -25,14 +26,17 @@ func apiV1Main(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
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 {
if r.Method == node.Method && path == node.Path {
node.Handler(w, r)
node.Handler(w, r, &session)
return
}
}
//Catch for all
apiV1DumpRequest(w, r)
apiV1DumpRequest(w, r, &session)
}

func apiV1ErrorCheck(e error) {

+ 9
- 3
config.go Прегледај датотеку

@@ -13,9 +13,15 @@ type configStaticHtml 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"

+ 7
- 1
config.json Прегледај датотеку

@@ -17,5 +17,11 @@
"StaticUrl": "/spa2/",
"StripPrefix" : "/spa2/"
}
]
],
"Session" : {
"Guest": true,
"Year": 10,
"Month": 1,
"Day": 1
}
}

+ 3
- 3
go.mod Прегледај датотеку

@@ -1,11 +1,11 @@
module SFM_Loan_RestApi
module biukop/sfm/apiv1

go 1.15

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

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/brianvoe/gofakeit/v6 v6.0.1
github.com/go-sql-driver/mysql v1.5.0

+ 5
- 3
main.go Прегледај датотеку

@@ -1,7 +1,7 @@
package main

import (
"biukop.com/sfm/loan"
"biukop/sfm/loan"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
@@ -64,17 +64,19 @@ func apiSendJson(p interface{}, w http.ResponseWriter) {
if e == nil {
fmt.Fprint(w, string(b))
} 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 = strings.TrimSpace(dump)
msg := fmt.Sprintf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path)
dumpLines := strings.Split(dump, "\r\n")
ar := apiV1ResponseBlank()
ar.Env.Msg = msg
ar.Env.Session = *ss
ar.Env.Session.Secret = "***********"
ar.add("Body", dumpLines)
b, _ := ar.toJson()
fmt.Fprintf(w, "%s\n", b)

Loading…
Откажи
Сачувај