pp_projekt/main.go

235 lines
4.5 KiB
Go
Raw Normal View History

2018-11-24 20:50:31 +01:00
package main
import (
2018-12-02 19:22:59 +01:00
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
2018-11-24 20:50:31 +01:00
"log"
"net/http"
2018-12-02 19:22:59 +01:00
"os"
"strconv"
"time"
2018-11-24 20:50:31 +01:00
)
2018-12-02 19:22:59 +01:00
import _"github.com/lib/pq"
2018-11-24 20:50:31 +01:00
func main() {
2018-12-02 19:22:59 +01:00
initDb()
defer db.Close()
2018-11-24 20:50:31 +01:00
2018-12-02 19:22:59 +01:00
//router := router.NewRouter()
http.HandleFunc("/test", test)
http.HandleFunc("/test2", createTerms2)
http.HandleFunc("/date", createTerms)
log.Fatal(http.ListenAndServe(":8080", nil))
2018-11-24 20:50:31 +01:00
}
2018-12-02 19:22:59 +01:00
var db *sql.DB
const (
dbhost="DBHOST"
dbport="5432"
dbuser="postgres"
dbpass="admin"
dbname="mydb"
)
func initDb() {
//config := dbConfig()
var err error
//psqlInfo := fmt.Sprintf("host=localhost port=%s user=%s password=%s dbname=%s sslmode=disable",
// config[dbport], config[dbuser], config[dbpass], config[dbname])
db, err = sql.Open("postgres", "port=5432 password=admin user=postgres dbname=mydb sslmode=disable")
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfullu connected!")
}
func dbConfig() map[string]string {
conf := make(map[string]string)
host, ok := os.LookupEnv(dbhost)
if !ok {
panic("DBHOST environment variable required but not set")
}
port, ok := os.LookupEnv(dbport)
if !ok {
panic("DBPORT environment variable required but not set")
}
user, ok := os.LookupEnv(dbuser)
if !ok {
panic("DBUSER environment variable required but not set")
}
password, ok := os.LookupEnv(dbpass)
if !ok {
panic("DBPASS environment variable required but not set")
}
name, ok := os.LookupEnv(dbname)
if !ok {
panic("DBNAME environment variable required but not set")
}
conf[dbhost] = host
conf[dbport] = port
conf[dbuser] = user
conf[dbpass] = password
conf[dbname] = name
return conf
}
func testFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println("weszlo")
repos := repositories{}
err := queryRepos(&repos)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
out, err := json.Marshal(repos)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
fmt.Fprint(w, repos)
fmt.Fprintf(w, string(out))
}
func test(rw http.ResponseWriter, req *http.Request) {
//body, err := ioutil.ReadAll(req.Body)
//if err != nil {
// panic(err)
//}
//fmt.Println(string(body))
//var t test_struct
//err = json.Unmarshal(body, &t)
//if err != nil {
// panic(err)
//}
//fmt.Println(t.Test1)
//fmt.Println(t.aaa)
//addRow("2018-02-04")
}
func createTerms2(rw http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var t2 test_struct2
err := decoder.Decode(&t2)
if err != nil {
panic(err)
}
fmt.Println(t2.Test)
fmt.Println(t2.SomeKey)
}
type test_struct2 struct {
Test []int `json:"months"`
SomeKey int `json:"year"`
}
type test_struct struct {
Test1 int
aaa string
}
func createTerms(w http.ResponseWriter, req *http.Request) {
//year, ok :=r.URL.Query()["year"]
//daysIn(11,2018)
//daysIn(12,2018)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
log.Println(string(body))
var term term_struct
err = json.Unmarshal(body, &term)
if err != nil {
panic(err)
}
//fmt.Println(term.Year)
//fmt.Println(term.Months)
for i:=0; i< len(term.Months); i++ {
daysIn(term.Months[i], term.Year)
// //fmt.Println(daysIn(newTerm.months[i], newTerm.year))
}
}
type term_struct struct {
Months []int `json:"months"`
Year int `json:"year"`
}
func daysIn(m int, year int) {
var data string
dni := time.Date(year, time.Month(m+1),0,0,0,0,0,time.UTC)
for i := 1; i <= dni.Day(); i++ {
data = strconv.Itoa(year) + "-" + strconv.Itoa(int(m)) + "-" + strconv.Itoa(i)
fmt.Println("data: ", data)
addRow(data)
}
}
func addRow(data string) {
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer tx.Rollback()
stmt, err := tx.Prepare("INSERT INTO calendar (date, status, contractNo) VALUES ( $1, 'available', '');")
if err != nil {
log.Fatal(err)
}
defer stmt.Close() // danger!
for i := 0; i < 1; i++ {
_, err = stmt.Exec(data)
if err != nil {
log.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
}
func queryRepos(repos *repositories) error {
rows, err := db.Query(`SELECT * FROM friends.test;`)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
repo := repositorySummary{}
err = rows.Scan(
&repo.firstname,
&repo.lastname,)
if err != nil {
return err
}
fmt.Println("mam", repo)
repos.Repositories = append(repos.Repositories, repo)
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
type repositorySummary struct {
firstname string
lastname string
}
type repositories struct {
Repositories []repositorySummary
}