Commit dcd65f0
Changed files (2)
client.go
@@ -2,10 +2,14 @@ package main
import (
"bufio"
+ "crypto/tls"
+ "crypto/x509"
"flag"
"fmt"
"github.com/BurntSushi/toml"
+ "io/ioutil"
"log"
+ "net/http"
"os"
"strconv"
"strings"
@@ -80,11 +84,25 @@ func initUserSession() {
}
}
+// setupHttpClient initializes the http connection handler
+func setupHttpClient() {
+ ca_pool := x509.NewCertPool()
+ pemCert, _ := ioutil.ReadFile("/Users/RLuby/dev/golang/src/richluby/questioner/cert.pem")
+ ca_pool.AppendCertsFromPEM(pemCert)
+ tlsConfig := tls.Config{
+ ClientCAs: ca_pool,
+ InsecureSkipVerify: true}
+ transport := &http.Transport{
+ TLSClientConfig: &tlsConfig}
+ client = http.Client{Transport: transport}
+}
+
// initializes the client and handles the user interaction
func ExecuteClient() {
clientConfig.SERVER = "127.0.0.1"
clientConfig.PORT = 80
clientConfig.USE_HTTPS = false
+ setupHttpClient()
filePath := flag.String("file", "", "defines a path to the configuration file")
flag.Parse()
if strings.Compare(*filePath, "") != 0 {
command.go
@@ -137,7 +137,6 @@ func displayTest(clientTest ClientTest) {
// getScoreFromServer retrieves the previous tests for this user
// from the server
func getScoreFromServer(command Command) error {
- client := &http.Client{}
resp, err := client.Get(clientConfig.SERVER_URL + API_ROOT + "/test/score?username=" + clientConfig.USER)
defer resp.Body.Close()
if err != nil {
@@ -196,7 +195,7 @@ func runTest(clientTest *ClientTest) {
}
// postRecordsToServer sends the client responses back to the server
-func postRecordsToServer(client *http.Client, recordArray *ClientTest) error {
+func postRecordsToServer(recordArray *ClientTest) error {
data, err := json.Marshal(recordArray)
if err != nil {
return err
@@ -213,7 +212,7 @@ func postRecordsToServer(client *http.Client, recordArray *ClientTest) error {
}
// getRecordFromServer retrieves a record from the server
-func getRecordFromServer(client *http.Client, config CLIENT_CONFIG,
+func getRecordFromServer(config CLIENT_CONFIG,
numQuestions int, blueprint string) ([]ClientRecord, error) {
// allows adding configuration for port specific (ie HTTPS) requests
var resp *http.Response
@@ -240,7 +239,6 @@ func getRecordFromServer(client *http.Client, config CLIENT_CONFIG,
// categories from the server. functionality provided for
// command integration as well
func getCategoriesFromServer(command Command) error {
- client := &http.Client{}
resp, err := client.Get(clientConfig.SERVER_URL + API_ROOT + "/questions/categories")
if err != nil {
return err
@@ -307,7 +305,6 @@ func buildBluePrint(useBlueprint bool) (string, error) {
// executeTest runs a user through a test
// from retrieving the records to returning the answers
func executeTest(command Command) error {
- client := &http.Client{}
var clientTest ClientTest
questions, useBluprint, err := parseTestCommandFlags(command)
if err != nil {
@@ -320,7 +317,7 @@ func executeTest(command Command) error {
if err != nil {
return fmt.Errorf("Error while creating blueprint: %+v", err)
}
- clientTest.Records, err = getRecordFromServer(client, clientConfig, questions, blueprint)
+ clientTest.Records, err = getRecordFromServer(clientConfig, questions, blueprint)
if err != nil {
return fmt.Errorf("Error while requesting test from server: %+v", err)
}
@@ -328,5 +325,5 @@ func executeTest(command Command) error {
runTest(&clientTest)
fmt.Printf("You scored: %.2f%%.\n", clientTest.Score)
- return postRecordsToServer(client, &clientTest)
+ return postRecordsToServer(&clientTest)
}