Skip to content

Commit

Permalink
replace redirect from absolute path to relative path. This is necesar…
Browse files Browse the repository at this point in the history
…y when deploy app using hostname. ie: govwa.example.com
  • Loading branch information
purnaresa.yuliartanto authored and purnaresa.yuliartanto committed Oct 21, 2020
1 parent 0dee17e commit 60af91f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ config
*.test
*.out

config/config.json
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
setting.SetRouter(router)

s := http.Server{
Addr: ":8888",
Addr: fmt.Sprintf(":%s", config.Cfg.Webport),
Handler: router,
}

Expand Down
10 changes: 4 additions & 6 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package util
import (
"fmt"
"net/http"

"github.com/govwa/util/config"
)

func Redirect(w http.ResponseWriter, r *http.Request, location string, code int){
redirect := fmt.Sprintf("%s%s", config.Fullurl,location)
http.Redirect(w,r,redirect,code)
}
func Redirect(w http.ResponseWriter, r *http.Request, location string, code int) {
redirect := fmt.Sprintf("/%s", location)
http.Redirect(w, r, redirect, code)
}
46 changes: 22 additions & 24 deletions util/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package middleware

import(
"log"
"time"
import (
"errors"
"regexp"
"log"
"net/http"
"regexp"
"time"

"github.com/govwa/util/config"
"github.com/govwa/user/session"
"github.com/julienschmidt/httprouter"
)


type Class struct{}

func New()*Class{
func New() *Class {
return &Class{}
}

func(self *Class) LoggingMiddleware(h httprouter.Handle) httprouter.Handle{
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params){
func (self *Class) LoggingMiddleware(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
start := time.Now()
log.Printf("Request From %s", r.Header.Get("User-Agent"))
log.Printf("Started %s %s", r.Method, r.URL.Path)
Expand All @@ -31,9 +29,9 @@ func(self *Class) LoggingMiddleware(h httprouter.Handle) httprouter.Handle{

func (this *Class) AuthCheck(h httprouter.Handle) httprouter.Handle {
var sess = session.New()
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if !sess.IsLoggedIn(r) {
redirect := config.Fullurl + "login"
redirect := "/login"
http.Redirect(w, r, redirect, http.StatusSeeOther)
return
}
Expand All @@ -42,39 +40,39 @@ func (this *Class) AuthCheck(h httprouter.Handle) httprouter.Handle {
}
}

func (this *Class)CapturePanic(h httprouter.Handle) httprouter.Handle {
func (this *Class) CapturePanic(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var err error
defer func() {
r := recover()
if r != nil {
switch t := r.(type) {
case string:
r := recover()
if r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
case error:
err = t
default:
err = errors.New("Unknown error")
default:
err = errors.New("Unknown error")
}
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}()
h(w, r, ps)
}
}

func(this *Class)DetectSQLMap(h httprouter.Handle)httprouter.Handle{
func (this *Class) DetectSQLMap(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
userAgent := r.Header.Get("User-Agent")
sqlmapDetected, _ := regexp.MatchString("sqlmap*", userAgent)
if sqlmapDetected{
if sqlmapDetected {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("Forbidden"))
log.Printf("sqlmap detect ")
return
}else{
} else {
h(w, r, ps)
}
}
}
}

0 comments on commit 60af91f

Please sign in to comment.