Skip to content
Snippets Groups Projects
Commit 5a69b118 authored by Hans Kristian Hoel's avatar Hans Kristian Hoel
Browse files

have put the different handler func in seperet files, and made a func file for sheard functions

parent e504a82d
No related branches found
No related tags found
No related merge requests found
......@@ -7,42 +7,8 @@ import (
"net/http"
"sort"
"strconv"
"time"
)
var StartTime time.Time
func HandlerNil(w http.ResponseWriter, r *http.Request) { //standar default respons
fmt.Println("Default Handler: Invalid request received.")
http.Error(w, "Invalid request", http.StatusBadRequest)
}
func QueryGet(s string, t string, r *http.Request) string {
test := r.URL.Query().Get(s)
if test == "" {
test = t
}
return test
}
func DoRequest(Client *http.Client, w http.ResponseWriter, API string) *http.Response { // Request api
req, err := http.NewRequest(http.MethodGet, API, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
panic(err)
}
resp, err := Client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
panic(err)
}
return resp
}
func HandlerCommits(w http.ResponseWriter, r *http.Request) {
var I []IDS
......@@ -115,75 +81,3 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(C)
}
func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
var I []IDLang
var L Language
URL1 := "https://git.gvk.idi.ntnu.no/api/v4/projects"
Client := http.DefaultClient
resp := DoRequest(Client, w, URL1)
err := json.NewDecoder(resp.Body).Decode(&I)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for i := range I {
URL := URL1 + "/" + strconv.Itoa(I[i].ID) + "/languages"
resp = DoRequest(Client, w, URL)
err = json.NewDecoder(resp.Body).Decode(&L.Languages)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
// limit := QueryGet("limit", "5", r)
// auth := QueryGet("auth", "false", r)
http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
json.NewEncoder(w).Encode(L)
}
func HandlerStatus(w http.ResponseWriter, r *http.Request) {
var S Status
resp, err := http.Get("https://git.gvk.idi.ntnu.no")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
S.Gitlab = resp.StatusCode
resp, err = http.Get("https://git.gvk.idi.ntnu.no") //må endre link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
S.Database = resp.StatusCode
elapse := time.Since(StartTime)
S.Uptime = elapse.Seconds()
S.Version = "v1"
http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
json.NewEncoder(w).Encode(S)
}
// func HandlerWebhooks(w http.ResponseWriter, r *http.Request) {
// }
package assignment2
import (
"fmt"
"net/http"
"time"
)
var StartTime time.Time
func HandlerNil(w http.ResponseWriter, r *http.Request) { //standar default respons
fmt.Println("Default Handler: Invalid request received.")
http.Error(w, "Invalid request", http.StatusBadRequest)
}
func QueryGet(s string, t string, r *http.Request) string {
test := r.URL.Query().Get(s)
if test == "" {
test = t
}
return test
}
func DoRequest(Client *http.Client, w http.ResponseWriter, API string) *http.Response { // Request api
req, err := http.NewRequest(http.MethodGet, API, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
panic(err)
}
resp, err := Client.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
panic(err)
}
return resp
}
// func HandlerWebhooks(w http.ResponseWriter, r *http.Request) {
// }
package assignment2
import (
"encoding/json"
"net/http"
"strconv"
)
func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
var I []IDLang
var L Language
URL1 := "https://git.gvk.idi.ntnu.no/api/v4/projects"
Client := http.DefaultClient
resp := DoRequest(Client, w, URL1)
err := json.NewDecoder(resp.Body).Decode(&I)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for i := range I {
URL := URL1 + "/" + strconv.Itoa(I[i].ID) + "/languages"
resp = DoRequest(Client, w, URL)
err = json.NewDecoder(resp.Body).Decode(&L.Languages)
if err != nil {
return
}
}
// limit := QueryGet("limit", "5", r)
// auth := QueryGet("auth", "false", r)
http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
json.NewEncoder(w).Encode(L)
}
package assignment2
import (
"encoding/json"
"net/http"
"time"
)
func HandlerStatus(w http.ResponseWriter, r *http.Request) {
var S Status
resp, err := http.Get("https://git.gvk.idi.ntnu.no")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
S.Gitlab = resp.StatusCode
resp, err = http.Get("https://git.gvk.idi.ntnu.no") //må endre link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
S.Database = resp.StatusCode
elapse := time.Since(StartTime)
S.Uptime = elapse.Seconds()
S.Version = "v1"
http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
json.NewEncoder(w).Encode(S)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment