Skip to content

Commit

Permalink
Merge pull request #16 from LinuxSuRen/fix-health-endpoint
Browse files Browse the repository at this point in the history
Fix the wrong health endpoint
  • Loading branch information
LinuxSuRen authored Oct 25, 2021
2 parents 8b740d5 + 34a1a73 commit b483bd6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go run cmd/server/root.go
go run cmd/server/root.go --mode
run-as-proxy:
go run cmd/server/root.go --mode proxy --gc-duration 1s

Expand Down
6 changes: 1 addition & 5 deletions cmd/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
case "server":
http.HandleFunc("/health", server.HealthHandler)
http.HandleFunc("/", server.GogetHandler)
if err = server.IntervalSelfRegistry(o.proxyCenter, o.externalAddress, time.Minute*1); err != nil {
err = fmt.Errorf("failed to self registry to the center proxy, error: %v", err)
return
}
fmt.Println("self registry success")
server.IntervalSelfRegistry(o.proxyCenter, o.externalAddress, time.Minute*1)
case "proxy":
http.HandleFunc("/registry", proxy.RegistryHandler)
http.HandleFunc("/", proxy.RedirectionHandler)
Expand Down
9 changes: 9 additions & 0 deletions pkg/common/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

import "strings"

// IsValid check if this is a valid uri
func IsValid(uri string) bool {
return strings.HasPrefix(uri, "/github.com/") ||
strings.HasPrefix(uri, "/gitee.com/")
}
18 changes: 2 additions & 16 deletions pkg/proxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package proxy
import (
"context"
"fmt"
"github.com/linuxsuren/goget/pkg/common"
"github.com/spf13/viper"
"net/http"
"strings"
"sync"
"time"
)
Expand All @@ -16,7 +16,7 @@ const KCandidates = "candidates"
// RedirectionHandler is the handler of proxy
func RedirectionHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("received a request", r.RequestURI)
if !isValid(r.RequestURI) {
if !common.IsValid(r.RequestURI) {
// TODO do the validation check
w.WriteHeader(http.StatusBadRequest)
_,_ = w.Write([]byte("invalid request, please check https://github.com/LinuxSuRen/goget"))
Expand All @@ -35,11 +35,6 @@ func RedirectionHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("no candidates found, please feel free to be a candidate with command 'goget-server --mode proxy --externalAddress your-ip:port'"))
}

func isValid(uri string) bool {
return strings.HasPrefix(uri, "/github.com/") ||
strings.HasPrefix(uri, "/gitee.com/")
}

// RegistryHandler receive the proxy registry request
func RegistryHandler(w http.ResponseWriter, r *http.Request) {
candidate := NewCandidate(r.URL.Query().Get("address"))
Expand Down Expand Up @@ -117,15 +112,6 @@ func getCandidatesFromConfig() (candidates *candidateSlice) {
fmt.Println(val)
candidates = &candidateSlice{}
}
//if candidatesRaw, ok := viper.Get(KCandidates).([]interface{}); !ok {
// if candidatesRaw, ok := viper.Get(KCandidates).([]map[interface{}]interface{}); !ok {
// candidates = newFromMap(candidatesRaw)
// } else {
// candidates = newFromMap(candidatesRaw)
// }
//} else {
// candidates = newFromArray(candidatesRaw)
//}
return
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/proxy/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewCandidate(address string) *candidate {
return &candidate{address: address}
}

func (c *candidate) reachable() bool {
func (c *candidate) reachable() (ok bool) {
if c.address == "" {
return false
}
Expand All @@ -32,9 +32,13 @@ func (c *candidate) reachable() bool {
}

client := http.DefaultClient
client.Timeout = time.Second * 3
resp, err := client.Get(address)
return err == nil && resp.StatusCode == http.StatusOK
client.Timeout = time.Second * 10
resp, err := client.Get(fmt.Sprintf("%s/health", address))
ok = err == nil && resp.StatusCode == http.StatusOK
if !ok {
fmt.Printf("failed to check health: %s\n", err.Error())
}
return
}

func (c *candidate) getHost() (address string) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/server/goget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/linuxsuren/goget/pkg/common"
"io"
"io/ioutil"
"net/http"
Expand All @@ -19,6 +20,12 @@ import (
// GogetHandler handles the goget request
func GogetHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.RequestURI)
if !common.IsValid(r.RequestURI) {
// TODO do the validation check
w.WriteHeader(http.StatusBadRequest)
_,_ = w.Write([]byte("invalid request, please check https://github.com/LinuxSuRen/goget"))
return
}

var err error
requestPath := strings.Split(r.RequestURI, "?")[0]
Expand Down
11 changes: 5 additions & 6 deletions pkg/server/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
Expand All @@ -15,22 +16,20 @@ func selfRegistry(center, address string) (err error) {
var resp *http.Response
if resp, err = http.Post(fmt.Sprintf("%s/registry?address=%s", center, address), "", nil); err == nil {
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("unexpected status code: %d", resp.StatusCode)
data, _ := ioutil.ReadAll(resp.Body)
err = fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, string(data))
}
}
return
}

func IntervalSelfRegistry(center, address string, duration time.Duration) (err error) {
err = selfRegistry(center, address)

func IntervalSelfRegistry(center, address string, duration time.Duration) {
go func() {
ticker := time.NewTicker(duration)
for range ticker.C {
if err = selfRegistry(center, address); err != nil {
if err := selfRegistry(center, address); err != nil {
fmt.Println("self registry failed", err)
}
}
}()
return
}

0 comments on commit b483bd6

Please sign in to comment.