diff --git a/apiV1Response.go b/apiV1Response.go index 159ec04..6949f45 100644 --- a/apiV1Response.go +++ b/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 { diff --git a/apiV1login.go b/apiV1login.go index 1647f67..ed78c32 100644 --- a/apiV1login.go +++ b/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{} diff --git a/apiv1.go b/apiv1.go index 48336e6..9c9a9a8 100644 --- a/apiv1.go +++ b/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) { diff --git a/config.go b/config.go index b970d41..25f40e5 100644 --- a/config.go +++ b/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" diff --git a/config.json b/config.json index b762b28..9504a9b 100644 --- a/config.json +++ b/config.json @@ -17,5 +17,11 @@ "StaticUrl": "/spa2/", "StripPrefix" : "/spa2/" } - ] + ], + "Session" : { + "Guest": true, + "Year": 10, + "Month": 1, + "Day": 1 + } } \ No newline at end of file diff --git a/go.mod b/go.mod index 37788cd..30cf567 100644 --- a/go.mod +++ b/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 diff --git a/main.go b/main.go index 66a0c2b..5af2b2a 100644 --- a/main.go +++ b/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)