Skip to content
Snippets Groups Projects
Commit d311e679 authored by Kenneth Tran's avatar Kenneth Tran
Browse files

Adding functions in status

parent abda73db
No related branches found
No related tags found
No related merge requests found
......@@ -8,13 +8,15 @@ import (
A"oblig2/packs"
)
func main(){
A.Init() //starting the app time
port := os.Getenv("PORT")
port = "8080"
if port == "" {
log.Fatal("PORT must be set")
}
A.GetDefaultHandler()
A.GetStatusHandler()
A.GetCommits() // calling handler from commits
log.Fatal(http.ListenAndServe(":"+port, nil))
......
package packs
import (
"encoding/json"
"net/http"
"fmt"
"time"
)
// Version of this app
const Version = "v1"
// Time of program start
var StartTime time.Time
type statusResp struct {
Gitlab int `json:gitlab`
Uptime int `json:uptime`
Version string `json:version`
}
func GetDefaultHandler(){
http.HandleFunc("/repocheck/v1/",defaultHandler)
}
func GetStatusHandler(){
http.HandleFunc("/repocheck/v1/status",StatusHandler)
}
func defaultHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "This is default handler, Welcome to Assignment2!", http.StatusBadRequest)
}
func Init() {
StartTime = time.Now()
}
func StatusHandler (w http.ResponseWriter, r* http.Request) {
switch r.Method {
case http.MethodGet:
var stats statusResp
//Testing if api returns 2 pages
res, _ := http.Get(APIROOT + "?per_page=1")
stats.Gitlab = res.StatusCode
stats.Version = Version
stats.Uptime = int(time.Since(StartTime).Seconds())
w.Header().Add("content-type", "application/json")
err := json.NewEncoder(w).Encode(stats)
if err != nil {
fmt.Println(err)
}
default:
http.Error(w, "Rejected on http method", http.StatusNotImplemented)
return
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment