GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( 27c8ce...a5aa25 )
by Amir
01:18
created

httpserver.putHandler   B

Complexity

Conditions 5

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nop 4
dl 0
loc 23
rs 8.9833
c 0
b 0
f 0
1
package httpserver
2
3
import (
4
	"bytes"
5
	"github.com/arazmj/gerdu/cache"
6
	"github.com/gorilla/mux"
7
	"github.com/prometheus/client_golang/prometheus/promhttp"
8
	"log"
9
	"net/http"
10
	"strconv"
11
)
12
13
func httpServe(httpPort int, gerdu cache.UnImplementedCache, verbose bool) (host string, router *mux.Router) {
14
	host = ":" + strconv.Itoa(httpPort)
15
	router = mux.NewRouter()
16
	router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) {
17
		getHandler(w, r, gerdu, verbose)
18
	}).Methods(http.MethodGet)
19
	router.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) {
20
		putHandler(w, r, gerdu, verbose)
21
	}).Methods(http.MethodPut)
22
	router.Handle("/metrics", promhttp.Handler())
23
	return host, router
24
}
25
26
func HttpServe(httpPort int, gerdu cache.UnImplementedCache, verbose bool) {
0 ignored issues
show
introduced by
exported function HttpServe should have comment or be unexported
Loading history...
introduced by
func HttpServe should be HTTPServe
Loading history...
27
	host, router := httpServe(httpPort, gerdu, verbose)
28
	log.Printf("Gerdu started listening HTTP on %d port\n", httpPort)
29
	log.Fatal(http.ListenAndServe(host, router))
30
}
31
32
func HttpServeTLS(httpPort int, tlsCert, tlsKey string, gerdu cache.UnImplementedCache, verbose bool) {
0 ignored issues
show
introduced by
exported function HttpServeTLS should have comment or be unexported
Loading history...
introduced by
func HttpServeTLS should be HTTPServeTLS
Loading history...
33
	host, router := httpServe(httpPort, gerdu, verbose)
34
	log.Printf("Gerdu started listening HTTPS TLS on %d port\n", httpPort)
35
	log.Fatal(http.ListenAndServeTLS(host, tlsCert, tlsKey, router))
36
}
37
38
func putHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache, verbose bool) {
39
	vars := mux.Vars(r)
40
	key := vars["key"]
41
	buf := new(bytes.Buffer)
42
	_, err := buf.ReadFrom(r.Body)
43
	if err != nil {
44
		log.Fatal(err.Error())
45
		return
46
	}
47
	value := buf.String()
48
49
	created := gerdu.Put(key, value)
50
	if verbose {
51
		if !created {
52
			log.Printf("HTTP UPDATE Key: %s Value: %s\n", key, value)
53
		} else {
54
			log.Printf("HTTP INSERT Key: %s Value: %s\n", key, value)
55
		}
56
	}
57
	if created {
58
		w.WriteHeader(http.StatusCreated)
59
	} else {
60
		w.WriteHeader(http.StatusNoContent)
61
	}
62
}
63
64
func getHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache, verbose bool) {
65
	vars := mux.Vars(r)
66
	key := vars["key"]
67
	if value, ok := gerdu.Get(key); ok {
68
		if verbose {
69
			log.Printf("HTTP RETREIVED Key: %s Value: %s\n", key, value)
70
		}
71
		w.WriteHeader(http.StatusOK)
72
		_, _ = w.Write([]byte(value))
73
	} else {
74
		if verbose {
75
			log.Printf("HTTP MISSED Key: %s \n", key)
76
		}
77
		w.WriteHeader(http.StatusNotFound)
78
	}
79
}
80