Skip to content
Snippets Groups Projects
Select Git revision
  • 1680c752303ff8ca79b578ffcdcaaca7620fc196
  • main default protected
  • feature/finalizing_v2
  • ui-experiment
  • feature/finalizing
  • dev
  • feedback_errors_to_llama
  • automatic_prompt
  • timetracking
  • database
  • experiment/api-running-ollama
11 results

should_not_compile

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    main.go 3.05 KiB
    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"html"
    	"log"
    	"net/http"
    	"os"
    	"strings"
    	"time"
    )
    //https://golang.org/doc/articles/wiki/#tmp_4
    //Server that prints hello
    // URL given to r, page data loaded.
    // formatted HTML is then sent to the page via W
    
    
    
    func urlSplitForCountry(a string)(string, string){
    const lengthUrl = 5
    
    	// 0/1=conserv/2=v1/3=country/4=no&limit=10
    	partsOne := strings.Split(a, "/")
    
    	// 0=no & 1=limit=10
    	partsTwo := strings.Split(partsOne[4], "&")
    
    	// 0=&limit 1=10
    	partsThree := strings.Split(partsOne[4], "=")
    
    
    	fmt.Printf(" PartsOne: %d\n PartsOne[4]: %d\n", len(partsOne), len(partsOne[4]))
    	if len(partsOne) == lengthUrl {
    
    		CountryCode := partsTwo[0]
    		fmt.Printf("CountryCode is: %s\n", CountryCode)
    
    		Limit := partsThree[1]
    		fmt.Printf("Limit is: %s\n", Limit)
    
    		return CountryCode, Limit
    
    	} else {fmt.Printf("Remember to use /conservation/v1/country/<value>&limit=<value> ")
    	return "",""
    	}
    
    }
    
    func defaultHandler(w http.ResponseWriter, r *http.Request){
    	//code to be added
    	fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    }
    
    func viewHandler(w http.ResponseWriter, r *http.Request) {
    
    	//Code to do
    	// should you test for client error or server error 400 / 500
    
    	CountryCode, Limit := urlSplitForCountry(r.URL.Path)
    
    	// Get country
    	CountryInfo:= getCountryName(CountryCode)
    
    	//fmt.Fprintf(w,"Country name is %s\n Country code is: %s\n Flag is: %s\n ", Country, Ccode, Flag)
    
    	// get species from a given country
    	Species := getSpeciesByCountry(CountryCode,Limit)
    	//fmt.Fprintf(w,"Species %v\n",Species.Collection)
    	json.NewEncoder(w).Encode(CountryInfo)
    	json.NewEncoder(w).Encode(Species)
    }
    
    func speciesHandler(w http.ResponseWriter, r *http.Request){
    	// 0/1=conserv/2=v1/3=species/4=speciesKey
    	partSpecie:= strings.Split(r.URL.Path,  "/")
    	speciesKey := partSpecie[4]
    
    	// SpeciesKey from url
    Species := getSpecies(speciesKey)
    
        // Year from url
    Year:= getYearForSpecie(speciesKey)
    
        // write to web
    //fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year)
        json.NewEncoder(w).Encode(Species)
        json.NewEncoder(w).Encode(Year)
    }
    
    func diagnosticHandler(w http.ResponseWriter, r *http.Request){
    	//  /conservation/v1/diag/
    	Diagnostic := getDiagnostic()
    
    	json.NewEncoder(w).Encode(Diagnostic)
    
    }
    // Global var type time
    var Start time.Time
    
    // Main function
    func main() {
    	port := os.Getenv("PORT")
    
    	// Remove this when deploy
    	//port ="8080"
    
    	if port == "" {
    		//log.fatal formatting output and exits
    		log.Fatal("$PORT must be set")
    	}
    
    	Start = time.Now()
    
        // we initialize http using viewHandler to handle any req
        // under the path / remember to end with trailing /
    
        // Add code to this function
    	http.HandleFunc("/", defaultHandler)
    
    	http.HandleFunc("/conservation/v1/country/", viewHandler)
    
    	http.HandleFunc("/conservation/v1/species/", speciesHandler)
    
    	http.HandleFunc("/conservation/v1/diag/", diagnosticHandler)
    
    
    
        //LiAnSe listens on TCP net addr, calls serve with handler (nil)
        // -to handle req on incoming connections, if accept it keeps TCP alive
    	log.Fatal(http.ListenAndServe(":"+port, nil))
    }