twitchapon

[Twi]tch [Cha]nnel [Po]ints Rewards Redemption Listener
git clone git://bsandro.tech/twitchapon
Log | Files | Refs | README | LICENSE

local_server.go (714B)


      1 package main
      2 
      3 import (
      4 	"io"
      5 	"log"
      6 	"net/http"
      7 )
      8 
      9 func LocalServerJob() {
     10 	http.HandleFunc("/", HandleHttpRequest)
     11 	err := http.ListenAndServe(App.Config.LocalServer, nil)
     12 	if err != nil {
     13 		log.Fatal(err)
     14 	}
     15 	App.WaitGroup.Done()
     16 }
     17 
     18 func HandleHttpRequest(w http.ResponseWriter, req *http.Request) {
     19 	var resp string
     20 	defer func() {
     21 		io.WriteString(w, resp)
     22 	}()
     23 
     24 	codes, exists := req.URL.Query()["code"]
     25 	if exists && len(codes) > 0 {
     26 		auth_code := codes[0]
     27 
     28 		err := App.InitAuth(auth_code)
     29 		if err != nil {
     30 			resp = err.Error()
     31 			return
     32 		}
     33 
     34 		err = App.InitUser()
     35 		if err != nil {
     36 			resp = err.Error()
     37 			return
     38 		}
     39 
     40 		RequestListen()
     41 		resp = "OK"
     42 
     43 	} else {
     44 		resp = "Error: no code provided"
     45 	}
     46 }