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 ( 638b76...87b3f2 )
by Amir
01:24
created

httpserver/http_server.go   A

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 73
dl 0
loc 96
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B httpserver.putHandler 0 23 5
A httpserver.HttpServeTLS 0 4 1
A httpserver.getHandler 0 14 4
A httpserver.HttpServe 0 4 1
A httpserver.httpServe 0 14 4
A httpserver.deleteHandler 0 13 4
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.HandleFunc("/cache/{key}", func(w http.ResponseWriter, r *http.Request) {
23
		deleteHandler(w, r, gerdu, verbose)
24
	}).Methods(http.MethodDelete)
25
	router.Handle("/metrics", promhttp.Handler())
26
	return host, router
27
}
28
29
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...
30
	host, router := httpServe(httpPort, gerdu, verbose)
31
	log.Printf("Gerdu started listening HTTP on %d port\n", httpPort)
32
	log.Fatal(http.ListenAndServe(host, router))
33
}
34
35
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...
36
	host, router := httpServe(httpPort, gerdu, verbose)
37
	log.Printf("Gerdu started listening HTTPS TLS on %d port\n", httpPort)
38
	log.Fatal(http.ListenAndServeTLS(host, tlsCert, tlsKey, router))
39
}
40
41
func putHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache, verbose bool) {
42
	vars := mux.Vars(r)
43
	key := vars["key"]
44
	buf := new(bytes.Buffer)
45
	_, err := buf.ReadFrom(r.Body)
46
	if err != nil {
47
		log.Fatal(err.Error())
48
		return
49
	}
50
	value := buf.String()
51
52
	created := gerdu.Put(key, value)
53
	if verbose {
54
		if !created {
55
			log.Printf("HTTP UPDATE Key: %s Value: %s\n", key, value)
56
		} else {
57
			log.Printf("HTTP INSERT Key: %s Value: %s\n", key, value)
58
		}
59
	}
60
	if created {
61
		w.WriteHeader(http.StatusCreated)
62
	} else {
63
		w.WriteHeader(http.StatusNoContent)
64
	}
65
}
66
67
func getHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache, verbose bool) {
68
	vars := mux.Vars(r)
69
	key := vars["key"]
70
	if value, ok := gerdu.Get(key); ok {
71
		if verbose {
72
			log.Printf("HTTP RETREIVED Key: %s Value: %s\n", key, value)
73
		}
74
		w.WriteHeader(http.StatusOK)
75
		_, _ = w.Write([]byte(value))
76
	} else {
77
		if verbose {
78
			log.Printf("HTTP MISSED Key: %s \n", key)
79
		}
80
		w.WriteHeader(http.StatusNotFound)
81
	}
82
}
83
84
func deleteHandler(w http.ResponseWriter, r *http.Request, gerdu cache.UnImplementedCache, verbose bool) {
85
	vars := mux.Vars(r)
86
	key := vars["key"]
87
	if ok := gerdu.Delete(key); ok {
88
		if verbose {
89
			log.Printf("HTTP DELETED Key: %s\n", key)
90
		}
91
		w.WriteHeader(http.StatusOK)
92
	} else {
93
		if verbose {
94
			log.Printf("HTTP MISSED Key: %s \n", key)
95
		}
96
		w.WriteHeader(http.StatusNotFound)
97
	}
98
}
99