| 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 { |
| 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{} |
| 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) { |
| } | } | ||||
| 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" |
| "StaticUrl": "/spa2/", | "StaticUrl": "/spa2/", | ||||
| "StripPrefix" : "/spa2/" | "StripPrefix" : "/spa2/" | ||||
| } | } | ||||
| ] | |||||
| ], | |||||
| "Session" : { | |||||
| "Guest": true, | |||||
| "Year": 10, | |||||
| "Month": 1, | |||||
| "Day": 1 | |||||
| } | |||||
| } | } |
| 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 |
| 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) |