Backend/main.go

68 lines
1.4 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"timefu.li/backend/tubson"
"timefu.li/backend/tubsql"
)
func main() {
type foo_request_body struct {
Arg1 int
Arg2 int
}
type foo_response_body struct {
Field1 string
Field2 int
}
foo_route_handler := func(body foo_request_body, raw_request *http.Request) (foo_response_body, tubson.Http_status_code) {
return foo_response_body {
Field1: "hi there on foo!",
Field2: body.Arg1 + body.Arg2,
}, tubson.OK
}
tubson.New_route(tubson.GET, "foo", foo_route_handler)
type bar_response_body struct {
Response int
Message string
}
bar_route_handler := func(body tubson.Empty_request_body, raw_request *http.Request) (bar_response_body, tubson.Http_status_code) {
return bar_response_body {
Response: 400,
Message: "hi there on bar!",
}, tubson.INTERNAL_SERVER_ERROR
}
tubson.New_route(tubson.GET, "bar", bar_route_handler)
db := tubsql.Connect_database()
tubsql.Test_commit(db)
db.Close()
http_server := &http.Server{
Addr: ":8080",
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
log.Fatal(http_server.ListenAndServe())
}()
fmt.Println("Server started! Listening in on ", http_server.Addr)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
<-sigs
fmt.Println("Server stopped!")
os.Exit(1)
}