<commit_before>package main

import (
	"flag"
	"fmt"
	"net/http"
	"sync"
)

const (
	PORT = 5607
)

var (
	cache = make(map[string]string)
	mutx  sync.RWMutex
)

func put(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.WriteHeader(405)
	} else {
		err := r.ParseForm()
		if err != nil {
			w.WriteHeader(400)
			w.Write([]byte("Error parsing form params"))
		} else {
			key := r.Form.Get("key")
			if len(key) == 0 {
				w.WriteHeader(400)
				w.Write([]byte("Missing key"))
			} else {
				value := r.Form.Get("value")
				mutx.Lock()
				cache[key] = value
				mutx.Unlock()

				w.WriteHeader(200)
				w.Write([]byte("OK"))
			}
		}
	}
}

func get(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(405)
	} else {
		keys, ok := r.URL.Query()["key"]
		if !ok || len(keys) < 1 {
			w.WriteHeader(400)
			w.Write([]byte("Missing key"))
		} else {
			mutx.RLock()
			v := cache[keys[0]]
			mutx.RUnlock()

			if len(v) == 0 {
				w.WriteHeader(404)
				w.Write([]byte(fmt.Sprintf("No value found for '%s'", keys[0])))
			} else {
				w.WriteHeader(200)
				w.Write([]byte(v))
			}
		}
	}
}

func main() {
	port := flag.Int("port", PORT, "port to listen to")
	flag.Parse()

	fmt.Println("*** Welcome to the mini key/value store! ***")
	fmt.Printf("*** Listening on: %d ***\n", *port)

	mux := http.NewServeMux()
	mux.HandleFunc("/put", put)
	mux.HandleFunc("/get", get)

	http.ListenAndServe(fmt.Sprintf(":%d", *port), mux)
}<commit_msg>Improve http calls and add error handling<commit_after>// A simple PoC key/value store that works with strings
package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"sync"
)

const (
	PORT = 5607
)

var (
	cache = make(map[string]string)
	mutx  sync.RWMutex
)

func put(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "", http.StatusMethodNotAllowed)
	} else {
		err := r.ParseForm()
		if err != nil {
			http.Error(w, "Error parsing form params", http.StatusBadRequest)
		} else {
			key := r.Form.Get("key")
			if len(key) == 0 {
				http.Error(w, "Missing key", http.StatusBadRequest)
			} else {
				value := r.Form.Get("value")
				mutx.Lock()
				cache[key] = value
				mutx.Unlock()

				w.WriteHeader(http.StatusOK)
				w.Write([]byte("OK"))
			}
		}
	}
}

func get(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "", http.StatusMethodNotAllowed)
	} else {
		keys, ok := r.URL.Query()["key"]
		if !ok || len(keys) < 1 {
			http.Error(w, "Missing key", http.StatusBadRequest)
		} else {
			mutx.RLock()
			v := cache[keys[0]]
			mutx.RUnlock()

			if len(v) == 0 {
				http.Error(w, fmt.Sprintf("No value found for '%s'", keys[0]), http.StatusNotFound)
			} else {
				w.WriteHeader(http.StatusOK)
				w.Write([]byte(v))
			}
		}
	}
}

func main() {
	port := flag.Int("port", PORT, "port to listen to")
	flag.Parse()

	fmt.Println("*** Welcome to the mini key/value store! ***")
	fmt.Printf("*** Listening on: %d ***\n", *port)

	mux := http.NewServeMux()
	mux.HandleFunc("/put", put)
	mux.HandleFunc("/get", get)

	if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), mux); err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(-1)
	}
}
{'fraction_non_alphanumeric': 0.1984399375975039, 'fraction_numerical': 0.01341653666146646, 'mean_word_length': 3.1528497409326426, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1505103', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1279, 'n_words': 276}