<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/jordan-wright/cve-api/cve"
)

/*
	TODO: 	- Add gzip encoding (Middleware)
			- Support arbitrary fields (perhaps reflect can help with this?)
			- Add proper offset and limits (Middleware)
			- Add logging (Middleware)
*/

func NewRouter() *mux.Router {
	r := mux.NewRouter()
	r.HandleFunc("/{year:[1-2][0-9]{3}}", CVEByYearHandler)
	r.HandleFunc("/{year:[1-2][0-9]{3}}/{id:[0-9]+}", CVEByIdHandler)
	r.HandleFunc("/", CVEHandler)
	r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
	return r
}

func getCVE(e *cve.Entry) error {
	tx := db.Begin()
	tx.Model(e).Related(&e.CVSS)
	tx.Model(e).Related(&e.References)
	for i := 0; i < len(e.References); i++ {
		tx.Model(e.References[i]).Related(&e.References[i].Link)
	}
	tx.Model(e).Related(&e.Products)
	err := tx.Commit().Error
	return err
}

func CVEHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	entries := []cve.Entry{}
	db.Limit(100).Find(&entries)
	for i := 0; i < len(entries); i++ {
		err := getCVE(&entries[i])
		if err != nil {
			fmt.Println(err)
		}
	}
	entriesJs, err := json.MarshalIndent(&entries, " ", "    ")
	if err != nil {
		fmt.Println(err)
	}
	w.Write(entriesJs)
}

//CVEByYearHandler queries the database for all CVE's from a specific year and returns the details in JSON format.
func CVEByYearHandler(w http.ResponseWriter, r *http.Request) {
	year := mux.Vars(r)["year"]
	fmt.Fprintf(w, "Searching for CVE-%s-*", year)
}

//CVEByIdHandler queries the database for a specific CVE (CVE-XXXX-YYY...) and returns the details in JSON format.
func CVEByIdHandler(w http.ResponseWriter, r *http.Request) {
	year := mux.Vars(r)["year"]
	id := mux.Vars(r)["id"]
	fmt.Fprintf(w, "Searching for CVE-%s-%s", year, id)
}

//CVEByIdHandler queries the database for a specific CVE (CVE-XXXX-YYY...) and returns the details in JSON format.
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "404 - Not Found")
}<commit_msg>Added /:year, /:year/:id, and /:year-:id routes<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/jordan-wright/cve-api/cve"
)

/*
	TODO: 	- Add gzip encoding (Middleware)
			- Support arbitrary fields (perhaps reflect can help with this?)
			- Add proper offset and limits (Middleware)
			- Add logging (Middleware)
*/

func NewRouter() *mux.Router {
	r := mux.NewRouter()
	r.HandleFunc("/{year:[1-2][0-9]{3}}", CVEByYearHandler)
	r.HandleFunc("/{year:[1-2][0-9]{3}}-{id:[0-9]+}", CVEByIdHandler)
	r.HandleFunc("/{year:[1-2][0-9]{3}}/{id:[0-9]+}", CVEByIdHandler)
	r.HandleFunc("/", CVEHandler)
	r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
	return r
}

func getCVE(e *cve.Entry) error {
	tx := db.Begin()
	tx.Model(e).Related(&e.CVSS)
	tx.Model(e).Related(&e.References)
	for i := 0; i < len(e.References); i++ {
		tx.Model(e.References[i]).Related(&e.References[i].Link)
	}
	tx.Model(e).Related(&e.Products)
	err := tx.Commit().Error
	return err
}

func CVEHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	entries := []cve.Entry{}
	db.Limit(50).Find(&entries)
	for i := 0; i < len(entries); i++ {
		err := getCVE(&entries[i])
		if err != nil {
			fmt.Println(err)
		}
	}
	entriesJs, err := json.MarshalIndent(&entries, " ", "    ")
	if err != nil {
		fmt.Println(err)
	}
	w.Write(entriesJs)
}

//CVEByYearHandler queries the database for all CVE's from a specific year and returns the details in JSON format.
func CVEByYearHandler(w http.ResponseWriter, r *http.Request) {
	year := mux.Vars(r)["year"]
	entries := []cve.Entry{}
	db.Limit(50).Debug().Where("c_v_e_id LIKE ?", "CVE-"+year+"-%").Find(&entries)
	for i := 0; i < len(entries); i++ {
		err := getCVE(&entries[i])
		if err != nil {
			fmt.Println(err)
		}
	}
	entriesJs, err := json.MarshalIndent(&entries, " ", "    ")
	if err != nil {
		fmt.Println(err)
	}
	w.Write(entriesJs)
}

//CVEByIdHandler queries the database for a specific CVE (CVE-XXXX-YYY...) and returns the details in JSON format.
func CVEByIdHandler(w http.ResponseWriter, r *http.Request) {
	year := mux.Vars(r)["year"]
	id := mux.Vars(r)["id"]
	entry := cve.Entry{}
	db.Where("c_v_e_id = ?", "CVE-"+year+"-"+id).Find(&entry)
	err := getCVE(&entry)
	if err != nil {
		fmt.Println(err)
	}
	entryJs, err := json.MarshalIndent(&entry, " ", "    ")
	if err != nil {
		fmt.Println(err)
	}
	w.Write(entryJs)
}

//CVEByIdHandler queries the database for a specific CVE (CVE-XXXX-YYY...) and returns the details in JSON format.
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "404 - Not Found")
}
{'fraction_non_alphanumeric': 0.18688248896363255, 'fraction_numerical': 0.010300609627916754, 'mean_word_length': 4.077908217716115, 'pattern_counts': {'":': 0, '<': 8, '<?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': '151026', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1836, 'n_words': 403}