Files
news-demo/checkpoints/02/main.go
Ayooluwa Isaiah cc4a2af266 Add checkpoints
2020-11-22 20:47:20 +01:00

69 lines
1.4 KiB
Go

package main
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/freshman-tech/news-demo-starter-files/news"
"github.com/joho/godotenv"
)
var tpl = template.Must(template.ParseFiles("index.html"))
func indexHandler(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, nil)
}
func searchHandler(newsapi *news.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
params := u.Query()
searchQuery := params.Get("q")
page := params.Get("page")
if page == "" {
page = "1"
}
fmt.Println("Search Query is: ", searchQuery)
fmt.Println("Page is: ", page)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Println("Error loading .env file")
}
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
apiKey := os.Getenv("NEWS_API_KEY")
if apiKey == "" {
log.Fatal("Env: apiKey must be set")
}
myClient := &http.Client{Timeout: 10 * time.Second}
newsapi := news.NewClient(myClient, apiKey, 20)
fs := http.FileServer(http.Dir("assets"))
mux := http.NewServeMux()
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
mux.HandleFunc("/search", searchHandler(newsapi))
mux.HandleFunc("/", indexHandler)
http.ListenAndServe(":"+port, mux)
}