Skip to content

Commit

Permalink
Merge pull request #1 from fmhr/severBuild
Browse files Browse the repository at this point in the history
Sever build
  • Loading branch information
fmhr committed Sep 23, 2023
2 parents 8ed0bb0 + de3e35f commit 0552c15
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ test:
server: $(SERVER_BINARY)

$(SERVER_BINARY): $(GO_FILES)
go build -o $(BUILD_DIR)/$(SERVER_BINARY) $(SERVER_DIR)
CGO_ENABLED=0 go build -o $(BUILD_DIR)/$(SERVER_BINARY) $(SERVER_DIR)
32 changes: 27 additions & 5 deletions cloudRun.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
package fj

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"

"github.com/pelletier/go-toml/v2"
)

func CloudRun(cnf *Config, seed int) (map[string]float64, error) {
return googleCloudRun(cnf, seed)
return cloudRun(cnf, seed)
}

func googleCloudRun(cnf *Config, seed int) (map[string]float64, error) {
url := fmt.Sprintf("%s?seed=%d", cnf.CloudURL, seed)
func cloudRun(cnf *Config, seed int) (map[string]float64, error) {
baseURL, err := url.Parse(cnf.CloudURL)
if err != nil {
return nil, fmt.Errorf("error parsing cloud url: %v", err)
}
params := url.Values{}
params.Add("seed", strconv.Itoa(seed))
baseURL.RawQuery = params.Encode()
finalURL := baseURL.String()

conData, err := toml.Marshal(cnf)
if err != nil {
return nil, fmt.Errorf("error marshaling config: %v", err)
}

resp, err := http.Get(url)
resp, err := http.Post(finalURL, "application/toml", bytes.NewReader(conData))
if err != nil {
return nil, fmt.Errorf("error making GET request to %s: %v", url, err)
return nil, fmt.Errorf("error making POST request to %s: %v", finalURL, err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error making GET request to %s: %s", finalURL, resp.Status)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
Expand All @@ -29,6 +50,7 @@ func googleCloudRun(cnf *Config, seed int) (map[string]float64, error) {
if err := json.Unmarshal(body, &data); err != nil {
return nil, fmt.Errorf("error parsing response body: %v", err)
}
fmt.Println(mapString(data))

return data, nil
}
29 changes: 0 additions & 29 deletions cmd/server/Dockerfile

This file was deleted.

92 changes: 91 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,106 @@
package main

import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"

"github.com/fmhr/fj"
"github.com/pelletier/go-toml/v2"
)

func main() {
log.Println("starting server...")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Println("listening on port " + port)
http.HandleFunc("/", handler)

log.Fatal(http.ListenAndServe(":"+port, nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
log.Println("handling request...")
w.Write([]byte("hello"))
// seed
seedString := r.URL.Query().Get("seed")
if seedString == "" {
http.Error(w, "no seed specified", http.StatusBadRequest)
return
}
// seed
seed, err := strconv.Atoi(seedString)
if err != nil {
http.Error(w, "invalid seed specified", http.StatusBadRequest)
return
}
log.Println("seed:", seed)

var config fj.Config
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println("read body error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = toml.Unmarshal(body, &config)
if err != nil {
log.Println("toml unmarshal error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
config.Cmd = readCmd()
config.GenPath = "gen"
config.VisPath = "vis"
config.TesterPath = "tester"
reactiveString := r.URL.Query().Get("reactive")
if reactiveString == "" || reactiveString == "false" {
config.Reactive = false
} else {
config.Reactive = true
}
// GEN
fj.Gen(&config, seed)
// RUN
rtn, err := run(&config, seed)
if err != nil {
log.Println("run error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Println("rtn:", rtn)
jsonData, err := json.Marshal(rtn)
if err != nil {
log.Println("json marshal error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}

func run(cfg *fj.Config, seed int) (map[string]float64, error) {
if cfg.Reactive {
log.Println("reactive mode")
return fj.ReactiveRun(cfg, seed)
}
log.Println("normal mode")
return fj.RunVis(cfg, seed)
}

func readCmd() string {
cmd, err := ioutil.ReadFile("cmd.txt")
if err != nil {
log.Fatal(err)
}
return string(cmd)
}
56 changes: 33 additions & 23 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,48 @@ package fj

import (
"fmt"
"log"
"os"
"runtime"

"github.com/pelletier/go-toml/v2"
)

const (
configFileName = "fj_config.toml"
)
const configFileName = "fj_config.toml"

var ErrConfigNotFound = fmt.Errorf("%s not found, please run `fj -mode init`", configFileName)

type Config struct {
Cmd string `toml:"Cmd"`
Reactive bool `toml:"Reactive"`
TesterPath string `toml:"TesterPath"`
VisPath string `toml:"VisPath"`
GenPath string `toml:"GenPath"`
InfilePath string `toml:"InfilePath"`
OutfilePath string `toml:"OutfilePath"`
Jobs int `toml:"Jobs"`
CloudURL string `toml:"http://localhost:8080"`
Cmd string `toml:"Cmd"`
Args []string `toml:"Args"`
Reactive bool `toml:"Reactive"`
TesterPath string `toml:"TesterPath"`
VisPath string `toml:"VisPath"`
GenPath string `toml:"GenPath"`
InfilePath string `toml:"InfilePath"`
OutfilePath string `toml:"OutfilePath"`
Jobs int `toml:"Jobs"`
Cloud bool `toml:"Cloud"`
CloudURL string `toml:"CloudURL"`
}

func GenerateConfig() {
if _, err := os.Stat(configFileName); err == nil {
fmt.Printf("%s already exists\n", configFileName)
return
}
numCPUs := runtime.NumCPU() - 1
numCPUs := maxInt(1, runtime.NumCPU()-1)
conf := &Config{
Cmd: "",
Args: []string{},
Reactive: false,
TesterPath: "tools/target/release/tester",
VisPath: "tools/target/release/vis",
GenPath: "tools/target/release/gen",
InfilePath: "tools/in/",
OutfilePath: "tmp/",
OutfilePath: "out/",
Jobs: numCPUs,
Cloud: false,
CloudURL: "http://localhost:8888",
}
if err := generateConfig(conf); err != nil {
fmt.Println("Error: ", err)
Expand All @@ -58,18 +60,14 @@ func generateConfig(conf *Config) error {
defer file.Close()

encoder := toml.NewEncoder(file)
err = encoder.Encode(conf)
if err != nil {
return err
}
return nil
return encoder.Encode(conf)
}

func LoadConfigFile() (*Config, error) {
if _, err := os.Stat(configFileName); err != nil {
log.Printf("%s not found, please run `fj -mode init`\n", configFileName)
return &Config{}, err
if !configExists() {
return &Config{}, ErrConfigNotFound
}

conf := &Config{}
file, err := os.Open(configFileName)
if err != nil {
Expand All @@ -90,3 +88,15 @@ func checkConfigFile(cnf *Config) error {
}
return nil
}

func configExists() bool {
_, err := os.Stat(configFileName)
return err == nil
}

func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
67 changes: 67 additions & 0 deletions dockerfiles/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# ローカルで動かすための手順
# docker build --no-cache -t fj-server .# ローカルで動かすための手順
# docker run -p 8888:8080 --rm fj-server
# docker exec -it container_name sh

FROM rust:buster as rust-builder
WORKDIR /work
COPY /tools .
# コンテストによって、以下のコメントアウトを切り替える
# gen
RUN cargo build --release --bin=gen
RUN cp target/release/gen .
RUN chmod +x /work/gen
# vis リアクティブ問題のときコメントアウト
RUN cargo build --release --bin=vis
RUN cp target/release/vis .
RUN chmod +x /work/vis
# tester リアクティブな問題の時だけ使う
#RUN cargo build --release --bin=tester
#RUN cp target/release/tester .
#RUN chmod +x /work/tester

# fj/server
FROM golang:latest as golang-builder
RUN git clone -b severBuild https://github.com/fmhr/fj.git /go/src/github.com/fmhr/fj
WORKDIR /go/src/github.com/fmhr/fj/cmd/server
RUN CGO_ENABLED=0 go build -o fj-server
#COPY /Users/fumihiro/Desktop/fj /go/src/github.com/fmhr/fj
#WORKDIR /go/src/github.com/fmhr/fj/cmd/server
#RUN CGO_ENABLED=0 go build -o fj-server

# gここは提出言語によって変える
FROM golang:1.20 as golang-builder2
WORKDIR /work
COPY src/ .
RUN go mod init main
RUN CGO_ENABLED=0 go build -o main



# 新しいステージを開始し、最小限のイメージをベースにする
# gcloudにログインしてないと失敗する $ gcloud auth login
# プロジェクトをたててGoogle Container Registry APIを有効にする
#FROM gcr.io/google-cloud-builders/debian-slim

FROM debian:buster
WORKDIR /app
# バイナリをコピー
#COPY --from=rust-builder /work/tester /app/tester
COPY --from=rust-builder /work/gen /app/gen
COPY --from=rust-builder /work/vis /app/vis
COPY --from=golang-builder /go/src/github.com/fmhr/fj/cmd/server/fj-server /app/fj-server
COPY --from=golang-builder2 /work/main /app/main

# ローカル環境で必要な時だけ使う
#COPY ahc022-d9fa70931824.json /app/service_account_key.json
#ENV GOOGLE_APPLICATION_CREDENTIALS /app/service_account_key.json


RUN echo -n "./main" > cmd.txt
ENV PATH /app:$PATH

# ポート8080を公開
EXPOSE 8080

# バイナリを実行
CMD ["/app/fj-server"]
Loading

0 comments on commit 0552c15

Please sign in to comment.