Basic golang web server.

master
Tomasz Polgrabia 2024-07-06 15:32:06 +02:00
parent 89f49add33
commit 6b50fca7e6
5 changed files with 175 additions and 1 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ output-vagrant
.git .git
.vagrant .vagrant
.secret .secret
*.iml
.idea

View File

@ -0,0 +1,3 @@
module golang_demo1
go 1.22

View File

@ -0,0 +1,97 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"time"
)
type PersonRequest struct {
Name string
Age int
}
type PersonResponse struct {
Greeting string
Errors string
}
type StatusResponse struct {
StartDateTime string
UpTime string
Status string
}
var startTime = time.Now()
func main() {
portPtr := flag.Int("port", 8080, "port for webserver")
flag.Parse()
port := *portPtr
fmt.Printf("Server is listening on port: %d", port)
server := http.NewServeMux()
server.HandleFunc("/status", statusHandler)
server.HandleFunc("/greeting", greetingHandler)
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", port), server)
if err != nil {
fmt.Println("Error:", err)
return
}
}
func greetingHandler(writer http.ResponseWriter, request *http.Request) {
decoder := json.NewDecoder(request.Body)
decoder.DisallowUnknownFields()
var p = PersonRequest{}
errDecode := decoder.Decode(&p)
if errDecode != nil {
println("Error: ", errDecode)
writer.WriteHeader(400)
_, _ = writer.Write([]byte("Bad request data"))
return
}
r := PersonResponse{
Greeting: fmt.Sprintf("Hey %s. Your age is %d", p.Name, p.Age),
Errors: "",
}
writer.Header().Add("Content-Type", "application/json")
encoder := produceEncoder(writer)
errEncode := encoder.Encode(&r)
if errEncode != nil {
writer.WriteHeader(500)
println("Error: ", errEncode)
return
}
writer.WriteHeader(200)
}
func produceEncoder(writer http.ResponseWriter) *json.Encoder {
encoder := json.NewEncoder(writer)
return encoder
}
func statusHandler(writer http.ResponseWriter, _ *http.Request) {
upTime := time.Now().Unix() - startTime.Unix()
status := StatusResponse{
Status: "Ok",
StartDateTime: startTime.Format("2006-01-02 15:04:05.000-0700"),
UpTime: fmt.Sprintf("%d", upTime),
}
writer.Header().Add("Content-Type", "application/json")
encoder := produceEncoder(writer)
errEncode := encoder.Encode(&status)
if errEncode != nil {
writer.WriteHeader(500)
println("Error: ", errEncode)
return
}
writer.WriteHeader(200)
}

View File

@ -0,0 +1,72 @@
package ch.polgrabia.demos.spring_demo1.utils;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ListsProgram {
public static class OneDirListItemIteratorImpl<T> implements Iterator<T> {
private OneDirListItem<T> item;
public OneDirListItemIteratorImpl(OneDirListItem<T> item) {
this.item = item;
}
@Override
public boolean hasNext() {
return item != null;
}
@Override
public T next() {
OneDirListItem<T> curr = item;
item = item.next;
return curr.val;
}
}
public static class OneDirListItem<T> implements Iterable<T> {
public T val;
public OneDirListItem<T> next;
@Override
public Iterator<T> iterator() {
return new OneDirListItemIteratorImpl<>(this);
}
}
public static <T> OneDirListItem<T> makeOneDirList(List<T> items) {
OneDirListItem<T> curr = new OneDirListItem<>();
OneDirListItem<T> start = curr;
OneDirListItem<T> prev = null;
for (T val : items) {
curr.val = val;
curr.next = new OneDirListItem<T>();
prev = curr;
curr = curr.next;
}
if (prev != null) {
prev.next = null;
}
return start;
}
public static void main(String[] args) {
int lastXNr = 5;
var l = makeOneDirList(Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
var lastXItems = new LinkedList<Integer>();
for (var el : l) {
lastXItems.addLast(el);
if (lastXItems.size() > lastXNr) {
lastXItems.removeFirst();
}
}
Integer first = lastXItems.getFirst();
System.out.printf("The last %d element is %d", lastXNr, first);
}
}

View File

@ -3,4 +3,4 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialec
spring.datasource.url=jdbc:postgresql://localhost:5432/app spring.datasource.url=jdbc:postgresql://localhost:5432/app
spring.datasource.username=postgres spring.datasource.username=postgres
spring.datasource.password=secret spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=validate